• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

read/write files working on a Pc not on a Mac


Here's my problem this program run's nicely when run on a pc (j2re1.4.2_03)
but .. when it's run on a Mac (jre1.4.2) it does not find the file.
strangely enough a System.out.printn tells me it look at the right location wich is:

Users/pcName/path/fileName

On the pc it shows:

C:/path/fileName and it's working.

Do you have any idea what may be wrong?

________________________________________
// class variable
private File settingsFile;

private void initialize() {
                //  stuff....
       
      // get details from the settings.bw file
      URL url = getClass().getResource("settings" + java.io.File.separator + "settings.bw");
      String fileIdentifier = url.toString();        
       
      // just to take the space in a file name into account.
      fileIdentifier = Compatibility.replaceAll(fileIdentifier, "%20", " ");

      // remove file:/ at the begining of the fileIdentifier
      fileIdentifier = fileIdentifier.substring(fileIdentifier.indexOf("/")+1);

      try {
            settingsFile  = new File(fileIdentifier);
            FileReader fileReader = new FileReader(fileIdentifier);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String nextLine = bufferedReader.readLine();
            while (nextLine!=null) {
                  nextLine = nextLine.trim();
                  // do things
            }
            fileReader.close();
            fileReader = null;
            bufferedReader.close();
            bufferedReader = null;
      } catch (Exception e) {
            e.printStackTrace();
      }
      
      // more stuf....
}


// update the information in a file
private void updateProgramSettings() {
        // stuff....

      try {
            settingsFile.delete();
            settingsFile.createNewFile();
            FileWriter fileWriter = new FileWriter(settingsFile);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(settingsString1, 0, settingsString1.length());
            bufferedWriter.newLine();
            bufferedWriter.write(settingsString2, 0, settingsString2.length());
            bufferedWriter.newLine();
            bufferedWriter.write(settingsString3, 0, settingsString3.length());
            bufferedWriter.close();
            fileWriter.close();
            bufferedWriter = null;
            fileWriter = null;
      } catch (Exception e) {
            e.printStackTrace();
      }
}
0
SolangeRichard
Asked:
SolangeRichard
  • 6
  • 4
  • 3
2 Solutions
 
CEHJCommented:
>>URL url = getClass().getResource("settings" + java.io.File.separator + "settings.bw");

URLs don't contain file separators on Windows btw. Only Unix-based and Mac
0
 
CEHJCommented:
(Still looking at the actual problem ;-))
0
 
CEHJCommented:
Still haven't diagnosed the problem, but this is all you need for platform independence (a beginning forward slash could be required):


InputStream is = getClass().getResourceAsStream("settings/settings.bw");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
Make sure settings.bw is in the directory settings relative to the location of the class making the call to getResource(), and also that it is available from the classpath.
0
 
SolangeRichardAuthor Commented:
object: Since it's working fine from a pc I imagine the call is fine.  Am I wrong in assuming this?
0
 
CEHJCommented:
It's more the parameters to the calls - not the calls themselves. Is settings.bw stored in a jar or the file system?
0
 
CEHJCommented:
Actually i think you're using the filesystem. If you place your setting file (settings.bw) under your application root, the last code i posted should work on any platform
0
 
SolangeRichardAuthor Commented:
This settings.bw is stored on the file system.  Initially it contains "New Install".
Then I ask for the licence number, Name and other info.  Once I have those info, it's stored on the settings.bw file.  So next time the application is open, the default value are displayed on the login screen.

I will do as you say to read the file but I still wonder How could I then, overwrite the file with the new values?

Will something like that work?

PrintWriter q = new PrintWriter( new FileOutputStream("settings/settings.bw") , true);
q.println("info");
0
 
objectsCommented:
> object: Since it's working fine from a pc I imagine the call is fine.  Am I wrong in assuming this?

Thats correct, you need to check that the classpath and location of the resource are setup so the file is found.

> This settings.bw is stored on the file system.

Make sure it is accessible from the classpath, using the name supplicaed (rfelative to the class making the call)

> Will something like that work?
> PrintWriter q = new PrintWriter( new FileOutputStream("settings/settings.bw") , true);

No, what you have is fine.
0
 
CEHJCommented:
If you have your settings in the form of key + value, e.g.

x=y

or

x    y

then you can do

Properties props = new Properties();
// load them
props.load(new FileInputStream("settings/settings.bw"));

...

// save them
props.store(new FileOutputStream("settings/settings.bw"), null);
0
 
SolangeRichardAuthor Commented:
Sorry for the delay, I was busy with an other project for a while.

Here's the structure of the program and what I did to make it work.

rootFolder
      constants
      data
      elements
      help
      java_bytecode       // default output folder
      main            // This is where the login reads and update the file
      mysql
      .classpath
      .project
      bwsettings.dat  // the file to read and write to.



Now this code works both on mac and pc

      try {
       BufferedReader in = new BufferedReader(new FileReader
                                               ("bwSettings.dat")); //$NON-NLS-1$

       String nextLine = in.readLine();
       while (nextLine != null) {
          // do stuff
       }
       in.close();
      } catch (FileNotFoundException e1) {
       e1.printStackTrace();
      } catch (IOException e1) {
       e1.printStackTrace();
      }
     
//////////////////

try {
      // save new settings to settings.dat
      java.io.PrintWriter out = new java.io.PrintWriter(new FileWriter(
              "bwSettings.dat")); //$NON-NLS-1$
      out.println(settingsString1);
      out.println(settingsString2);
      out.println(settingsString3);
      out.close();

} catch (java.io.IOException exception) {
      exception.printStackTrace();
}

///////////////////

The structure use to be:

rootFolder
      constants
      data
      elements
      help
      java_bytecode       // default output folder
      
      main            // This is where the login reads and update the file
         settings
               bwsettings.dat
      
      mysql
      .classpath
      .project
      
And I was not abel to make it work on the mac.      
Now, if I wish to return to this structure... How should I modify the code?

I know this question must be very basic but I never really worked with I/O.
I looked in the books to get the solution I have right now.. but it seem very limited to have to access the file directly at the root folder.

Thank you for your comments and suggestions.
0
 
objectsCommented:
that code will only work if you run it from rootFolder.
If you return to your original structiure you could achive the same by ruuning the app from rootFolder/main/settings (though that may break other code if you use other relative path names).

if running from rootFolder try:

"main/settings/bwsettings.dat"

0
 
SolangeRichardAuthor Commented:
I'm sorry I forgot about this open question since I did not really found any solution to my problem I just moved the file to the root folder wich worked fine.
I think it is fair to split the point between CEHJ and objects since they both helped me to find a working solution for me.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now