IO流內容整理

IO流概述
IO流用來處理設備之間的數據傳輸
Java對數據的操作是通過流的方式
Java用於操作流的對象都在IO包中
IO流分類
按照數據流向
輸入流 讀入數據
輸出流 寫出數據
按照數據類型
字節流 可以讀寫任何類型的文件 比如音頻 視頻 文本文件
字符流 只能讀寫文本文件
什麼情況下使用哪種流呢?
如果數據所在的文件通過windows自帶的記事本打開並能讀懂裏面的內容,就用字符流。其他用字節流。
如果你什麼都不知道,就用字節流
IO流基類概述
a:字節流的抽象基類:
InputStream ,OutputStream。
b:字符流的抽象基類:
Reader , Writer。
注:由這四個類派生出來的子類名稱都是以其父類名作爲子類名的後綴。
如:InputStream的子類FileInputStream。
如:Reader的子類FileReader。
FileOutputStream的構造方法,
FileOutputStream(File file)
創建一個向指定 File 對象表示的文件中寫入數據的文件輸出流
FileOutputStream(File file, boolean append)
創建一個向指定 File 對象表示的文件中寫入數據的文件輸出流。
FileOutputStream(FileDescriptor fdObj)
創建一個向指定文件描述符處寫入數據的輸出文件流,該文件描述符表示一個到文件 系統中的某個實際文件的現有連接。
FileOutputStream(String name)
創建一個向具有指定名稱的文件中寫入數據的輸出文件流。
FileOutputStream(String name, boolean append)
創建一個向具有指定 name 的文件中寫入數據的輸出文件流。
使用具體子類FileOutputStream
Io流的分類:

  • (1): 按照流向進行劃分
    輸入流
    輸出流
    • (2): 按照操作的數據類型進行劃分
    • 字節流
    • 字節輸入流 InputStream 讀
    • 字節輸出流 OutputStream 寫
    • 字符流
    • 字符輸入流 Reader 讀
    • 字符輸出流 Writer 寫
      注意事項:
      創建字節輸出流對象了做了幾件事情?
      a:調用系統資源創建a.txt文件
      b:創建了一個fos對象
      c:把fos對象指向這個文件
      爲什麼一定要close()?
      a: 通知系統釋放關於管理a.txt文件的資源
      b: 讓Io流對象變成垃圾,等待垃圾回收器對其回收
      FileInputStream
      構造方法:FileInputStream(File file)
      通過打開一個到實際文件的連接來創建一個 FileInputStream,該文件通過文件系統中 的 File 對象 file 指定。
      FileInputStream(FileDescriptor fdObj)
      通過使用文件描述符 fdObj 創建一個 FileInputStream,該文件描述符表示到文件系統 中某個實際文件的現有連接。
      FileInputStream(String name)
      通過打開一個到實際文件的連接來創建一個 FileInputStream,該文件通過文件系統中 的路徑名 name 指定。
      BufferedOutputStream
      A:緩衝思想
      字節流一次讀寫一個數組的速度明顯比一次讀寫一個字節的速度快很多,
      這是加入了數組這樣的緩衝區效果,java本身在設計的時候,
      也考慮到了這樣的設計思想(裝飾設計模式後面講解),所以提供了字節緩衝區流
      B:BufferedOutputStream的構造方法
      BufferedOutputStream(OutputStream out)
      創建一個新的緩衝輸出流,以將數據寫入指定的底層輸出流。
      BufferedOutputStream(OutputStream out, int size)
      創建一個新的緩衝輸出流,以將具有指定緩衝區大小的數據寫入指定的底層輸出流。
      BufferedInputStream
      A:BufferedInputStream的構造方法
      BufferedInputStream(InputStream in)
      創建一個 BufferedInputStream 並保存其參數,即輸入流 in,以便將來使用。
      BufferedInputStream(InputStream in, int size)
      創建具有指定緩衝區大小的 BufferedInputStream 並保存其參數,即輸入流 in,以便 將來使用。
      具體案例演示
      基本字節流一次讀寫一個字節
      public class t7 {
      public static void main(String[] args) throws IOException {
      FileInputStream inputStream = new FileInputStream("Student.txt");
      FileOutputStream outputStream = new FileOutputStream("sss.txt");
      int num=0;
      while ((num=inputStream.read())!=-1){
      outputStream.write(num);
      }
      inputStream.close();
      outputStream.close();
      }
      }
      基本字節流一次讀寫一個字節數組
      public class t8 {
      public static void main(String[] args) throws IOException {
      FileInputStream inputStream = new FileInputStream("Student.txt");
      FileOutputStream outputStream = new FileOutputStream("sss.txt");
      int num=0;
      byte[] bytes = new byte[1024 1024];
      while ((num=inputStream.read(bytes))!=-1){
      outputStream.write(bytes,0,num);
      }
      inputStream.close();
      outputStream.close();
      }
      }
      高效字節流一次讀寫一個字節
      public class t9 {
      public static void main(String[] args) throws IOException {
      BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("Student.txt"));
      BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("sss.txt"));
      int num=0;
      while ((num=inputStream.read())!=-1){
      outputStream.write(num);
      }
      inputStream.close();
      outputStream.close();
      }
      }
      高效字節流一次讀寫一個字節數組
      public class t10 {
      public static void main(String[] args)throws IOException {
      BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("Student.txt"));
      BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("sss.txt"));
      int num=0;
      byte[] bytes = new byte[1024
      1024];
      while ((num=inputStream.read(bytes))!=-1){
      outputStream.write(bytes,0,num);
      }
      inputStream.close();
      outputStream.close();
      }
      }

