Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Reading Files in Java

Name: Anonymous 2010-11-27 12:30

Whenever I read a file in java I always have to put the text file in some arbitrary folder. How can I make my program read/writes files from the same folder that its in.

I want this to work so that even if its moved to another folder or computer, it still works.

Name: Anonymous 2010-11-27 12:57

Sorry, what?

Name: Anonymous 2010-11-27 12:58

How can I make my program read/writes files from the same folder that its in
By reading the file.  The directory context with respect to the *.class or *.jar always starts from ./ if no other paths are assigned.

Name: Anonymous 2010-11-27 13:01

Stop giving it absolute paths.

Name: Anonymous 2010-11-27 13:08

We'd avoid these kind of amateur questions if we would just put all of our computer's files into a single directory.

Name: Anonymous 2010-11-27 13:34

I'm not really understanding what you guys are suggesting.

How would I make this:
String fileName="src/className/SetupFileInfo.txt";
BufferedReader br = null;       
FileReader fr = new FileReader(fileName);
br = new BufferedReader(fr);

not point to an absolute path exactly?

Name: Anonymous 2010-11-27 13:44

Name: Anonymous 2010-11-27 13:54

>>7
Yes, I am. Please enlighten me.

I tried ./SetupFileInfo.txt and ../SetupFileInfo.txt and neither worked.

Name: Anonymous 2010-11-27 13:57

>>6
At this point, I would like a vaerification of what you're trying to do; please clarify if you're actually asking the program to find SetupFileInfo.txt from an arbitrary and unreliable location, that it may or may not be (within) the local directory?

Otherwise, you're already doing what we're saying more or less.  You can just as easily use String fileName="./SetupFileInfo.txt"; to use the file from within the local directory, rather than a sub-directory, of the application's class file.  (Make sure the file is actually in that directory, of course.)

Name: Anonymous 2010-11-27 14:02

>>8
Do either of those files exist?

Name: Anonymous 2010-11-27 14:07

I have to send all the code I'm writing in one folder with no subfolders to my professor. I want to make sure that regardless of what folder he runs it from, that it will be able to read my setup file to get the right data. I'm sure that the setup file is with the rest of my java files that are using it, but whenever I try to read it without using this path (src/packageName) it fails to find it.

Name: Anonymous 2010-11-27 14:09

Please sage your shitty posts, fuckface.

Name: Anonymous 2010-11-27 14:10

Oh yeah, the direction of the (forward/back) slash is OS dependent.  Use java.io.separatorChar to be certain that you're using the correct one; asides from that Java may also want you to escape the appropriate slash (e.g., "\\") when you write the file path in a String object.
It gets real finicky about escape characters.

Name: Anonymous 2010-11-27 14:17

>>13
Oh yeah, the direction of the (forward/back) slash is OS dependent.
Wait, really? You mean that Java doesn't do what good old C does? (Which is not being an anus and accepting the `/' separator on all systems.)

Name: Anonymous 2010-11-27 14:18

>>12
eat shit faggot

>>1
Stop using Java. Also, what operating system are you on?

Name: Anonymous 2010-11-27 14:23

>>15
Windows.

I'm pretty sure I have the slashes in the right direction since I've been able to find the file using "src/packageName/SetupFileInfo.txt"

Name: Anonymous 2010-11-27 14:25

>>16
How are you starting your application?

Name: Anonymous 2010-11-27 14:29

>>14
I'm talking about portability.  Assuming that source code will be compiled on a Windows computer in an academic setting is not valid.

Name: Anonymous 2010-11-27 16:28


+----ANUSWARE     (<--- your program MAIN folder)
        |
        +-------FShit (<-- Some folder with a file you might want to read)
        |
        +-------FSomeOtherFolder


Let's name the file you try to read as data.dat

- Let's say that you want to save the folder in ANUSWARE (where your main program lives). Then you will refer to it as 'data.dat' in your open function.

- If data.dat lives on Fshit, then you will refer to the file as 'Fshit\data.dat'      
       
- If you want to use an arbitrary (random?) existing folder, anywhere in the drive, then you can use the Windows registry to save it's location

Name: Anonymous 2010-12-22 14:33

Name: Anonymous 2010-12-25 9:34


Don't change these.
Name: Email:
Entire Thread Thread List