前言
- 通过"Java"实现文本文件的读写操作,快速上手;
实操
【前提】
- 使用"Windows 11" 系统通过"IntelliJ IDEA" 软件完成;
【目录】
- “方式1”:文本文件的读取操作;
- “方式2”:文本文件的读取操作;
- “方式3”:文本文件的写入操作;
- “方式4”:文本文件的写入操作;
- “方式5”:文本文件的写入操作;
【方式1】
代码
【类名A】
TextInputDemoA
【代码A】
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; public class TextInputDemoA { public static void main(String[] args) throws IOException { // 参数:要读取的目标文本文件的路径 FileInputStream fis = new FileInputStream("test/InputDemo.txt"); InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } }
结果
【方式2】
代码
【类名A】
TextInputDemoB
【代码A】
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; public class TextInputDemoB { public static void main(String[] args) throws IOException { // 参数:要读取的目标文本文件的路径 File file = new File("test/InputDemo.txt"); FileInputStream fis = new FileInputStream(file); long len = file.length(); byte[] data = new byte[(int)len]; fis.read(data); String line = new String(data, StandardCharsets.UTF_8); System.out.println(line); fis.close(); } }
结果
【方式3】
代码
【类名A】
TextOutputDemoA
【代码A】
import java.io.FileNotFoundException; import java.io.PrintWriter; public class TextOutputDemoA { public static void main(String[] args) throws FileNotFoundException { // 参数:要写入的目标文本文件的路径 PrintWriter pw = new PrintWriter("test/OutputDemoA.txt"); pw.println("这是第一句;"); pw.println("这是第二句;"); pw.println("这是第三句;"); System.out.println("写出完毕!"); pw.close(); } }
结果
【方式4】
代码
【类名A】
TextOutputDemoB
【代码A】
import java.io.*; import java.nio.charset.StandardCharsets; public class TextOutputDemoB { public static void main(String[] args) throws FileNotFoundException { // 参数1:要写入的目标文本文件的路径;参数2:写入方式(追加还是覆盖); FileOutputStream fos = new FileOutputStream("test/OutputDemoB.txt", true); //参数2:在转换流上加上字符集,可以按照指定字符集写出。 OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8); BufferedWriter bw = new BufferedWriter(osw); // 参数2:是否开启自动行刷新功能; PrintWriter pw = new PrintWriter(bw, true); pw.println("这是第一句;"); pw.println("这是第二句;"); pw.println("这是第三句;"); System.out.println("写出完毕!"); pw.close(); } }
结果
【方式5】
代码
【类名A】
TextOutputDemoC
【代码A】
import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; public class TextOutputDemoC { public static void main(String[] args) throws IOException { // 参数1:要写入的目标文本文件的路径;参数2:写入方式(追加还是覆盖); FileOutputStream fos = new FileOutputStream("test/OutputDemoC.txt", true); String line = "这是第一句;"; byte[] data = line.getBytes(StandardCharsets.UTF_8); fos.write(data); line = "这是第二句;"; data = line.getBytes(StandardCharsets.UTF_8); fos.write(data); line = "这是第三句;"; data = line.getBytes(StandardCharsets.UTF_8); fos.write(data); System.out.println("写出完毕!"); fos.close(); } }
结果
后记
- 和此文相关的所有内容,需要的请下载;