#How do I make a .txt file able to be read in an IDE?

1 messages · Page 1 of 1 (latest)

vocal cliff
#

My code is supposed to read a txt file n assess the content n stuff, but IDK how to make the .txt file accessible to the IDE?

fiery sphinxBOT
#

<@&987246527741304832> please have a look, thanks.

fiery sphinxBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

vocal cliff
#

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

surreal crag
#

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?

vocal cliff
#

Idk how to import the file into IntelliJ

#

I think I did it once let me send that

#

I put it under src

surreal crag
#

Ok awesome

vocal cliff
#

Is that all I need to do??

#

Wait lemme check if it works or not

surreal crag
#

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.

vocal cliff
#

I'm not reallly following...

surreal crag
#

And what does readFile(file name) do? Return a list?

vocal cliff
#

Pretty much yeah

surreal crag
#

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)

vocal cliff
#

So do I put the .txt file into the out folder?

surreal crag
#

Or reference it in the src folder or somewhere else.

vocal cliff
#

Reference it?

surreal crag
#

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”.

vocal cliff
#

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

surreal crag
#

Not quite - we’ll need to know what exactly readFile() needs - do you have access to that method?

surreal crag
#

Can you create a gist?

#

I think the Bot gives an option to do so (but I am unsure how)

vocal cliff
#

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

surreal crag
#

Oh

#

You know - we could also just try to add “/“ before the file name haha and see if that helps.

vocal cliff
#

💀 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

surreal crag
#

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)

vocal cliff
#

With a . at the front or no?

#

💀 Im actually so bad at this

#

BOTH dont work

#

💀 aughh

surreal crag
#

no its ok - I

#

I'm not doing so hot here either haha

vocal cliff
#

💀 I don't know what this IDE wants ong

#

Ive tried so many paths

surreal crag
#

Let me play with it for one second

vocal cliff
#

Alr

surreal crag
#

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

vocal cliff
surreal crag
#

ok so the \ need to be \ if not already

#

oh \

#

oh

#

haha double \

vocal cliff
#

OH

#

OK lemme try

#

NOOOOO

#

GODDDDD

#

Saying the file doesnt exist

#

💀

vocal cliff
#

Were there any steps u took that i might not've

surreal crag
#

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

vocal cliff
#

HOLY SHIT

#

IT FUCKIIG. WORKED

#

YES

#

YES

#

OH MY GOD

#

FUCK

#

THANK YOU

#

HOLY SHIT

#

IT WORKS

surreal crag
#

haha

vocal cliff
#

HELL YEAHHHHH

surreal crag
#

ok wait

#

a better way

vocal cliff
#

Yea?

surreal crag
#

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

vocal cliff
#

ALR ALR

surreal crag
#

(Because now its just using a generic way of finding the file via the path versus it being hardcoded in)

vocal cliff
#

RadiationSample.class.getClassLoader().getResource("7_14_2019.txt").getFile()

#

for mine?

surreal crag
#

yes

vocal cliff
#

ALR

#

THANK YOU SO MUCH

#

YOU'RE SO SMART

surreal crag
#

hahaha not really

vocal cliff
#

I WOULDVE FAILED THIS WITHOUT YOU

#

THANKS SM

#

UR COOL AS HELL

visual rock
#

Usually you would put text files under resources

ancient swan