Skinnyginny1973
Skinnyginny1973

Reputation: 19

How to create and output to files in Java

My current problems lie with the fact that no matter what solution I attempt at creating a file in Java, the file never, ever is created or shows up.

I've searched StackOverflow for solutions and tried many, many different pieces of code all to no avail. I've tried using BufferedWriter, PrintWriter, FileWriter, wrapped in try and catch and thrown IOExceptions, and none of it seems to be working. For every field that requires a path, I've tried both the name of the file alone and the name of the file in a path. Nothing works.

//I've tried so much I don't know what to show. Here is what remains in my method: 

FileWriter fw = new FileWriter("testFile.txt", false);
PrintWriter output = new PrintWriter(fw);
fw.write("Hello");

I don't get any errors thrown whenever I've run my past code, however, the files never actually show up. How can I fix this? Thank you in advance!

Upvotes: 0

Views: 921

Answers (2)

Harshal Parekh
Harshal Parekh

Reputation: 6017

There are several ways to do this:

Write with BufferedWriter:

public void writeWithBufferedWriter() 
  throws IOException {
    String str = "Hello";
    BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    writer.write(str);

    writer.close();
}

If you want to append to a file:

public void appendUsingBufferedWritter() 
  throws IOException {
    String str = "World";
    BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));
    writer.append(' ');
    writer.append(str);

    writer.close();
}

Using PrintWriter:

public void usingPrintWriteru() 
  throws IOException {
    FileWriter fileWriter = new FileWriter(fileName);
    PrintWriter printWriter = new PrintWriter(fileWriter);
    printWriter.print("Some String");
    printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
    printWriter.close();
}

Using FileOutputStream:

public void usingFileOutputStream() 
  throws IOException {
    String str = "Hello";
    FileOutputStream outputStream = new FileOutputStream(fileName);
    byte[] strToBytes = str.getBytes();
    outputStream.write(strToBytes);

    outputStream.close();
}

Note:

  1. If you try to write to a file that doesn’t exist, the file will be created first and no exception will be thrown.
  2. It is very important to close the stream after using it, as it is not closed implicitly, to release any resources associated with it.
  3. In output stream, the close() method calls flush() before releasing the resources which forces any buffered bytes to be written to the stream.

Source and More Examples: https://www.baeldung.com/java-write-to-file

Hope this helps. Good luck.

Upvotes: 1

user27158
user27158

Reputation: 307

A couple of things worth trying:

1) In case you haven't (it's not in the code you've shown) make sure you close the file after you're done with it

2) Use a File instead of a String. This will let you double check where the file is being created

File file = new File("testFile.txt");
System.out.println("I am creating the file at '" + file.getAbsolutePath() + "');
FileWriter fw = new FileWriter(file, false);
fw.write("Hello");
fw.close();

As a bonus, Java's try-with-resource will automatically close the resource when it's done, you might want to try

File file = new File("testFile.txt");
System.out.println("I am creating the file at '" + file.getAbsolutePath() + "');
try (FileWriter fw = new FileWriter(file, false)) {
    fw.write("Hello");
}

Upvotes: 1

Related Questions