您的位置:首页 > 编程语言 > Java开发

Java IO

2015-08-26 20:35 441 查看
1、字符流与字节流
字节流可以处理所有类型的数据,包括图片,文字,音频、视频等。在读取时,读到一个字节就返回一个字节。
在Java中对应的类都以Stream结尾。

字符流中仅能处理纯文本数据,如txt文本等。
在Java中对应的类都以Writer或者Reader结尾

2、字符、字节与编码
字节是通过网络传输信息或者在硬盘、内存中存储信息的单位,是计算机信息技术用于计量存储容量和传输容量的计量单位。1个字节等于8位二进制,即一个8位的二进制数。如0X01,0XFA等等...
字符是人们使用的记号,是抽象意义上的一个符号,如'1','中','a','$' 等等....
字符集(Charset)也称作“编码”,各个国际和地区所制作不同ANSI编码标准中,都只规定了各自语言所需的“字符”。这些ANSI编码标准规定的内容包含两层含义:
1)使用哪些字符。也就是说哪些汉字、字母和符号会被收入标准中,所包含的“字符”的合集就叫“字符集”
2)规定每个“字符”分别使用一个字节还是多个字节进行存储,使用哪些字节来存储,这个规定就叫“编码”
3、下面是一个字符流读写例子
package cn.lebo;

import java.io.*;

public class TestStream {

public static void main(String[] args) {
try {
FileInputStream fis = new  FileInputStream("1.gif");
FileOutputStream fos = new FileOutputStream("1.new.gif");
byte input[] = new byte[10];
while(fis.read(input) != -1){
fos.write(input);
}
fis.close();
fos.close();
System.out.println("copy done");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

}

}
4、下面是一个使用带缓冲的字节流读取数据的例子
package cn.lebo;

import java.io.*;

public class TestBufferedByteStream {

public static void main(String[] args) {

try {
FileInputStream fis = new FileInputStream("Microsoft Office 2010.iso");
BufferedInputStream bis = new BufferedInputStream(fis,10000);
FileOutputStream fos = new FileOutputStream("Microsoft Office 2010.new.iso");
BufferedOutputStream bos = new BufferedOutputStream(fos,10000);

byte data[] = new byte[5000];

int count = 0;
int len = 0;
long before = System.currentTimeMillis();
while((len = bis.read(data) )!= -1){
bos.write(data,0,len);
count ++;
}

bis.close();
fis.close();
bos.close();
fos.close();

System.out.println("读写了" + count + "次");
System.out.println("耗费了" + (System.currentTimeMillis() - before) + "ms");
} catch (IOException e) {
e.printStackTrace();
}
}

}

5、下面是一个使用字符流读写文件的例子(又称为转换流,即把字节流转为字符流)
package cn.llbb;

import java.io.*;
public class TestRWCharStream {

public static void main(String[] args) {
try {
File fin = new File("java.txt");
File fout = new File("java_new.txt");
FileInputStream fis = new FileInputStream(fin);
FileOutputStream fos = new FileOutputStream(fout);
InputStreamReader isr = new InputStreamReader(fis);
OutputStreamWriter osr = new OutputStreamWriter(fos);
char input[] = new char[100];
int len = 0;
while((len=isr.read(input)) != -1){
osr.write(input,0,len);
}

osr.close();
isr.close();
fos.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

6、下面是一个带有缓冲的字符流读写文件的例子
package cn.llbb;

import java.io.*;
public class TestRWBufferedCharStream {

public static void main(String[] args) {
try {
File fin = new File("java.txt");
File fout = new File("java_new.txt");
FileInputStream fis = new FileInputStream(fin);
FileOutputStream fos = new FileOutputStream(fout);
InputStreamReader isr = new InputStreamReader(fis);
OutputStreamWriter osr = new OutputStreamWriter(fos);

BufferedReader br = new BufferedReader(isr);
BufferedWriter bw = new BufferedWriter(osr);

//PrintWriter pw = new PrintWriter(osr,true); //如果这里制定了第二个参数为true
//那么下面就不需要再调用pw.flush

String str = null;
while((str=br.readLine())!=null){
bw.write(str+ "\n");                  //如果后面不加换行符,新写入的文件就                                                                      //没有换行符
//pw.println(str);                    //注意这里是println方法
}

//pw.flush();
bw.flush();

br.close();
bw.close();
//pw.close();
osr.close();
isr.close();
fos.close();
fis.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


本文出自 “leboit” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: