| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2305 人关注过本帖
标题:字节流、字符流
只看楼主 加入收藏
寒菲紫澈
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-8-22
收藏
 问题点数:0 回复次数:1 
字节流、字符流
字节流、字符流如何用读取/写入数据
搜索更多相关主题的帖子: 如何 
2016-08-22 19:10
寒菲紫澈
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-8-22
收藏
得分:0 
流的分类
根据方向分为输入流和输出流
输入流
所有接收,获得,读取的操作都是属于输入流
所有的输入流名字都带有input或Reader

输出流
所有发送,写的操作都是属入输出流
所有的输出流名字都带有output或Writer

根据格式划分

字节流
单字节为单位发送或接收数据,所有的数据格式都可以通过字节流来传输
但是字节流对中文(以及其他双字节的文字)格式不能直接支持,需要编码转换
字节流可以传输所有的文件包括二进制文件
字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的

字符流
双字节为单位(1字符)发送或接收数据,只能用于发送或接收文本格式的数据(比如txt文件),
本身支持中文(以及其他双字节文字)格式,不需要转码操作。
字符流只能传输文本文件
字符流在操作时使用了缓冲区,通过缓冲区再操作文件。

JAVA中流的结构图。



字符流复制文件

File file=new File("d:\\1.txt");
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);//读取一行
File file2=new File("d:\\2.txt");
FileWriter fw=new FileWriter(file2);
BufferedWriter bw=new BufferedWriter(fw);//添加一行
String str=br.readLine();
while (str!=null) {
bw.write(str);
str=br.readLine();
}
bw.close();
fw.close();
br.close();
fr.close();



字节流复制文件

File file1 = new File("D:/1.gif");
File file2 = new File("D:/2.gif");
FileInputStream in = new FileInputStream(file1);
FileOutputStream out = new FileOutputStream(file2);
int a = in.read();
while(a!=-1){
out.write(a);
a = in.read();
}
out.close();
in.close()

5、fileReader /inputstream/?    联系与区别
1 ) File 类介绍
File 类封装了对用户机器的文件系统进行操作的功能。例如,可以用 File 类获得文件上次修改的时间,移动,或者对文件进行删除、重命名。换句话说,流类关注的是文件内容,而 File 类关注的是文件在磁盘上的存储
File 类的主要方法有(),lastMod: getName(),getCanonicalFileified(),isDerector(),isFile(),getPath() 等;
2 ) File 类与 FileInputStream 类的区别:
流类关注的是文件内容,而 File 类关注的是文件在磁盘上的存储。
File 不属于文件流 , 只能代表一个文件或是目录的路径名而已。
提示:
如果处理文件或者目录名,就应该使用 File 对象,而不是字符串。例如, File 类的 equals 方法知道一些文件系统对大小写是敏感的,目录尾的“ / ”字符无关紧要。
FileInputStream 类或者 FileReader 类的构造函数有多个,其中典型的两个分别为:一个使用 File 对象为参数;而另一个使用表示路径的 String 对象作为参数
FileInputStream 类
1 ) FileInputStream 类介绍:
以字节为单位的流处理。字节序列:二进制数据。与编码无关,不存在乱码问题。
FileInputStream 类的主要方法有:
Read (), read ( byte[] b ), read ( byte[],int off,int len ) ,available();
2 ) FileInputStream 类与 FileReader 类的区别:
两个类的构造函数的形式和参数都是相同的,参数为 File 对象或者表示路径的 String ,它们到底有何区别呢?
FileInputStream :以字节流方式读取;
FileReader :把文件转换为字符流读入;
InputStream提供的是字节流的读取,而非文本读取,这是和Reader类的根本区别。用Reader读取出来的是char数组或者String ,使用InputStream读取出来的是byte数组。
Reader类及其子类提供的字符流的读取char,inputStream及其子类提供字节流的读取byte,所以FileReader类是将文件按字符流的方式读取,FileInputStream则按字节流的方式读取文件;InputStreamReader可以将读如stream转换成字符流方式,是reader和stream之间的桥梁
FileInputStream 类以二进制输入 / 输出, I/O 速度快且效率搞,但是它的 read ()方法读到的是一个字节,很不利于人们阅读。 而 FileReader 类弥补了这个缺陷,可以以文本格式输入/ 输出,非常方便;比如可以使用 while((ch = filereader.read())!=-1 ) 循环来读取文件;可以使用BufferedReader 的 readLine() 方法一行一行的读取文本
FileReader 类
FileReader fr = new FileReader("ming.txt");   
  char[] buffer = new char[1024];   
  int ch = 0;   
  while((ch = fr.read())!=-1 )   
   {   
   System.out.print((char)ch);   
  }   
InputStreamReader类

InputStreamReader isr=InputStreamReader(new FileInputStream(“ming.txt”));
 While((ch=isr.read()!=-1)
{
   System.out.print((char)ch);
}
BufferedReader类
BufferedReader br=new BufferedReader(new BufferedReader(new FileInputReader(“ming.txt”)));
String data=null;
While((data=br.readLine())!=null)
{
Sytem.out.println(data);
}
总结以上内容,得出比较好的规范用法:
1) File file = new File ("hello.txt");   
 FileInputStream in=new FileInputStream(file);   
InputStreamReader inReader=new InputStreamReader(in);  
BufferedReader bufReader=new BufferedReader(inReader);   
2)FileInputStream in=null;   
 File file = new File ("hello.txt");   
in=new FileInputStream(file);   
 BufferedReader bufReader=new BufferedReader(new InputStreamReader(in));   
3) File file = new File ("hello.txt");   
BufferedReader bufReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));   
 
2016-08-27 18:11
快速回复:字节流、字符流
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017076 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved