Java - Java IO Introduction

1 Overview

  1. Occasionally interview will be asked to write to let you read and write files interviewer
    1. I do not understand why this stuff must be
    2. The interviewer mean, this stuff as long as the developers will have to
    3. Of course, now I'm not developed

2. Environment

  1. Language
    1. java 1.8

3. Prepare

  1. Outline
    1. basic concept
  2. Scenes
    1. A read line
    2. Reads the specified encoding
  3. File (File Object)
    1. Outline
      1. Literacy goals
    2. other
      1. To read the file, you need to be present
      2. Usually do not read and write the same file for
      3. Note directory permissions
  4. Entry
    1. Outline
      1. Is read
      2. Nested
    2. FileInputStream
      1. Outline
        1. File input stream
        2. You need to determine the input file object
    3. InputStreamReader
      1. Outline
        1. Important parameters
          1. coding
    4. BufferedReader
      1. Outline
        1. Higher cache read efficiency
        2. The method may be used to read a line readline
  5. Export
    1. Outline
      1. Write file
      2. Is nested
    2. FileOutputStream
      1. Outline
        1. File output stream
        2. You need to file object
    3. The OutputStreamWriter
      1. Overview
      1. determining the coding

    4. BufferedWriter
      1. Overview
      1. You can write cache

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

  1. ref
    1. Written in a very clear, very detailed
    https://www.cnblogs.com/ll409546297/p/7197911.html
  2. Other scenes of reading and writing
    1. You do not need to focus on reading and writing encoded
    2. Every read and write fixed character / byte scenes
    3. randomaccessfile scene
  3. IO about Java
    1. This approach sets pipeline pipeline, really hard to endure
    2. Available under python can understand the io, simply suffocating
  4. The final shutdown process
    1. bw need to wave output
      1. There may be residual content
    2. Stream closed
      1. Only the topmost object can be closed
        1. The following will shut itself down

Guess you like

Origin www.cnblogs.com/xy14/p/11109215.html