How to write a String to a file in Java?

hier describes how to write a String to a File.

First Example:

new FileWriter(file) -> to write content to a file. All existing content will be overridden.

package com.nourit.javaIO;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteStringToFile {

  public static void main(String[] args) {
    try {
      File file = new File("D:/tmp/nourit.txt");
      FileWriter fileWriter = new FileWriter(file);
      fileWriter.write("This is ");
      String nouritText = "a test from nour-it.com";
      fileWriter.write(nouritText);
      System.out.println(nouritText + " writted in the file...");
      fileWriter.flush();
      fileWriter.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}

Output:

a test from nour-it.com writted in the file...

Second Example:

BufferedWriter to write content to a file.

package com.nourit.javaIO;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteStringToFile2 {

  public static void main(String[] args) throws IOException {
    BufferedWriter bufferedWriter = null;
    FileWriter fileWriter = null;

    try {
      String content = "This is the content of nourit-test to write into file\n";
      fileWriter = new FileWriter("D:/tmp/nourit.txt");
      bufferedWriter = new BufferedWriter(fileWriter);
      bufferedWriter.write(content);

      System.out.println("is written...Done");

    } catch (IOException e) {
      e.printStackTrace();
    } finally {

      if (bufferedWriter != null){
        bufferedWriter.close();
      }
      if (fileWriter != null){
        fileWriter.close();
      }

    }

  }

}

Output:

is written...Done

Third Example:

package com.nourit.javaIO;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteStringToFile3 {

  private static BufferedWriter bufferedWriter;

  public static void main(String[] args) {
    try{
      bufferedWriter = new BufferedWriter(new FileWriter("D:/tmp/nourit.txt"));
      String content = "This is the content of nourit-test2 to write into file\n";
      bufferedWriter.write(content);
      // You don't need to close anything is taking care of that. 
      //bw.close();
      System.out.println("is written...Done");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output:

is written...Done
Fourth Example:

FileOutputStream to write content to a file.

package com.nourit.javaIO;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteStringToFileWithFileOutputStream {

  public static void main(String[] args) {
    File file = new File("D:/tmp/nourit.txt");
    String content = "This is the content of nourit-test to write into file\n";

    try (FileOutputStream fop = new FileOutputStream(file)) {
      // get the content in bytes
      byte[] contentInBytes = content.getBytes();

      fop.write(contentInBytes);
      fop.flush();
      fop.close();

      System.out.println("is written...Done");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output:

is written...Done

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*