Skip to main content
gamedev.net
🔒 Locked

[java] File I/O

Started by BartG Oct 18, 2001 at 10:17 AM 5 replies 950+ views
Original Post
BartG
BartG
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..?
WayfarerX
WayfarerX
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

"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
BartG
BartG
Can you be a bit more indepth like a full fledged example?
GKW
GKW
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
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson
WayfarerX
WayfarerX
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
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
GKW
GKW
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
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson
BartG
BartG
Thanks for the help guys! I got my program working great now

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.