您的位置:首页 > 其它

IO流

2015-08-01 16:39 369 查看
IO流:
 一、字节流和字符流:
字节流:包含二进制数据  数据会以字节序列的形式写入到流中,与它们在内存中的形式完全一样,在这个过程中不会发生数据转换
字符流:包含字符数据  用于存储和提取文本,在写入字符流之前,所有的二进制数据都必须转化成文本来表示
 二、支持流输入和输出的主要包是java.io,定义了大量的接口和类。
1、能够从键盘中读取数据。
2、能够从流中读取格式化输出,比如System.out
3、能够读写包含字符串和基本类型数据的文件
4、能够读写包含对象的文件
 三、字节流输入输出类(字节流文件拷贝+缓冲流 ,提高性能)
1、输入类 InputStream 字节流输入操作的基类(抽象类)
InputStream is =new FileInputStream(src);
缓冲输入流:BufferedInputStream  通过缓存在内存中,让流的读取更加高效。
它派生自以InputStream作为基类的FilerInputStream
InputStream is =new BufferedInputStream(new FileInputStream(src));
2、输出类 OutputStream 字节流输出操作的基类(抽象类)
OutputStream os =new FileOutputStream(dest);
缓冲输出流:BufferedOutputStream
OutputStream os =new BufferedOutputStream( new FileOutputStream(dest));
public static void main(String[] args) {

}
/**
* 文件的拷贝
*/
public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
//1、建立联系 源(存在且为文件) +目的地(文件可以不存在)
File src =new File(srcPath);
File dest =new File(destPath);
if(! src.isFile()){ //不是文件或者为null
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
//2、选择流
InputStream is =new BufferedInputStream(new FileInputStream(src));
OutputStream os =new BufferedOutputStream( new FileOutputStream(dest));
//3、文件拷贝   循环+读取+写出
byte[] flush =new byte[1024];
int len =0;
//读取
while(-1!=(len=is.read(flush))){
//写出
os.write(flush, 0, len);
}
os.flush(); //强制刷出

//关闭流
os.close();
is.close();
}

 四、字符流输入输出类
1、输入类 Reader 读取字符流的基类(抽象类)
缓冲输入流:BufferedReader
2、输出类 Writer 写字符流的基类(抽象类)
缓冲输出流:BufferedWriter
public static void main(String[] args) {
//创建源 仅限于 字符的纯文本
File src =new File("");
File dest =new File("");
//选择流
BufferedReader reader =null;
BufferedWriter wr =null;
try {
reader =new BufferedReader(new FileReader(src));
wr =new BufferedWriter(new FileWriter(dest));
//读取操作
/*
char[] flush =new char[1024];
int len =0;
while(-1!=(len=reader.read(flush))){
wr.write(flush, 0, len);
}*/
//新增方法的操作
String line =null;
while(null!=(line=reader.readLine())){
wr.write(line);
//wr.append("\r\n");
wr.newLine(); //换行符号
}
wr.flush();//强制刷出
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("源文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取失败");
}finally{
try {
if (null != wr) {
wr.close();
}
} catch (Exception e2) {
}
try {
if (null != reader) {
reader.close();
}
} catch (Exception e2) {
}
}
}

 五、转换流(字节转为字符,确保源不能为乱码)
1、解码 InputStreamReader 
2、编码 OutputStreamWriter
public static void main(String[] args) throws IOException {
//指定解码字符集
BufferedReader br =new BufferedReader(
<span style="color:#ff0000;">new InputStreamReader</span>(
new BufferedInputStream(
new FileInputStream(
new File("src"))),"UTF-8")
);
//写出文件 编码
BufferedWriter bw =new BufferedWriter(
<span style="color:#ff0000;">new OutputStreamWriter</span>(
new BufferedOutputStream(
new FileOutputStream(new File("dest")))));

String info =null;
while(null!=(info=br.readLine())){
//System.out.println(info);
bw.write(info);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  io流