package com.zving.demo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class FilePrint { public static void main(String[] args) { outPutInfo(); inPutInfo(); } //文件写入 public static void outPutInfo() { try { FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Clover\\Desktop\\abc.txt")); OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8"); for (int i = 1; i < 10; i++) { osw.write("第 " + i + " 次写入数据" + "\r\n"); } osw.flush(); osw.close(); } catch (Exception e) { e.printStackTrace(); } } //文件读取 public static void inPutInfo() { String fileText = ""; try { FileInputStream fis = new FileInputStream("C:\\Users\\Clover\\Desktop\\abc.txt"); InputStreamReader isr = new InputStreamReader(fis, "utf-8"); @SuppressWarnings("resource") BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { fileText += line; fileText += "\r\n"; } System.out.println(fileText); } catch (Exception e) { e.printStackTrace(); } } }
O(∩_∩)O开心就好O(∩_∩)O