1 /* 2 FileReader,FileWriter 3 BufferedReader,BufferedWriter(4个字符流对象) 4 5 InputStream,OutputStream 6 BufferedReader,BufferedWriter(4个字节流对象); 7 8 操作图片数据,就需要用到字节流了 9 */ 10 11 import java.io.*; 12 class FileStream 13 { 14 public static void main(String[] args)throws IOException 15 { 16 readfile3(); 17 } 18 19 public static void readfile3()throws IOException 20 { 21 FileInputStream fis =new FileInputStream("fos.txt"); 22 23 byte [] buf =new byte[fis.available()];//定义一个刚刚好的缓冲区就不需要循环了 24 fis.read(buf); 25 sop(new String(buf)); 26 fis.close(); 27 } 28 29 public static void readfile2()throws IOException 30 { 31 FileInputStream fis =new FileInputStream("fos.txt"); 32 byte [] buf =new byte[1024]; 33 int len=0; 34 while((len=fis.read(buf))!=-1) 35 { 36 String s =new String(buf,0,len); 37 /* 38 public String(byte[] bytes,int offset,int length) 39 通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。 40 新 String 的长度是字符集的函数,因此可能不等于该子数组的长度。 41 */ 42 sop(s); 43 } 44 fis.close(); 45 } 46 47 public static void readFile()throws IOException 48 { 49 FileInputStream fis =new FileInputStream("fos.txt"); 50 int ch =0; 51 while((ch=fis.read())!=-1) 52 { 53 sop((char)ch); 54 } 55 fis.close(); 56 } 57 58 public static void writefile()throws IOException 59 { 60 FileOutputStream fos =new FileOutputStream("fos.txt"); 61 62 fos.write("whoisjena".getBytes()); 63 fos.close(); 64 } 65 66 public static void sop(Object obj) 67 { 68 System.out.println(obj); 69 } 70 }
正式开工:
1 public static void copyPic() 2 { 3 FileInputStream fis =null; 4 FileOutputStream fos =null; 5 6 try 7 { 8 fis =new FileInputStream("拷贝图片.png"); 9 fos =new FileOutputStream("拷贝图片copy.png"); 10 11 byte[] buf =new byte[1024]; 12 int len =0; 13 while((len=fis.read(buf))!=-1) 14 { 15 fos.write(buf,0,len); 16 } 17 } 18 catch (IOException e) 19 { 20 throw new RuntimeException("图片读写失败"); 21 } 22 23 finally 24 { 25 try 26 { 27 if (fis!=null) 28 { 29 fis.close(); 30 } 31 } 32 catch (IOException e) 33 { 34 throw new RuntimeException("图片读取失败"); 35 } 36 try 37 { 38 if (fos!=null) 39 { 40 fos.close(); 41 } 42 } 43 catch (IOException e) 44 { 45 throw new RuntimeException("图片写入失败"); 46 } 47 } 48 }
当然为了提高效率,可以使用缓冲区技术:
1 //使用缓冲区的文件复制 2 public static void copyFileBuffer() 3 { 4 BufferedInputStream bufis =null; 5 BufferedOutputStream bufos =null; 6 try 7 { 8 bufis = new BufferedInputStream(new FileInputStream("book.pdf")); 9 bufos = new BufferedOutputStream(new FileOutputStream("bookcopyb.pdf")); 10 byte[] buf =new byte[1024]; 11 int len=0; 12 while((len=bufis.read(buf))!=-1) 13 { 14 bufos.write(buf,0,len); 15 } 16 } 17 catch (IOException e) 18 { 19 throw new RuntimeException("文件读写失败"); 20 } 21 finally 22 { 23 try 24 { 25 if (bufis!=null) 26 { 27 bufis.close(); 28 } 29 } 30 catch (IOException e) 31 { 32 throw new RuntimeException("读失败"); 33 } 34 35 try 36 { 37 if (bufos!=null) 38 { 39 bufos.close(); 40 } 41 } 42 catch (IOException e) 43 { 44 throw new RuntimeException("写失败"); 45 } 46 47 } 48 }
效果还是比较明显的:使用缓冲区后时间约为之前的1/3:
1 public static void main(String[] args) 2 { 3 long start =System.currentTimeMillis(); 4 copyPicBuffer(); 5 long end =System.currentTimeMillis(); 6 sop(end-start); 7 }
copyFile()
1 public static void copyLNFile()throws IOException 2 { 3 //BufferedReader bufr =null; 4 FileReader fr =null;//定义fr便于异常处理时关闭流 5 LineNumberReader lnr =null; 6 BufferedWriter bufw =null; 7 8 lnr =new LineNumberReader(fr =new FileReader("Test.java")); 9 bufw =new BufferedWriter(new FileWriter("Testcopy.java")); 10 String line=null; 11 while((line=lnr.readLine())!=null) 12 { 13 14 //System.out.println(lnr.getLineNumber());//在控制台是可以正常显示行号的 15 bufw.write(lnr.getLineNumber()+" "+line);//write语句要放在一起,如果分开到了文件中由于编码问题会导致行号变为了符号 16 //bufw.write(line); 17 bufw.newLine(); 18 bufw.flush(); 19 } 20 }