前面所举例的InputStream,OutputStream,是针对字节流进行操作的。
FileReader,FileWriter是针对字符流进行操作的。
另外在写入的时候并未提供编码格式,极易造成中文乱码。
InputStreamReader/OutputStreamWriter,为字节流与字符流的转换提供了途径。
实例一:读取每个字节转为char
@Test public void testInputStreamReader(){ BufferedReader br = null; BufferedWriter bw = null; try { //此时文件是在项目下,即contextPath/ File file = new File("test1.txt"); File file1 = new File("test2.txt"); //输入流 InputStream in = new FileInputStream(file); //输入转换器 InputStreamReader iReader = new InputStreamReader(in, "UTF-8"); // 输出流 OutputStream out = new FileOutputStream(file1); //输出转换器 OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8"); //缓冲包装 br = new BufferedReader(iReader); bw = new BufferedWriter(osw); byte[] c = new byte[1024]; int len; while((len = br.read())!= -1){ System.out.println("ASCII:"+len+","+"vlaue :"+(char)len); bw.write(len); } }catch (IOException e) { e.printStackTrace(); }finally{ 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(); } } } }
实例二:读入到字符数组
@Test public void testInputStreamReader(){ BufferedReader br = null; BufferedWriter bw = null; try { //此时文件是在项目下,即contextPath/ File file = new File("test1.txt"); File file1 = new File("test2.txt"); //输入流 InputStream in = new FileInputStream(file); //输入转换器 InputStreamReader iReader = new InputStreamReader(in, "UTF-8"); // 输出流 OutputStream out = new FileOutputStream(file1); //输出转换器 OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8"); //缓冲包装 br = new BufferedReader(iReader); bw = new BufferedWriter(osw); /*读入到字符数组*/ char[] c = new char[1024]; int len; while((len = br.read(c))!= -1){ System.out.println("字符长度:"+len); String str = new String(c, 0, len); System.out.print(str); bw.write(c, 0, len); } }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ 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(); } } } }
实例三:每次读取一行
@Test public void testInputStreamReader(){ BufferedReader br = null; BufferedWriter bw = null; try { //此时文件是在项目下,即contextPath/ File file = new File("test1.txt"); File file1 = new File("test2.txt"); //输入流 InputStream in = new FileInputStream(file); //输入转换器 InputStreamReader iReader = new InputStreamReader(in, "UTF-8"); // 输出流 OutputStream out = new FileOutputStream(file1); //输出转换器 OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8"); //缓冲包装 br = new BufferedReader(iReader); bw = new BufferedWriter(osw); /*每次读取一行*/ String str; while((str = br.readLine()) != null){ System.out.println(str); bw.write(str + "\n"); bw.newLine(); bw.flush(); } }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ 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(); } } } }