字节流:InputStream,OutStream
字符流:Reader,Writer
字符流专门处理用来处理包含文字的文件,可以指定文件的编码方式
字节流可以指定一次传输的大小,常常用来处理图片,视屏等大文件
一般txt的编码方式是ACSII码,比如你写入"12abc",实际存入计算机的是81—82—97—98—99,的二进制
下面以两个例子来阐述字符流和字节流
字符流:
/**
* 字符流方式,拷贝文件
* @author Li Jia Xuan
* @version 1.0
* @since 2012-10-30
* @time 上午10:53:32
*/
public class TestCopy {
/**
*
* @param source
* @param target
*/
public static void copy(String source, String target) {
if (!source.equals("") && !target.equals("")) {
File sourcefile = new File(source);
File targetfile = new File(target);
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(sourcefile)));
// bw=new BufferedWriter(new FileWriter(targetfile));
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(targetfile)));
String line = "";
while ((line = br.readLine()) != null) {
bw.write(line);
}
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeIO(br,bw);
}
}
}
/**
* 关闭流,一般采取先开后关、由外向内的原则
* @param br
* @param bw
*/
public static void closeIO(BufferedReader br,BufferedWriter bw){
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
字节流:
/**
*利用字节流,实现对视屏音频文件的拷贝
*@author Li Jia Xuan
*@version 1.0
*@since 2012-10-30
*@time 上午11:42:48
*/
public class TestCopy1 {
public static void copy(String source,String target){
InputStream is=null;
OutputStream os=null;
try {
is=new FileInputStream(source);
os=new FileOutputStream(target);
byte[] b=new byte[1024*1024];//1K的读取单位
for(int len=0;(len=is.read(b, 0, b.length))!=-1;){
os.write(b, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void closeIO(InputStream is,OutputStream os){
if(os!=null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
备注:以上包名没有导入
分享到:
相关推荐
Java 流在处理上分为字符流和字节流。字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符、字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组。 Java 内用 Unicode 编码存储字符,字符流...
Java字符流和字节流
Java IO字符流和字节流,讲述了字符传输流和字节传输流的不同和作用
JAVA 字符流与字节流
重载重写、接口、抽象类、String指针和字符串拼接、异常catch、throw、throws、字符流和字节流
这是一个关于io流的文件,里面包括代码和图片截图。详细的说明了io流的内在
java字节流和字符流java字节流和字符流java字节流和字符流
javaIO操作各种文件,筛选文件,拷贝文件,上传下载,字符流和字节流都有,根据实际项目情况直接贴就可以了,根据文件类型选择不同的testclss就可以。全部使用效率最高的流
1. 什么是流 ...Java中基本的两个字节流类是InputStream和OutputStream,它们分别代表了组基本的输入字节流和输出字节流。InputStream类与OutputStream类均为抽象类,我们在实际使用中通常使用Java类库中提
主要为大家详细介绍了Java中字符流与字节流的区别,这两个的概念易混淆,今天就为大家进行详细区分,感兴趣的小伙伴们可以参考一下
本篇文章主要介绍了Java的IO流分为字符流(Reader,Writer)和字节流(InputStream,OutputStream),字节流顾名思义字节流就是将文件的内容读取到字节数组,对初学者很有用,有需要的朋友可以了解一下。
java字节流和字符流[整理].pdf
字节流与字符流在java.io包中操作文件内容的主要有两大类:字节流、字符流,两类都分为输入和输出操作。在字节流中输出数据主要是使用OutputStream完成,输入使的是InputStream,在字符流中输出主要是使用Writer类...
数据流,对象流,字节流,字符流 的一些总结说明
字节流和字符流的区别: • 读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次 可能读多个字节。 • 处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流...
java IO流 1. 流的概念 2. 输入流和输出流 3. 字符流、字节流 4. 缓冲流 5. 转换流处理流
字节流字符
字节流与字符流的练习文档,内附一个简要的记事本综合文档。
IO流学习(字节流 字符流 文本流)!
InputStreamReader 是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。FileReader 可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader 的方法。我们可以从这个类...