Java 使用字符流拷贝数据
使用字符流拷贝数据时,需要注意在文件末尾处的数据,因为最后一次读取的长度不会刚好与数组input长度相同,所以需要引入新的变量来存储每次读取的长度。
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class RWByCharStream { public static void main(String[] args) { try { // File file=new File("java.txt"); FileInputStream fis=new FileInputStream("java.txt"); InputStreamReader isr=new InputStreamReader(fis,"UTF-8"); FileOutputStream fos=new FileOutputStream("java_copy.txt"); OutputStreamWriter osw=new OutputStreamWriter(fos); char input[]=new char[100]; int f=0; while((f=isr.read(input))!=-1) { // System.out.println(new String(input,0,f)); osw.write(input,0,f); } isr.close(); fis.close(); osw.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
使用缓冲字符流读写数据:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class RWByBufferedCharStream { public static void main(String[] args) { try { // File file=new File("java.txt"); FileInputStream fis=new FileInputStream("java.txt"); InputStreamReader isr=new InputStreamReader(fis,"UTF-8"); FileOutputStream fos=new FileOutputStream("java_copy_buffer.txt"); OutputStreamWriter osw=new OutputStreamWriter(fos); BufferedReader br=new BufferedReader(isr); BufferedWriter bw=new BufferedWriter(osw); String input; while((input=br.readLine())!=null) { bw.write(input); } bw.close(); br.close(); isr.close(); fis.close(); osw.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
拷贝的文件中没有换行,全在同一行。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; public class RWByBufferedCharStream { public static void main(String[] args) { try { // File file=new File("java.txt"); FileInputStream fis=new FileInputStream("java.txt"); InputStreamReader isr=new InputStreamReader(fis,"UTF-8"); FileOutputStream fos=new FileOutputStream("java_copy_buffer.txt"); OutputStreamWriter osw=new OutputStreamWriter(fos); BufferedReader br=new BufferedReader(isr); // BufferedWriter bw=new BufferedWriter(osw); PrintWriter pw=new PrintWriter(osw); String input; while((input=br.readLine())!=null) { // bw.write(input); pw.println(input); } // bw.close(); pw.flush(); //将最后一次读取的内容从缓冲区读出来 pw.close(); br.close(); isr.close(); fis.close(); osw.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
使用PrintWriter代替BufferedWriter,方便处理最后一次读取的内容和每行里的换行。