源:

  键盘 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: IO 学习小结的更多相关文章

  1. Java IO学习笔记:概念与原理

    Java IO学习笔记:概念与原理   一.概念   Java中对文件的操作是以流的方式进行的.流是Java内存中的一组有序数据序列.Java将数据从源(文件.内存.键盘.网络)读入到内存 中,形成了 ...

  2. Java IO学习笔记总结

    Java IO学习笔记总结 前言 前面的八篇文章详细的讲述了Java IO的操作方法,文章列表如下 基本的文件操作 字符流和字节流的操作 InputStreamReader和OutputStreamW ...

  3. Java IO学习笔记三

    Java IO学习笔记三 在整个IO包中,实际上就是分为字节流和字符流,但是除了这两个流之外,还存在了一组字节流-字符流的转换类. OutputStreamWriter:是Writer的子类,将输出的 ...

  4. Java IO学习笔记二

    Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...

  5. Java IO学习笔记一

    Java IO学习笔记一 File File是文件和目录路径名的抽象表示形式,总的来说就是java创建删除文件目录的一个类库,但是作用不仅仅于此,详细见官方文档 构造函数 File(File pare ...

  6. java IO 学习(三)

    java IO 学习(一)给了java io 进行分类,这一章学习这些类的常用方法 一.File 1.创建一个新的File的实例: /** * 创建一个新的File实例 */ File f = new ...

  7. java IO 流小结

    java IO 流小结 java流类图结构 流的分类 按方向 输入流 输出流 按类型 字节流 字符流 结论:只要是处理纯文本数据,就优先考虑使用字符流. 除此之外都使用字节流.

  8. Java IO学习笔记一:为什么带Buffer的比不带Buffer的快

    作者:Grey 原文地址:Java IO学习笔记一:为什么带Buffer的比不带Buffer的快 Java中为什么BufferedReader,BufferedWriter要比FileReader 和 ...

  9. Java IO学习笔记二:DirectByteBuffer与HeapByteBuffer

    作者:Grey 原文地址:Java IO学习笔记二:DirectByteBuffer与HeapByteBuffer ByteBuffer.allocate()与ByteBuffer.allocateD ...

随机推荐

  1. JS调用JCEF方法

    坐下写这篇文章的时候,内心还是有一点点小激动的,折腾了一个多星期,踩了一个又一个的坑,终于找到一条可以走通的路,内心的喜悦相信经历过的人都会明白~~~~~今儿个老百姓啊,真呀个真高兴啊,哈哈,好了,废 ...

  2. python之禅

    >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is ...

  3. SQL的多表连接查询

    SQL的多表连接查询 多表连接查询具有两种规范,SQL92和SQL99规范. SQL92规范支持下列多表连接查询: (1)等值连接: (2)非等值连接: (3)外连接: (4)广义笛卡尔积: SQL9 ...

  4. .NET蓝牙开源库:32feet.NET

    在用C#调用蓝牙编程一文中我留个小悬念就是:InTheHand.Net.Personal.dll是怎么来的?这篇文章来解答这个问题,InTheHand.Net.Personal.dll就是来源于今天要 ...

  5. QLPreViewController的初步实用

    前一阵项目需要添加一个文档文件的查看功能,于是就各种找资料,一开始想实用webView,然而webView有的格式不支持,而且占内存太大了.找着找着就找到QLPreViewController.用了一 ...

  6. R语言拆分字符串

    R语言拆分字符串 aaa<-"aa;bb;cc"ccc<-strsplit(aaa,split=";") bbb<- unlist(strsp ...

  7. div居中方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Eclipse 实现关键字自动补全功能

    一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选项是默认关闭的, ...

  10. windows下如何安装和启动MySQL

    1.下载,解压到自己喜欢的目录 2.配置环境变量.MYSQL_HOME,值为mysql的根目录:在path中添加%MYSQL_HOME%/bin目录. 3.向windows注册mysql服务.必须用管 ...