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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

writing data to file in java

Reply to Message Icon

Name: ibansah
Date: March 26, 2006 at 21:45:59 Pacific
OS: XP
CPU/Ram: 256
Product: 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



Sponsored Link
Ads by Google

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

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();


0

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

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


0

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

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.


0

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

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


0

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

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.


0

Related Posts

See More



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

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: writing data to file in java

Writing to a file in C www.computing.net/answers/programming/writing-to-a-file-in-c/10521.html

how to write binary file in visual www.computing.net/answers/programming/how-to-write-binary-file-in-visual-/11505.html

Writing text to files in QuickBASIC www.computing.net/answers/programming/writing-text-to-files-in-quickbasic/12906.html