源:

  键盘 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. 使用Xmanager远程访问Redhat Enterprise Linux 6.1

    使用Xmanager远程访问Redhat Enterprise Linux 6.1   在Linux服务器开启gdm 配置防火墙 配置selinux 使用xmanager连接linux服务器 在Lin ...

  2. jQuery学习笔记(五)jQuery中的表单

    目录 单行文本框的应用 表单验证 上次我们说完jQuery中的动画之后,我们再来看一种jQuery在Web网页应用最为广泛的一种形式,这就是jQuery对表单的操作,通过jQuery对表单的操作,可以 ...

  3. 今天学的是 HTML基本元素、基本语法元素特点等,就发图片吧。

    现在我们新手用的软件是:Adobe Dreamweaver CS6 按照下面格式来改,以后点HTML5直接就改过来了. 可以敲敲这些代码,大家一起学习. <!doctype html>&l ...

  4. Linux下查看/管理当前登录用户及用户操作历史记录

    转载自: http://www.cnblogs.com/gaojun/archive/2013/10/24/3385885.html 一.查看及管理当前登录用户 1.使用w命令查看登录用户正在使用的进 ...

  5. Latex图片显示问题(1)

    用latex编译后,若用dvipdf生成pdf文件,则其中有个eps图的左侧会显示不完全:若是用dvips--pspdf生成pdf文件,图像显示没问题. 这种情况的问题出在,加载 graphicx 宏 ...

  6. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

  7. Node聊天程序实例06:server.js

    作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. server ...

  8. codeigniter nginx配置

    转载:http://www.nginx.cn/1134.html server{ listen 80; server_name www.ci.oa.com; access_log /usr/local ...

  9. idea中配置eslint 静态代码检查

    配置: 1,安装依赖 sudo tnpm install eslint -g sudo tnpm install eslint-plugin-import -g sudo tnpm install e ...

  10. C3P0的两种使用方法

    方法一: package   C3P0; import   java.sql.Connection; import   java.sql.SQLException; import   java.bea ...