A summary of read write method instances for text files in Java

  • 2020-04-01 04:22:31
  • OfStack

This example summarizes the read-write methods for text files in Java. Share with you for your reference, as follows:

Write text data

Method one:


import java.io.*;
public class A {
  public static void main(String args[]) {
    FileOutputStream out;
    PrintStream ps;
    try {
      out = new FileOutputStream("a.txt");
      ps = new PrintStream(out);
      ps.println("qun qun.");
      ps.println("fei fei");
      ps.close();
    } catch (Exception e) {
      System.out.println(e.toString());
    }
  }
}

Method 2:


import java.io.*;
public class B {
  public static void main(String args[]) {
    FileWriter fw;
    PrintWriter pw;
    try {
      fw = new FileWriter("b.txt");
      pw = new PrintWriter(fw);
      pw.print("qunqu n ");
      pw.println("feiefi ss");
      pw.print("qunqu n ");
      pw.close();
      fw.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}

Method 3:


import java.io.*;
public class C {
  public static void main(String args[]) {
    String str_written = "This is a simple example";
    try {
      FileWriter fwriter = new FileWriter("c.txt");
      BufferedWriter bfwriter = new BufferedWriter(fwriter);
      bfwriter.write(str_written, 0, str_written.length());
      bfwriter.flush();
      bfwriter.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}

Note: method 1 and method 2, method 3 are created when the operation text file does not exist, otherwise, when overwritten!

On the other; Methods three

BufferedWriter provides efficient writes of individual characters, arrays, and strings by writing text to the character output stream, buffering individual characters.

Attachment: append write:


import java.io.*;
public class C {
  public static void main(String args[]) {
    String str_written = "This is a simple example";
    try {
      FileWriter fwriter = new FileWriter("c.txt", true);
      BufferedWriter bfwriter = new BufferedWriter(fwriter);
      bfwriter.newLine();
      bfwriter.write(str_written, 0, str_written.length());
      bfwriter.flush();
      bfwriter.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}

Read text data

Method one:


import java.io.*;
public class A {
  public static void main(String args[]) {
    try {
      FileInputStream fstream = new FileInputStream("a.txt");
      DataInputStream in = new DataInputStream(fstream);
      while (in.available() != 0) {
        String a = in.readLine();
        System.out.println(a);
        System.out.println(a.length());
      }
      in.close();
    } catch (Exception e) {
      System.out.println(e.toString());
    }
  }
}

Method 2:


import java.io.*;
public class B {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader("a.txt");
      BufferedReader br = new BufferedReader(fr);
      String str;
      int count = 0;
      while ((str = br.readLine()) != null) {
        count++;
        System.out.println(count + " : " + str);
      }
      br.close();
      fr.close();
    } catch (Exception e) {
      System.out.println(e.toString());
    }
  }
}

Attachment: method 2 can effectively read out the text data

I hope this article has been helpful to you in Java programming.


Related articles: