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();
      }
}
SolangeRichardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.