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",将学生成绩按照总分排列,并且储存在本地文件中.

 package Day20;
import java.io.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet; public class StudentInfo { public static void main(String[] args) {
OutputStream out = null;
try{
out = new FileOutputStream("exam.txt");
//Set<Student> stus = StudentTool.getStudents();
Comparator<Student> cmp = Collections.reverseOrder();
Set<Student> stus = StudentTool.getStudents(cmp);
StudentTool.write2File(stus, out);
}
catch(IOException e){
throw new RuntimeException("写入异常");
}
finally{
try{
if(out!=null)
out.close();
}
catch(IOException e){
throw new RuntimeException("关闭写入动作遇到错误");
}
}
} } class Student implements Comparable
{
private String name;
private double math,cn,en,sum; Student(String name, double math, double cn, double en){
this.name = name;
this.math = math;
this.cn = cn;
this.en = en;
sum = math + cn + en; }
public String getName(){
return name;
}
public double getSum(){
return sum;
}
@Override
public int compareTo(Object o){
if(!(o instanceof Student))
throw new ClassCastException("非Student类对象");
Student s = (Student)o;
if(this.sum == s.sum)
return this.name.compareTo(s.name);
else if((this.sum-s.sum)>0)
return -1;
else if((this.sum-s.sum)<0)
return 1;
return 0;
}
@Override
public boolean equals(Object o){
if(!(o instanceof Student))
throw new ClassCastException("非Student类对象");
Student s = (Student)o;
return this.name.equals(s.name) && this.math==s.math && this.cn == s.cn && this.en == s.en;
}
@Override
public String toString(){
return "student ["+name + "]" + " ,sum = " + sum;
}
} class StudentTool
{
public static Set<Student> getStudents()throws IOException{
return getStudents(null);
}
public static Set<Student> getStudents(Comparator cmp)throws IOException{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
Set<Student> stus = null;
if(cmp==null)
stus = new TreeSet<Student>();
else
stus = new TreeSet<Student>(cmp);
while((line=bufr.readLine())!=null){
if("done".equals(line))
break;
String[] info = line.split(",");
Student stu = new Student(info[0],Double.parseDouble(info[1]),
Double.parseDouble(info[2]),
Double.parseDouble(info[3]));
stus.add(stu);
}
bufr.close();
return stus;
} public static void write2File(Set<Student> stus,OutputStream out)throws IOException{
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(out)); for(Student s: stus){
bufw.write(s.toString());
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}
上一篇:java中会存在内存泄漏吗,请简单描述。


下一篇:django 加载静态文件(图片,js,css)