黑马程序员——第十二篇:字节流、字符流
来源:互联网 发布:淘宝哪家赌石好 编辑:程序博客网 时间:2024/05/16 11:50
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a
href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
1、字节流进行文件复制
1:字节输入流,读取数据源字节
FileInputStream 读取1个字节
2:字节输出流,写入数据目的字节
FileOutputStream写1个字节
2. FileInputStream读取文件
int read() 每次读取1个字节,返回int值
文件结尾返回-1
read()方法重载形式
1:int read(byte[] b)
方法中传递字节数组,流对象,将读取到的字节存储数组中
实现缓冲区作用,提高效率
int 返回值作用
数组里面存储的又是什么
InputStream类,读取字节数组方式
int fis.read(bytes) 返回每次读取,存储数组的有效个数
OutputStream类,写字节数组
void write(byte[] b, int off, int len)
写字节数组,从0开始,写几个,有效个数
2:程序Demo:用字节数组复制文件
publicclass CopyFile_1 {
publicstaticvoid main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("E:\\t.jpg");
fos = new FileOutputStream("d:\\t.jpg");
//定义字节数组,长度1024整数倍
byte[] bytes =newbyte[1024];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
//fos流写字节数组
fos.write(bytes, 0, len);
}
} catch (IOException ex) {
ex.printStackTrace();
thrownew RuntimeException("复制失败");
} finally {
try {
if (fis !=null)
fis.close();
} catch (IOException ex) {
thrownew RuntimeException("释放资源失败");
} finally {
try {
if (fos !=null)
fos.close();
} catch (IOException ex) {
thrownew RuntimeException("释放资源失败");
}
}
}
}
}
3. IO中的缓冲流对象
读写数组,速度比读写单个字节快
JDK设计的时候,也考虑到效率问题,因此开发好了缓冲区流对象,
提高效率
1. BufferedOutputStream
字节输出流缓冲区对象,目的提高字节输出流的写入效率
BufferedOutputStream继承OutputStream
写的方法write,已经不需要学习了
构造方法:
BufferedOutputStream(OutputStream out)
传递的是OutputStream任意子类对象,可以传递任何字节输出流
为什么要传递字节输出流,BufferedOutputStream提高效率
提高谁的效率,构造方法中,传递的是谁,就提高谁
BufferedOutputStream(new FileOutputStream())
原理,自身有字节数组,我们写的数据,直接写在他的数组中
一次性写入文件中
2. BufferedInputStream
字节输入流对象,目的提高字节输入流的读取效率
BufferedInputStream继承InputStream
读取方法,不需要学习,读取单个字节,读取字节数组
构造方法:
BufferedInputStream(InputStream in)
传递的是InputStream任意子类对象,可以传递任意的字节输入流
传递的是谁,就对谁提高效率
new BufferedInputStream(new FileInputStream())
3.程序Demo:缓冲字节流复制文件
publicclass CopyFile_2 {
publicstaticvoid main(String[] args) {
//创建2个缓冲区流的变量,=null
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//创建字节流缓冲区对象,传递字节流
bis = new BufferedInputStream(newFileInputStream("e:\\t.jpg"));
//创建字节输出流缓冲区对象,传递字节输出流
bos = new BufferedOutputStream(new FileOutputStream("d:\\t.jpg"));
//读写单个字节
int len = 0;
while ((len = bis.read()) != -1) {
bos.write(len);
}
} catch (IOException ex) {
thrownew RuntimeException("文件复制失败");
} finally {
try {
if (bis !=null)
bis.close();
} catch (IOException ex) {
thrownew RuntimeException("释放资源失败");
} finally {
try {
if (bos !=null)
bos.close();
} catch (IOException ex) {
thrownew RuntimeException("释放资源失败");
}
}
}
}
}
4、字符编码
1. 编码表的由来
计算机电信号,变成数字信号,01
让算计机直接识别人类文件
美国,制定编码表,让人类的文字对应数字
97 字母 a
98 字母 b
47 数字 0
ASCII编码表:数字,字母,符号
2. 简体中文编码表
数字对应汉字
两个数字,两个字节对应1个汉字,汉字采用负数表示
汉字两个字节,第一个字节肯定是负数,第二个字节可能是正数,可能是负数
出现第一张简体中文编码表 GB2312, Windows 95-98 Windows3.X
汉字存储量少,4000个汉字
编码扩容,汉字数量20000个, GBK 2000-10
Windows中文版系统,默认编码表都是GBK
再一次扩容, GB18030
3. Big5
大五码,繁体中文编码表,香港,澳门,台湾
4. ISO8859-1编码表
拉丁文编码表
编码表中没有中文
JavaWeb,程序运行在互联网的世界
网络服务器软件, Apache Tomcat(拉丁文)
5. Unicode万国码
国际标准化组织,让一张码表融合全球所有语言
Unicode采用2个字节存储1个汉字,
Java中的char,就是Unicode
6. UTF-8编码表
万国码,汉字采用3个字节存储
中,GBK,UTF-8
开发中文版程序的困难
浏览器混乱:
IE FireFox Chrome Safari Opera
5.字符串的编码和解码
1. 字符串的编码
将看的懂的,变成看不懂的
字符串转成字节数组
2. 字符串的解码
将看不懂,变成看的懂的
字节数组变成字符串
编码: String类方法getBytes()
解码: String类构造方法
6、字符流
读写纯文本文件,中文也行
1. OutputStreamWriter 转换流
OutputStreamWriter 继承Writer
写的方法 write字符,字符数组,字符数组一个部分,写字符串
作用:字符流向字节的桥梁,字符转成字节
构造方法:
OutputStreamWriter(OutputStream out)
传递任意的字节输出流
OutputStreamWriter流,将数据变成字节,写入传递的字节流中
new OutputStreamWriter(new FileOutputStream())
OutputStreamWriter(OutputStream out,String 编码表名)
发现现象:字节流,还是字符流,写数据方法都是write
区别:字节流写的是byte
字符流写的是char, String
程序Demo:(字符流写出数据):
分析步骤:
/*
* OutputStreamWriter写数据
* 1.创建转换流对象,传递字节输出流
* 2.调用转换流对象方法write写数据字符,字符串,字符数组
*
* 3.刷新,所有的字符流输出的,必须刷新flush
*
* 4.关闭资源
* close() 先刷新,在关流
*
* 注意:字符输出流,写数据
* 写一次,刷新一次
* write flush
*/
/*
* OutputStreamWriter写数据,默认编码GBK
*/
publicstaticvoid writeGBK()throws IOException {
//创建转换流对象,封装字节输出流
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("c:\\gbk.txt"));
//写字符串
osw.write("你好");
osw.close();
}
/*
* OutputStreamWriter写数据,采用UTF-8编码表转换流构造方法中,加编码表名字
*/
publicstaticvoid writeUTF8()throws IOException {
//创建转换流对象,封装字节输出流
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("c:\\utf.txt"),"UTF-8");
//写字符串
osw.write("你好");
osw.close();
}
2. InputStreamReader 转换流
InputStreamReader继承Reader
读取的方法read,读取单个字符,读取字符数组
作用:字节流向字符的桥梁
构造方法:
InputStreamReader(InputStream in)
传递任意字节输入流,转换流从传递字节输入流中,读取数据
自己会进行自己的判断,如果是负数字节,让字节流再次读取
new InputStreamReader( new FileInputStream())
InputStreamReader(InputStream in,String 编码表名字)
程序Demo:(字符数组读入文件)
publicstaticvoid method_1()throws IOException{
//创建字节输入流,包装文件
FileInputStream fis = new FileInputStream("c:\\1.txt");
//创建转换流对象,传递字节输入流
InputStreamReader isr = new InputStreamReader(fis);
//定义字符数组
char[] cbuf =newchar[1024];
int len = 0 ;
while((len = isr.read(cbuf))!=-1){
System.out.println(new String(cbuf,0,len));
}
isr.close();
}
* 转换流,InputStreamRrader读取文件
* 采用UTF-8编码表
* 转换流的构造方法中,写字符串编码表名字
*/
publicstaticvoid readUTF()throws IOException{
//创建转换流对象 ,传递字节输入流,指定编码表名字
InputStreamReader isr = new
InputStreamReader(new FileInputStream("c:\\utf.txt"),"utf-8");
char[] cbuf =newchar[1024];
int len = 0 ;
while((len = isr.read(cbuf))!=-1){
System.out.println(new String(cbuf,0,len));
}
isr.close();
}
7.转换流的便捷类
1. OutputStreamWriter 类
子类 FileWriter, 继承OutputStreamWriter
FileWriter是OutputStreamWriter类便捷写法
前提:操作的文件必须是操作系统中的默认编码表才可以
FileWriter 构造方法
FileWriter(File file)传递File对象,写入目的
FileWriter(String filename)传递字符串文件名
程序Demo:(转换流便捷类写出文件)
publicclass FileWriterDemo {
publicstaticvoid main(String[] args)throws IOException {
FileWriter fw = new FileWriter("c:\\abc.txt");
fw.write("你好吗");
fw.flush();
char[] arr ="今天还行是吧,好像没什么事情".toCharArray();
fw.write(arr);
fw.flush();
fw.write(arr, 3, 4);
fw.flush();
fw.close();
}
}
2. InputStreamReader 类
子类 FileReader,继承InputStreamReader
FileReader类是InputStreamReader类便捷写法
前提:操作的文件必须是操作系统中的默认编码表才可以
构造方法
FileReader(File file)传递File对象,读取数据源
FileReader(String filename) 传递字符串文件名,读取数据源
读取方法:
read() 1个字符,
read(char[] c)字符数组
程序Demo:(转换流读入文件)
publicclass FileReaderDemo {
publicstaticvoid main(String[] args)throws IOException {
FileReader fr = new FileReader("c:\\abc.txt");
int len = 0 ;
char[] cbuf =newchar[1024];
while((len = fr.read(cbuf))!=-1){
System.out.print(new String(cbuf,0,len));
}
fr.close();
}
}
- 黑马程序员——第十二篇:字节流、字符流
- 黑马程序员——IO流-字节流、字符流
- 黑马程序员—IO流(字符流与字节流)
- 黑马程序员——IO字符流与字节流
- 黑马程序员——字节流和字符流
- 黑马程序员——字节流和字符流
- 黑马程序员——字符流,字节流
- 黑马程序员——基础篇——关于字节流和字符流的区别
- 黑马程序员:字符流与字节流
- 黑马程序员---字符流,字节流
- 黑马程序员——第19天——IO输入与输出(缓冲字符流,字节流,缓冲字节流,字符流、字节流总结)
- 黑马程序员——IO流(三)字节流、字符流、转换流
- 黑马程序员——java基础IO流概述、字符流、字节流、流操作规律
- 黑马程序员——Java中IO流(字节流 字符流 其他流 )
- 黑马程序员——IO流(IO流、字节流、字符流)
- 黑马程序员——IO(一)字符流,字节流,缓冲流,转换流
- 黑马程序员——Java基础---IO流概述、字符流、字节流、流操作规律
- 黑马程序员——IO流之IO流概述、字符流、字节流
- 自己总结前端web知识学习——太多太多(html/css/js/php)
- MySql错误代码1045的解决方案
- MotionEvent的getX(),getY()与getRawX(),getRawY()区别
- 链矩阵乘法
- canvas自适应屏幕
- 黑马程序员——第十二篇:字节流、字符流
- 函数式编程-闭包跨域开发
- 安卓技能之闪电侠 -- 消息推送
- Faster R-CNN
- 源码探索系列4---数据库ORM框架之Ormlite解析
- 关于Notepad++中用正则表达式匹配中文的问题
- 2015-12-16
- 学习方法总结
- 11g 之后wm_contact