字符流出現的原因:由於字節流操作中文不是特別方便,所以,java就提供了字符流。

字符流: 字符流 = 字節流 + 編碼表
編碼: 就是把字符串轉換成字節數組

  • 把一個字符串轉換成一個字節數組
  • public byte[] getBytes();使用平臺的默認字符集將此 String編碼爲 byte 序列,並將結果存儲到一個新的 byte 數組中。
  • public byte[] getBytes(String charsetName) 使用指定的字符集將此 String 編碼爲 byte 序列,並將結果存儲到一個新的 byte 數組中。
  • 解碼: 把字節數組轉換成字符串
  • public String(byte[] bytes): 通過使用平臺的默認字符集解碼指定的 byte 數組,構造一個新的 String。
  • public String(byte[] bytes, String charsetName) 通過使用指定的 charset 解碼指定的 byte 數組,構造一個新的 String。
  • 使用什麼字符集進行編碼,那麼就是使用什麼字符集進行解碼
  • 老地方 ----- 十進制 ---- 二進制 ---- 發出去
  • 接收 ---- 二進制 ---- 十進制 --- 老地方
    編碼:把看得懂的變成看不懂的: String -- byte[]
    解碼:把看不懂的變成看得懂的: byte[] -- String
    OutputStreamWriter
    OutputStreamWriter的構造方法
    OutputStreamWriter(OutputStream out):根據默認編碼(GBK)把字節流的數據轉換爲字符流
    OutputStreamWriter(OutputStream out,String charsetName):根據指定編碼把字節流數據轉換爲字符流
    方法概述
    public void write(int c) 寫一個字符
    public void write(char[] cbuf) 寫一個字符數組
    public void write(char[] cbuf,int off,int len) 寫一個字符數組的 一部分
    public void write(String str) 寫一個字符串
    public void write(String str,int off,int len) 寫一個字符串的一部分
    InputStreamReader
    InputStreamReader的構造方法
    InputStreamReader(InputStream is):用默認的編碼(GBK)讀取數據
    InputStreamReader(InputStream is,String charsetName):用指定的編碼讀取數據
    方法概述
    public int read() 一次讀取一個字符
    public int read(char[] cbuf) 一次讀取一個字符數組 如果沒有讀到 返回-1
    FileWriter和FileReader
    FileReader和FileWriter的出現
    轉換流的名字比較長,而我們常見的操作都是按照本地默認編碼實現的,
    所以,爲了簡化我們的書寫,轉換流提供了對應的子類。
    FileWriter
    FileReader
    字符流便捷類: 因爲轉換流的名字太長了,並且在一般情況下我們不需要制定字符集,
    於是java就給我們提供轉換流對應的便捷類
    轉換流 -------------------------- 便捷類
    OutputStreamWriter ------- FileWriter
    InputStreamReader ------- FileReader
    字符緩衝流的特殊功能
    BufferedWriter: public void newLine():根據系統來決定換行符 具有系統兼容性的換行符
    BufferedReader: public String readLine():一次讀取一行數據 是以換行符爲標記的 讀到換行符就換行 沒讀到數據返回null
    包含該行內容的字符串,不包含任何行終止符,如果已到達流末尾,則返回 null
    字符流複製文件
    基本的流一次一個字符
    public class t1 {
    public static void main(String[] args) throws IOException {
    InputStreamReader reader = new InputStreamReader(new FileInputStream("Student.txt"));
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("SSS.txt"));
    int len=0;
    while ((len=reader.read())!=-1){
    writer.write(len);
    writer.flush();
    }
    writer.close();
    reader.close();
    }
    }
    基本的流一次一個字符數組
    public class t2 {
    public static void main(String[] args) throws IOException {
    InputStreamReader reader = new InputStreamReader(new FileInputStream("Student.txt"));
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("sss.txt"));
    int len=0;
    char[] chars = new char[1024 * 1024];
    while ((len=reader.read(chars))!=-1){
    writer.write(chars,0,len);
    writer.flush();
    }
    reader.close();
    writer.close();
    }
    }
    高效的流一次一個字符
    public class t3 {
    public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("Student.txt"));
    BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt"));
    int len=0;
    while ((len=reader.read())!=-1){
    writer.write(len);
    writer.flush();
    }
    reader.close();
    writer.close();
    }
    }

