Java class sharing that writes and reads the contents of a file in the specified code

  • 2020-04-01 02:55:48
  • OfStack

You can specify encodings such as utf-8 to write and read files. If the file encoding is unknown, the method can first get the file encoding and then specify the correct encoding to read, otherwise the file will be garble problems.

Please refer to: (link: #)


package com.zuidaima.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ReadWriteFileWithEncode {
 public static void write(String path, String content, String encoding)
   throws IOException {
  File file = new File(path);
  file.delete();
  file.createNewFile();
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
    new FileOutputStream(file), encoding));
  writer.write(content);
  writer.close();
 }
 public static String read(String path, String encoding) throws IOException {
  String content = "";
  File file = new File(path);
  BufferedReader reader = new BufferedReader(new InputStreamReader(
    new FileInputStream(file), encoding));
  String line = null;
  while ((line = reader.readLine()) != null) {
   content += line + "n";
  }
  reader.close();
  return content;
 }
 public static void main(String[] args) throws IOException {
  String content = " Chinese content ";
  String path = "c:/test.txt";
  String encoding = "utf-8";
  ReadWriteFileWithEncode.write(path, content, encoding);
  System.out.println(ReadWriteFileWithEncode.read(path, encoding));
 }
}


Related articles: