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));
四、字符流输入输出类
1、输入类 Reader 读取字符流的基类(抽象类)
缓冲输入流:BufferedReader
2、输出类 Writer 写字符流的基类(抽象类)
缓冲输出流:BufferedWriter
五、转换流(字节转为字符,确保源不能为乱码)
1、解码 InputStreamReader
2、编码 OutputStreamWriter
一、字节流和字符流:
字节流:包含二进制数据 数据会以字节序列的形式写入到流中,与它们在内存中的形式完全一样,在这个过程中不会发生数据转换
字符流:包含字符数据 用于存储和提取文本,在写入字符流之前,所有的二进制数据都必须转化成文本来表示
二、支持流输入和输出的主要包是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(); }
相关文章推荐
- java IO流文件的读写具体实例
- 装饰类设计模式 【不完善版】 IO流
- IO流操作规律 转码
- 重置标准输入输出流 IO流
- 【Java基础】IO流操作
- JAVA基础-IO流
- IO(输入输出流) 4000
- Java培训-IO流补充
- IO流操作规律(一)
- 黑马程序员————————IO流 FileWriter类 和 FileReader类的一些基本用法
- 黑马程序员--Java基础--IO流
- io 学习
- 黑马程序员—装箱拆箱与IO流重点
- 黑马程序员——java基础——IO流(2)File类,IO常用流对象及IO包中其他类
- 黑马程序员_java中IO流的操作规律
- 黑马程序员-java基础-IO流中的字节流和字符流
- 参加某培训机构Java培训记录(七)
- 黑马程序员——Java IO字符流、字节流(小练习)
- 黑马程序员——Java 对象序列化
- 黑马程序员—IO学习笔记