高效的流一次一個字符數組
public class t4 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("Student.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt"));
int len=0;
char[] chars = new char[1024 * 1024];
while ((len=reader.read(chars))!=-1){
writer.write(chars,0,len);
writer.flush();
}
reader.close();
writer.close();
}
}
高效的流一次一行字符串
public class t5 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("Student.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt"));
String len=null;
while ((len=reader.readLine())!=null){
writer.write(len);
writer.newLine();
writer.flush();
}
reader.close();
writer.close();

}

}
IO流(鍵盤錄入學生信息按照總分排序並寫入文本文件)
public class Student implements Comparable<Student>{
private String name;
private int chinese;
private int math;
private int English;
private int all;

public Student() {
}

public Student(String name, int chinese, int math, int english, int all) {
    this.name = name;
    this.chinese = chinese;
    this.math = math;
    English = english;
    this.all = all;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getChinese() {
    return chinese;
}

public void setChinese(int chinese) {
    this.chinese = chinese;
}

public int getMath() {
    return math;
}

public void setMath(int math) {
    this.math = math;
}

public int getEnglish() {
    return English;
}

public void setEnglish(int english) {
    English = english;
}

public int getAll() {
    return all;
}

public void setAll(int all) {
    this.all = all;
}

@Override
public String toString() {
    return "學生: " + "姓名~┏" + name + '┒' +
            ", 語文~" + chinese +
            ", 數學~" + math +
            ", 英語~" + English +
            ",總分~" + all ;
}

@Override
public int compareTo(Student student) {
    int num=-(this.all-student.all);
    int num2=num==0?this.name.compareTo(student.name):num;
    return num2;
}

}

public class text3 {
/ 3.A:案例演示: 需求:鍵盤錄入3個學生信息(
姓名,語文成績(chineseScore),數學成績(mathScore),英語成績(englishScore)),
/
public static void main(String[] args) throws IOException {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Student.txt"));
Scanner scanner = new Scanner(System.in);
TreeSet<Student> students = new TreeSet<>();
for (int i = 0, n = 1; i < 3; i++, n++) {
Student student = new Student();
System.out.println("請輸入第" + n + "個學生的姓名");
String name = scanner.nextLine();
student.setName(name);
System.out.println("請輸入第" + n + "個學生的語文成績");
int chinese = scanner.nextInt();
student.setChinese(chinese);
System.out.println("請輸入第" + n + "個學生的數學成績");
int math = scanner.nextInt();
student.setMath(math);
System.out.println("請輸入第" + n + "個學生的英語成績");
int English = scanner.nextInt();
student.setEnglish(English);
scanner = new Scanner(System.in);
int num = chinese + math + English;
student.setAll(num);
students.add(student);
}

    for (Student s : students) {
        String s1 = String.valueOf(s);
        out.write(s1.getBytes());
        out.write("\n\r".getBytes());
    }
    out.close();

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章