0x00 教程内容
- 写本地文件
- 读本地文件
0x01 写本地文件
1. 完整代码
a. 代码
package com.shaonaiyi.local; import java.io.File; import java.io.FileOutputStream; /** * Java实现本地写文件 */ public class WriteFile { public static void main(String[] args) throws Exception{ String path = "/Users/shaonaiyi/datas/tmp/hello.txt"; //win系统 // String path = "c:\\hello.txt"; File file = new File(path); String content = "hello,shaonaiyi.\n"; FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes()); fileOutputStream.close(); } }
0x02 读本地文件
1. 完整代码
a. 代码
package com.shaonaiyi.local; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; /** * Java实现本地读文件 */ public class ReadFile { public static void main(String[] args) throws Exception{ String path = "/Users/shaonaiyi/datas/tmp/hello.txt"; //win系统 // String path = "c:\\hello.txt"; FileInputStream fileInputStream = new FileInputStream(path); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } fileInputStream.close(); } }