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: IO 学习小结的更多相关文章
- Java IO学习笔记:概念与原理
Java IO学习笔记:概念与原理 一.概念 Java中对文件的操作是以流的方式进行的.流是Java内存中的一组有序数据序列.Java将数据从源(文件.内存.键盘.网络)读入到内存 中,形成了 ...
- Java IO学习笔记总结
Java IO学习笔记总结 前言 前面的八篇文章详细的讲述了Java IO的操作方法,文章列表如下 基本的文件操作 字符流和字节流的操作 InputStreamReader和OutputStreamW ...
- Java IO学习笔记三
Java IO学习笔记三 在整个IO包中,实际上就是分为字节流和字符流,但是除了这两个流之外,还存在了一组字节流-字符流的转换类. OutputStreamWriter:是Writer的子类,将输出的 ...
- Java IO学习笔记二
Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...
- Java IO学习笔记一
Java IO学习笔记一 File File是文件和目录路径名的抽象表示形式,总的来说就是java创建删除文件目录的一个类库,但是作用不仅仅于此,详细见官方文档 构造函数 File(File pare ...
- java IO 学习(三)
java IO 学习(一)给了java io 进行分类,这一章学习这些类的常用方法 一.File 1.创建一个新的File的实例: /** * 创建一个新的File实例 */ File f = new ...
- java IO 流小结
java IO 流小结 java流类图结构 流的分类 按方向 输入流 输出流 按类型 字节流 字符流 结论:只要是处理纯文本数据,就优先考虑使用字符流. 除此之外都使用字节流.
- Java IO学习笔记一:为什么带Buffer的比不带Buffer的快
作者:Grey 原文地址:Java IO学习笔记一:为什么带Buffer的比不带Buffer的快 Java中为什么BufferedReader,BufferedWriter要比FileReader 和 ...
- Java IO学习笔记二:DirectByteBuffer与HeapByteBuffer
作者:Grey 原文地址:Java IO学习笔记二:DirectByteBuffer与HeapByteBuffer ByteBuffer.allocate()与ByteBuffer.allocateD ...
随机推荐
- poj2485 kruskal与prim
Kruskal: #include<iostream> #include<cstdio> #include<algorithm> using namespace s ...
- Windows 版本的iTunes 修改iPhone的备份路径
帮朋友解决修改iPhone的备份路径问题,故写篇博客整理记录一下. 所需工具 Junction工具 下载该工具然后将文件放到C:\Windows 目录下,如下图: 找到iTunes的备份路径 Wind ...
- What technical details should a programmer of a web application consider before making the site public?
What things should a programmer implementing the technical details of a web application consider bef ...
- Error: Bootstrap's JavaScript requires jQuery错误
引入bootstrap时会出现的问题:boostrap下拉菜单无效,浏览器报Uncaught Error: Bootstrap's JavaScript requires jQuery错误, 解决办法 ...
- CSS学习心得
CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与表现分离的问题 外 ...
- python非递归全排列
刚刚开始学习python,按照廖雪峰的网站看的,当前看到了函数这一节.结合数组操作,写了个非递归的全排列生成.原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位 ...
- 未解决的问题,登录163邮箱http://mail.163.com/,用xpath的方式定位密码输入框的时候,总是报找不到该元素
退出的时候出现: xpath定位方法: 注意xpath路径写的太长,如果层级全部写完定位不到,就尝试去掉一些层级
- Ember.js入门教程、博文汇总
第一章 对象模型 Ember.js 入门指南——类的定义.初始化.继承 Ember.js 入门指南——类的扩展(reopen) Ember.js 入门指南——计算属性(compute properti ...
- goroutine
Go语言从诞生到普及已经三年了,先行者大都是Web开发的背景,也有了一些普及型的书籍,可系统开发背景的人在学习这些书籍的时候,总有语焉不详的感觉,网上也有若干流传甚广的文章,可其中或多或少总有些与事实 ...
- 比较评测epoll,select,and poll 时间机制
https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE21815