0

I am so sorry if this has been posted before.

What I am trying to do is write and read a simple string to a file located in one of the packages in my project. Inside Source Packages I have a package called "resources". Here i want to read a file called myfile.txt, do some stuff with it and save it again as newmyfile.txt. Just simple reading and storing of CSV data.

I have created a reader, which looks like this:

public BufferedReader getFileReader(String fileNameWithExtension, String sourcePackage) {
        try {
            String file = "/" + sourcePackage + "/" + fileNameWithExtension;
            BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(file)));
            return br;
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            return null;
        }
    }

This works fine on my Mac. Reading up on this i find out that i have to use File.separator in order to get it to work on all operating systems. So i change this, and still it don´t work. Somewhere it is said that a relative path always starts with / regardless of operating system when using relative paths, and other places ../. As a complete newbie on file storage on Java, I am confused.

There is also a lot of ways to read and write files, and I don´t know what method to choose, and why.

What is the best way for me to read and create files where all I want to do is save one long string, or an array of strings?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Using forward slashes on windows from Java works fine. Things become more complicated when you need things like the drive letter as well... – akaIDIOT Nov 09 '12 at 09:13
  • If you are working with Eclipse or other IDEs.you are doing something wrong with file location. – sunleo Nov 09 '12 at 09:34
  • *"i find out that i have to use File.separator"* No!! Not when using `getResource(String)`. It ***always*** takes forward slash. – Andrew Thompson Nov 09 '12 at 09:34
  • you mean he can't use File.separator,because it will work with getResource(String) but not with getResourceAsStream(String).Am I right?If I am wrong correct me Please. – sunleo Nov 09 '12 at 09:53
  • @AndrewThompson yes thank you.I read something.I got it. – sunleo Nov 09 '12 at 10:00
  • @AkaIDIOT: I do not need the drive letter, only the path to a file relative to the source folder of my project. – Aleksander Sjåfjell Nov 09 '12 at 16:51
  • @Andrew Thompson: I am using Java packages to separate between logic, models and resources. Inside these, I have regular .java class files, not jars. This is a simple project where I only use packages to better group class files. – Aleksander Sjåfjell Nov 09 '12 at 16:53
  • @Sunleo: I am using NetBeans, but that should not make any difference? Checking the actual file hierarchy shows that a package create a subfolder, and within the folder i find the .txt-files I am using. These also shows up under the resources package in project explorer. – Aleksander Sjåfjell Nov 09 '12 at 16:55
  • *"Inside these, I have regular .java class files, not jars."* You mkisunderstood me."* I suspect your **resources and classes** that are in packages and directories are now **inside a Jar.** Do you use an IDE? – Andrew Thompson Nov 09 '12 at 22:02
  • @AndrewThompson: Sorry for late answer. Yes, this is a Java Application. I use Netbeans as my IDE. – Aleksander Sjåfjell Nov 12 '12 at 07:02
  • Well, Netbeans will typically package the resources into a Jar before running them. – Andrew Thompson Nov 12 '12 at 07:04
  • Will that make any difference to how i read and write files? – Aleksander Sjåfjell Nov 12 '12 at 11:31

2 Answers2

1

I have busy with other things but have now, with help from you, found a solution that works on mac and windows.

For some weird reason, String file = "/" + sourcePackage + "/" + fileNameWithExtension;is the only thing that works for reading on my mac. Suddenly (no code changes done) it started to work on windows as well. I do not know why. So working code for getting a reader is now:

public BufferedReader getFileReader(String fileNameWithExtension, String sourcePackage) {
        try {
            String file = "/" + sourcePackage + "/" + fileNameWithExtension;
            BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(file)));
            return br;
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            return null;
        }
    }

For writing to files in the resourses folder, i used:

public BufferedWriter getFileWriter(String fileNameWithExtension, String sourcePackage) {
        Writer output = null;
        File file = new File("src" + File.separator + sourcePackage + File.separator + fileNameWithExtension);
        try {
            output = new BufferedWriter(new FileWriter(file));            
            return (BufferedWriter) output;
        } catch (IOException ex) {
            Logger.getLogger(FileHelper.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

Sadly, not the same way of getting the Readeras the Writer, but it works.

0

The key thing is that absolute paths always begin with a '/'. Relative paths must not begin with a '/'. That's at least part of what's causing problems for you.

For the rest, you say "it doesn't work". But what doesn't work?

Jochen
  • 2,277
  • 15
  • 22
  • Would you then have a '/' in front of: 'c:/Path/Textfile.txt'? I am using "String file = "/" + sourcePackage + "/" + fileNameWithExtension;" in my code, and this is intended to be a relative path, but i have '/' in front of it and it works. Is this ambiguity, or is it some way removed by parsing? – Aleksander Sjåfjell Nov 09 '12 at 17:01
  • I'd be surprised if that works on a Mac. It should certainly work without the '/' and that would also be more portable. – Jochen Nov 09 '12 at 20:38