【Java-IO】IO入门
2016-02-16 17:42
387 查看
根据处理的对象分为:字节流和字符流
字节流可以处理所有的数据:通常以“Stream”结尾,读取一个字节,返回一个字节。
字符流则只能处理纯文本数据:通常以“Reader”或者“Writer”结尾,需要通过查找编码表,才能返回字符
注意点:
1. 记得读写完成,要close();
2. FileInputStream.read(byte[])返回的是读取byte到缓存的长度,如果没有了,返回-1
3. 如果读取byte[]后想转成String,最好设置编码
写数据也一样
注意点:
1. 获取read数据的时候,得注意长度,因为最后一次读取的时候,往往不能填满char[]
2. 输入和输出流的编码得一致
注意点:
1. 使用BufferedReader readline时,读出的字符串是不带\n的
2. 在关闭输出流之前要用flush()强制把缓冲区的内容写出
3. 可以使用PrintWriter的println方法写,这个自带\n,其次PrintWriter在new的时候,可以设置autoflush为true,就不用
在关闭之前flush
注意点:
1. 记得flush,记得\n
2. FileReader/Writer 使用的是系统默认的字符集编码,所以可能存在编码的问题。如果出问题了,请使用FileInputStream -> InputStreamReader,并构造时规定编码
字节流可以处理所有的数据:通常以“Stream”结尾,读取一个字节,返回一个字节。
字符流则只能处理纯文本数据:通常以“Reader”或者“Writer”结尾,需要通过查找编码表,才能返回字符
一、字节流读写文件
try { FileInputStream fis = new FileInputStream("files/test.txt"); FileOutputStream fos = new FileOutputStream("files/new.txt"); byte[]bytes = new byte[1024]; while(fis.read(bytes)!=-1){ fos.write(bytes); } System.out.println("copy done"); fos.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
注意点:
1. 记得读写完成,要close();
2. FileInputStream.read(byte[])返回的是读取byte到缓存的长度,如果没有了,返回-1
3. 如果读取byte[]后想转成String,最好设置编码
try { FileInputStream fis = new FileInputStream("files/test.txt"); byte[] input = new byte[1024]; fis.read(input); String inputString = new String(input,"UTF-8"); System.out.println(inputString); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); }
写数据也一样
try{ FileOutputStream fos = new FileOutputStream("files/test.txt"); String s = "这是我写入txt的内容"; byte[] bytes = s.getBytes("UTF-8"); fos.write(bytes); System.out.println("写入完成"); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
二、字符流读写文件
try { FileInputStream fis = new FileInputStream("files/test.txt"); FileOutputStream fos = new FileOutputStream("files/new.txt"); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); char[] temp = new char[50]; int len = 0; while((len = isr.read(temp))!=-1){ osw.write(temp, 0, len); // osw.write(temp); // System.out.print(temp); } System.out.println("copy done"); osw.close(); fos.close(); isr.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
注意点:
1. 获取read数据的时候,得注意长度,因为最后一次读取的时候,往往不能填满char[]
2. 输入和输出流的编码得一致
三、带有缓冲区的字符流读写
try { FileInputStream fis = new FileInputStream("files/test.txt"); FileOutputStream fos = new FileOutputStream("files/new.txt"); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); BufferedReader br = new BufferedReader(isr); // BufferedWriter bw = new BufferedWriter(osw); PrintWriter pw = new PrintWriter(osw,true); String line = null; while((line = br.readLine())!=null){ // bw.write(line+"\n"); pw.println(line); } // bw.flush(); // pw.flush(); pw.close(); // bw.close(); osw.close();fos.close(); br.close();isr.close();fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
注意点:
1. 使用BufferedReader readline时,读出的字符串是不带\n的
2. 在关闭输出流之前要用flush()强制把缓冲区的内容写出
3. 可以使用PrintWriter的println方法写,这个自带\n,其次PrintWriter在new的时候,可以设置autoflush为true,就不用
在关闭之前flush
四、FileReader和FileWriter
如果只针对简单的纯文本文件进行读写操作,可以使用FileReader和FileWriter来代替FileInputStream以及OutputStream转InpttStreamReader/OutputStreamWritertry { FileInputStream fis = new FileInputStream("files/test.txt"); FileOutputStream fos = new FileOutputStream("files/new.txt"); byte[]bytes = new byte[1024]; while(fis.read(bytes)!=-1){ fos.write(bytes); } System.out.println("copy done"); fos.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
注意点:
1. 记得flush,记得\n
2. FileReader/Writer 使用的是系统默认的字符集编码,所以可能存在编码的问题。如果出问题了,请使用FileInputStream -> InputStreamReader,并构造时规定编码
相关文章推荐
- Java编写日历程序,显示当前月份及日期
- 【Java-IO】RandomAccessFile
- Eclipse: Android Device Chooser - Unknown Target
- java的date类型无法读取 出现错误net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
- 使用Spring Security实现权限管理
- 使用Java+MySQL做的简易留言板
- java中文词语大小比较
- Java Map的几种循环方式学习总结
- 第一个java程序Hellorld
- 【JVM系列】Java类加载器
- spring 在启动服务器的时候报错 Could not open ServletContext resource [/WEB-INF/dispather-servlet.xml]
- eclipse — Failed to load the JNI shared library”……\jvm.dll问题原因以及解决方案
- java 项目转化为maven的web项目
- 打印 java异常
- java常用算法之字梯(广度优先搜索bfs)
- Java变量类型及初始化顺序
- java之eclipse常用快捷键
- is-a、has-a和like-a、组合、聚合和继承 两组概念的区别
- Eclipse 的 Debug 介绍与技巧
- Mac系统下的Eclipse实现Android真机调试