Computing.Net > Forums > Programming > writing data to file in java

writing data to file in java

Reply to Message Icon

Original Message
Name: ibansah
Date: March 26, 2006 at 21:45:59 Pacific
Subject: writing data to file in java
OS: XP
CPU/Ram: 256
Model/Manufacturer: AMD
Comment:

Can somebody help me on how to write data to file in java and how do i specify the location of the file as well. Thanks


Report Offensive Message For Removal


Response Number 1
Name: DEOS_NR1
Date: March 27, 2006 at 06:38:50 Pacific
Reply: (edit)

FileOutputStream fo = new FileOutputStream(PATH_TO_OUTPUTFILE);
PrintStream ps = new PrintStream(fo);

// write here your own code.
// with ps.println you can print a line to the file specified by PATH_TO_OUTPUTFILE.

ps.close();
fo.close();


Report Offensive Follow Up For Removal

Response Number 2
Name: ibansah
Date: March 27, 2006 at 09:20:35 Pacific
Reply: (edit)

Thanks, in specifying path of the file do you use "c:/xxx/xxx/xxx" or "c:\xxx\xxx\xxx"
simply backslash or forwardslash
thanx


Report Offensive Follow Up For Removal

Response Number 3
Name: DEOS_NR1
Date: March 27, 2006 at 10:01:24 Pacific
Reply: (edit)

You can use forward slashes. For example, "d:/data/data.dat".

BTW: Don't forget to import java.io.* and to implement a try-catch statement around the code in my first reply: FileOutputStream can throw an Exception. So, the following code will do the job.

import java.io.*;

class test {
public static void main(String[] argv) {
try {
FileOutputStream fo = new FileOutputStream("d:/data/data.dat");
PrintStream ps = new PrintStream(fo);
ps.println("BLA");

ps.close();
fo.close();
} catch (Exception e) {}
}
}

Make sure that the directory "d:\data\" exists.


Report Offensive Follow Up For Removal

Response Number 4
Name: ibansah
Date: March 27, 2006 at 14:07:41 Pacific
Reply: (edit)

DEOS_NR1, Thanks for the suggestions. can you forward me your email add so that i can send you a copy of the particular section am having problem with so that you can look at.It does not seem to be working for me. Thanks


Report Offensive Follow Up For Removal

Response Number 5
Name: ibansah
Date: April 18, 2006 at 16:05:52 Pacific
Reply: (edit)

I have tried this method and it did not work. But i printed the error as u indicated in your previous mail and this is what it brought:
Java.Security.AccessControlException:access denied (java.io.FilePermission D:\data\bla
I figured it out that it could have something to do with my security permissions and have no clue how to go about it as am still new to java. Hope somebody can help.


Report Offensive Follow Up For Removal


Response Number 6
Name: shane69
Date: April 28, 2006 at 20:36:48 Pacific
Reply: (edit)

in java you have to use two back slashes to create a directory path like so C:\\program\\sub1 and so on because a single slash has a meaning


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: writing data to file in java

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge