1 Overview
- Occasionally interview will be asked to write to let you read and write files interviewer
- I do not understand why this stuff must be
- The interviewer mean, this stuff as long as the developers will have to
- Of course, now I'm not developed
2. Environment
- Language
- java 1.8
3. Prepare
- Outline
- basic concept
- Scenes
- A read line
- Reads the specified encoding
- File (File Object)
- Outline
- Literacy goals
- other
- To read the file, you need to be present
- Usually do not read and write the same file for
- Note directory permissions
- Outline
- Entry
- Outline
- Is read
- Nested
- FileInputStream
- Outline
- File input stream
- You need to determine the input file object
- Outline
- InputStreamReader
- Outline
- Important parameters
- coding
- Important parameters
- Outline
- BufferedReader
- Outline
- Higher cache read efficiency
- The method may be used to read a line readline
- Outline
- Outline
- Export
- Outline
- Write file
- Is nested
- FileOutputStream
- Outline
- File output stream
- You need to file object
- Outline
The OutputStreamWriter
1. Overview
1. determining the codingBufferedWriter
1. Overview
1. You can write cache
- Outline
4. Sample Code
// 注意: 这里只给出思路, 文件类, 输入输出流可能会遇到异常, 需要 try catch 或者 throws, 并且这些东西, 最好在 finally 里清空和关闭
// 声明变量: 读
// inputPath 是 String 类型的变量, 记录着 输入文件 在文件系统里的位置
File inputFile = new File(inputPath);
FileInputStream fis = new FileInputStream(inputFile);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(fis);
// 下面是简化版本
// BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile),"UTF-8"));
// 如果不需要调整编码, 也可以这么写
// BufferedReader br = new BufferedReader(new FileReader(inputFile)));
// 声明变量: 写
File outputFile = new File(outputPath);
FileOutputStream fos = new FileOutputStream(outputFile);
OutputStreamWriter osr = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(fos);
// 下面是简化版本
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile),"UTF-8"));
// 如果不需要调整编码, 也可以这么写
// BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile)));
//读取数据
//循环取出数据
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
//写入相关文件
bw.write(str);
bw.newLine();
}
//清楚缓存
bw.flush();
//关闭流
br.close();
bw.close();
4. Other
- ref
- Written in a very clear, very detailed
https://www.cnblogs.com/ll409546297/p/7197911.html
- Other scenes of reading and writing
- You do not need to focus on reading and writing encoded
- Every read and write fixed character / byte scenes
- randomaccessfile scene
- IO about Java
- This approach sets pipeline pipeline, really hard to endure
- Available under python can understand the io, simply suffocating
- The final shutdown process
- bw need to wave output
- There may be residual content
- Stream closed
- Only the topmost object can be closed
- The following will shut itself down
- Only the topmost object can be closed
- bw need to wave output