#How do I make a .txt file able to be read in an IDE?
1 messages · Page 1 of 1 (latest)
<@&987246527741304832> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
This is how I reference the file btw:
List<RadiationSample> radiationSamples = readFile("7_14_2019.txt");
int maxCount = findMaxCounts(radiationSamples);
System.out.println("Max count is : " + maxCount);
printWithinFiveCountsFromMax(radiationSamples, maxCount);```
Thats just a snippet of code ofc but yk
Where is the file? Because it doesn’t appear that you are accessing it in the directory with the current class. Is it in the source folder?
Idk how to import the file into IntelliJ
I think I did it once let me send that
I put it under src
Ok awesome
It won’t work since the class files are what are running - which would be found in the ‘out’ folder
Depending on how you want to access the file you will have to either give the directory that it is in (like src) or just place it in the same folder as the file that will be running or trying to access that file. The first one will give you more experience since it’s unlikely the file will be in the same place.
I'm not reallly following...
And what does readFile(file name) do? Return a list?
Pretty much yeah
So the .class files are what actually run. The .Java files are just the programming. So when the code executes, it searches for your file with the name “…2019…”. It will look where it is being executed, which will be that out folder (once the file has been compiled)
So do I put the .txt file into the out folder?
Or reference it in the src folder or somewhere else.
Reference it?
What exactly is the readFile() doing? Did you write it?
When you pass in the name, you can include a directory as well such as “../out/filename.txt”.
My program is supposed to be doing: "Find the maximum number of counts per minute in the file and write all of the data lines to the console that are within 5 counts of the maximum number of counts per minute. (for example, if you find that the maximum number of counts per minute is 50, write out every line that has 45 counts or greater.)"
I just sent that lil bit to show what name I was using for the file
Just "/out/7_14_2019.txt"?
Not quite - we’ll need to know what exactly readFile() needs - do you have access to that method?
https://www.baeldung.com/reading-file-in-java
(This may also help if you find what I am saying isn’t helping)
Do you want to see the code?
Can you create a gist?
I think the Bot gives an option to do so (but I am unsure how)
I think there's an option for that yea
OH not in the IDE
alr let me just copy n paste the code here and see
💀 okay I dont have nitro so let me just try to do this
Oh
You know - we could also just try to add “/“ before the file name haha and see if that helps.
💀 Same error
But the IDE lets me make a GIST thing
So ill do that and you can look
just give me a sec
Ngl
I have NO idea how to share it
HAHA I hope that works
Thats my code
The file is like
56 MB
the .txt one
Ok I see
So try and place it in the out folder (in your system not the IDE) and reference it via “./…2019…txt” (with the actual name haha)
With a . at the front or no?
💀 Im actually so bad at this
BOTH dont work
💀 aughh
Let me play with it for one second
Alr
I got it to work with the full path
(I am on windows so):
C:\Users\username\IdeaProjects\Rad\out\production\7_14_2019.txt
For example
💀 Illegal escape character error
How did you find this? Is there any way I can verify im using the right path stuff
Were there any steps u took that i might not've
Right so in the left side window - right click on the text file and (if in IntelliJ) it will give an option like copy absolute path
HOLY SHIT
IT FUCKIIG. WORKED
YES
YES
OH MY GOD
FUCK
THANK YOU
HOLY SHIT
IT WORKS
haha
HELL YEAHHHHH
Yea?
Place this into readFile() in the parenthesis
YourClassName.class.getClassLoader().getResource("7_14_2019.txt").getFile()
and ensure the file stays in the out file
This makes the code slighty more portable
ALR ALR
(Because now its just using a generic way of finding the file via the path versus it being hardcoded in)
RadiationSample.class.getClassLoader().getResource("7_14_2019.txt").getFile()
for mine?
yes
hahaha not really
Usually you would put text files under resources
@surreal crag
You shouldn't use getResource but getResourceAsStream