Java: IO 学习小结

源:

  键盘 System.in  硬盘 FileStream  内存 ArrayStream

目的:

  控制台 System.out  硬盘 FileStream  内存 ArrayStream

处理大文件或者多线程下载上传  

  RandomAccessFile 或者内存映射文件

方便对对象的永久化存储和调用

  ObjectStream

方便操作打印流

  PrintWriter 和 PrintStream

序列流,对多个流进行合并

  SequenceInpuntStream

管道流,输入和输出可以直接连接,通过结合线程使用

  PipedInputStream PipedOutputStream

方便操作基本数据类型

  DataInputStream DataOutputStream

方便操作字节数组

  ByteArrayInputStream  ByteArrayOutputStream  

方便操作字符数组

  CharArrayReader CharArrayWriter

方便操作字符串

  StringReader StringWriter


字符编码:

  字符流的出现为了方便操作字符,更重要的是加入了字符转换.

  字符转换通过转换流来完成

    InputStreamReader

    OutputStreamWriter

  在两个对象进行构造时可加入字符集 

编码;

  字符串变成字节数组

  String --> byte[]     str.getBytes()(按照平台默认的字符集)    str.getBytes(String charsetName)(按照指定字符集编码)

解码:

  字节数组变字符串

  byte[] --> String  new String(byte[])   new String(byte[], charsetName)(按照指定的字符编码)

常见的字符集:

  ASCII:美国标准信息交换码

    用一个字节的7位就可以表示

  IOS8859-1:拉丁码表欧洲码表

    用一个字节的8位表示

  GB2312:中国的中文编码表

  

  GBK:中文编码表升级,融合了更多的中文字符

  Unicode:国际标准编码,融合了多种文字

    所有字符都用两个字节表示,java语言就是用的unicode

  UTF-8:用3个字节来表示一个汉字


练习题:

  在屏幕上输入学生姓名,数学成绩,语文成绩,英语成绩,形如"owen,99,99,99",将学生成绩按照总分排列,并且储存在本地文件中.

  1 package Day20;
  2 import java.io.*;
  3 import java.util.Collections;
  4 import java.util.Comparator;
  5 import java.util.Set;
  6 import java.util.TreeSet;
  7 
  8 public class StudentInfo {
  9 
 10     public static void main(String[] args) {
 11         OutputStream out = null;
 12         try{
 13             out = new FileOutputStream("exam.txt");
 14             //Set<Student> stus = StudentTool.getStudents();
 15             Comparator<Student> cmp = Collections.reverseOrder();
 16             Set<Student> stus = StudentTool.getStudents(cmp);
 17             StudentTool.write2File(stus, out);
 18         }
 19         catch(IOException e){
 20             throw new RuntimeException("写入异常");
 21         }
 22         finally{
 23             try{
 24                 if(out!=null)
 25                     out.close();
 26             }
 27             catch(IOException e){
 28                 throw new RuntimeException("关闭写入动作遇到错误");
 29             }
 30         }
 31     }
 32 
 33 }
 34 
 35 class Student implements Comparable
 36 {
 37     private String name;
 38     private double math,cn,en,sum;
 39     
 40     Student(String name, double math, double cn, double en){
 41         this.name = name;
 42         this.math = math;
 43         this.cn = cn;
 44         this.en = en;
 45         sum = math + cn + en;
 46 
 47     }
 48     public String getName(){
 49         return name;
 50     }
 51     public double getSum(){
 52         return sum;
 53     }
 54     @Override
 55     public int compareTo(Object o){
 56         if(!(o instanceof Student))
 57             throw new ClassCastException("非Student类对象");
 58         Student s = (Student)o;
 59         if(this.sum == s.sum)
 60             return this.name.compareTo(s.name);
 61         else if((this.sum-s.sum)>0)
 62             return -1;
 63         else if((this.sum-s.sum)<0)
 64             return 1;
 65         return 0;
 66     }
 67     @Override
 68     public boolean equals(Object o){
 69         if(!(o instanceof Student))
 70             throw new ClassCastException("非Student类对象");
 71         Student s = (Student)o;
 72         return this.name.equals(s.name) && this.math==s.math && this.cn == s.cn && this.en == s.en;
 73     }
 74     @Override
 75     public String toString(){
 76         return "student ["+name + "]" + " ,sum = " + sum;
 77     }
 78 }
 79 
 80 class StudentTool
 81 {
 82     public static Set<Student> getStudents()throws IOException{
 83         return getStudents(null);
 84     }
 85     public static Set<Student> getStudents(Comparator cmp)throws IOException{
 86         BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
 87         String line = null;
 88         Set<Student> stus = null;
 89         if(cmp==null)
 90             stus = new TreeSet<Student>();
 91         else
 92             stus = new TreeSet<Student>(cmp);
 93         while((line=bufr.readLine())!=null){
 94             if("done".equals(line))
 95                 break;
 96             String[] info = line.split(",");
 97             Student stu = new Student(info[0],Double.parseDouble(info[1]),
 98                     Double.parseDouble(info[2]),
 99                     Double.parseDouble(info[3]));
100             stus.add(stu);
101         }
102         bufr.close();
103         return stus;
104     }
105     
106     public static void write2File(Set<Student> stus,OutputStream out)throws IOException{
107         BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(out));
108         
109         for(Student s: stus){
110             bufw.write(s.toString());
111             bufw.newLine();
112             bufw.flush();
113         }
114         bufw.close();
115     }
116 }
原文地址:https://www.cnblogs.com/siyingcheng/p/4452188.html