Archived

This topic is now archived and is closed to further replies.

BartG

[java] File I/O

Recommended Posts

BartG    122
Hello, I recently started working with File I/O in Java. I know how to do the basic reading and writing to data files but I was wondering if anyone can tell me the process so that I can write data to a file without erasing any previous data. Because right now let''s say I want to add the word "Hello" to the file mydocument.txt and mydocument.txt already contains words, it will delete all those words and just put "Hello" so the solution to this is..?

Share this post


Link to post
Share on other sites
WayfarerX    130
new FileOutputStream("my_file_name.txt", true);

(the "true" is for the parameter "append")

"If consquences dictate our course of action, it doesn''t matter what''s right, it''s only wrong if you get caught."
- Tool

Share this post


Link to post
Share on other sites
GKW    200
public FileOutputStream(String name,
boolean append)
throws FileNotFoundException

Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.

First, if there is a security manager, its checkWrite method is called with name as its argument.

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning

Throws:
FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.
SecurityException - if a security manager exists and its checkWrite method denies write access to the file.

Since:
JDK1.1
See Also:
SecurityManager.checkWrite(java.lang.String)


The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato

Share this post


Link to post
Share on other sites
WayfarerX    130
Just so you know, what GKW posted came from the Java API Specification. Bookmark it, you will need it over and over again.

as for an example:

        
import java.io.*;
class AppendTest {
public static void main (String args[]) {
try {
String msg = "Hello World";
// Doing this will overwrite the contents of your

// file with the String "Hello World"

FileOutputStream out1 = new FileOutputStream("my_file.txt");
out1.write(msg.getBytes());
out1.close();
// However, by adding that little true in there,

// "Hello World" will be appended to the end of the file

FileOutputStream out2 = new FileOutputStream("my_file.txt", true);
out2.write(msg.getBytes());
out2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


If you run this, you will end up with a file named "my_file.txt" containing the text "Hello WorldHello World".

BTW: GKW, your signature rocks.

// EDIT == FORMATTING

"If consquences dictate our course of action, it doesn't matter what's right, it's only wrong if you get caught."
- Tool



Edited by - WayfarerX on October 18, 2001 6:22:51 PM

Share this post


Link to post
Share on other sites
GKW    200
You can also download them if you don''t have a perminant internet connection.

It was that or

You gotta use premium dude! Premium! Dude!
--Snake

The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato

Share this post


Link to post
Share on other sites