Java File Writing

Pressure

Newbie
Joined
Jul 3, 2003
Messages
5,065
Reaction score
0
I can't for the life of me figure this out. No matter what I do I can't get Java to write information to a file. It's driving me crazy because I've done everything the book has told me to do and it still doesn't work. If you know Java could you look at this code and tell me what I'm doing wrong.

Code:
import java.io.*;
import java.util.*;

public class test {
	
	public static void main(String args[]) throws java.io.IOException {
		
		// Get name of file to be written
		System.out.print("Enter name of file to save:  ");
		String fileName = MyConsoleIO.getString();

		// Open file, Layer with BufferedWriter and PrintWriter
		FileWriter outfile = new FileWriter(fileName);
		BufferedWriter bufWrite = new BufferedWriter(outfile);
		PrintWriter output = new PrintWriter(bufWrite);

		// Write to file
		output.println("This is a test");
		
		// Close file
		outfile.close();		
	}
}

The MyConsoleIO is just a class I made for reading in information incase you were wondering. Can someone tell me what I'm doing wrong or at least show me a right way to do it?
 
I don't know java, but I have written\read from files with php.
With php there is a multiple ways to open a file.
Like
-Read Only
-Read\Write
-Write Only
-Overwrite\Write

I'd think there might be something like this with Java, and you may just be opening it up with Read Only.

I don't know, best to luck to ya though. I know it was a bitch for me to get PHP to write for the first time.
 
yes there is a thing in for read only.

FileWriter outfile = new FileWriter(fileName);

change it to

FileWriter outfile = new FileWriter(fileName,true);

true means it re-writable...

also i don't believe you need the Buffered Writer. For sequential files I only use this statement

PrintWriter out = new PrintWriter(new FileWriter("file.txt",true));

and it works fine...
 
What you should do is to instead of letting main throw the IOException is to add a try ... catch. In the catch, place System.out.println(e); or something like that, this information will probably give you the error there is.
A good (safe) way of writing to a file, using your code, is as follows:
Code:
import java.io.*;

public class test {

    public test() {
        System.out.print("Enter name of file to save:  ");
        String fileName = MyConsoleIO.getString();

        try {
            FileWriter outfile = null;
            BufferedWriter bufWrite = null;
            PrintWriter output = null;
            try {
                outfile = new FileWriter(fileName);
                bufWrite = new BufferedWriter(outfile);
                output = new PrintWriter(bufWrite);

                output.println("This is a test");
            } finally {
                close(output);
                close(bufWrite);
                close(outfile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void close(Writer w) throws IOException {
        if (w != null) {
            w.close();
        }
    }

    public static void main(String args[]) {
        new test();
    }

}
 
SenorDingDong said:
yes there is a thing in for read only.

FileWriter outfile = new FileWriter(fileName);

change it to

FileWriter outfile = new FileWriter(fileName,true);

true means it re-writable...

also i don't believe you need the Buffered Writer. For sequential files I only use this statement

PrintWriter out = new PrintWriter(new FileWriter("file.txt",true));

and it works fine...
Not entirely correct, the optional 'true' passed to the constructor is to append date to the file, or to start from the beginning.
 
Ansur said:
Not entirely correct, the optional 'true' passed to the constructor is to append date to the file, or to start from the beginning.

yea i was thinking Random Access I haven't done sequential for awhile.
 
I absolutely HATE java file IO, its a lot easier in C/C++ with fopen and fstream
 
Its not that bad :)

For reading (text)files you can also use the Scanner class (since 1.5), which only takes 1 line to start reading.
 
Back
Top