字节流:字节流是 8 位通用字节流,常用来处理图片、视频、文件
字节输入流:InputStream(父)---》
FileInputStream(子):常用方法:read() 、read(byte[])
字节输出流:OutputStream(父)----》
FileOutputStream(子):常用方法:write(int byte) 、 write(byte[])
注意:
1.输出流向外写数据的时候,如果目标地址不存在,自动创建
2.不管是字节输入流还是字节输出流,在读写数据完成之后,一定要手动关闭流资源,一般在finally代码中完成,垃圾回收机制不会自动回收物理资源
3.在用流操作文件的时候,有很多异常需要处理
读文件:
1 public static void Read(String address){ 2 File file=new File(address); 3 FileInputStream in=null; 4 //流来读 5 try { 6 in=new FileInputStream(file);//把信息放在流中 7 int data=0; //in.read()读到的其实就是字符对应的AS码值 8 while((data=in.read())!=-1){// -1表示文件读到末尾了 9 System.out.print((char)data); 10 } 11 } catch (FileNotFoundException e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } catch (IOException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 }finally{ 18 try { 19 in.close(); 20 } catch (IOException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 } 25 }
写文件:
1 public static void WriteFile(String address){ 2 FileOutputStream out=null; 3 try { 4 out=new FileOutputStream(new File(address)); 5 out.write(97); //97 -->a //98 -->b 6 out.write(98); 7 8 byte []bs=new byte[10];//借助字节数组 9 bs[0]='A'; 10 bs[1]='A'; 11 bs[2]='A'; 12 bs[3]='A'; 13 out.write(bs); 14 } catch (FileNotFoundException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 }finally{ 21 try { 22 out.close(); 23 } catch (IOException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 } 28 }
源代码:
1 import java.io.*; 2 3 public class ZiJie { 4 public static void main(String[] args) { 5 //ZiJie.Read("D:/temp/temp.txt"); 6 ZiJie.WriteFile("D:/temp/temp1.txt"); 7 } 8 9 public static void Read(String address){ 10 File file=new File(address); 11 FileInputStream in=null; 12 //流来读 13 try { 14 in=new FileInputStream(file);//把信息放在流中 15 int data=0; //in.read()读到的其实就是字符对应的AS码值 16 while((data=in.read())!=-1){// -1表示文件读到末尾了 17 System.out.print((char)data); 18 } 19 } catch (FileNotFoundException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } catch (IOException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 }finally{ 26 try { 27 in.close(); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 } 33 } 34 public static void WriteFile(String address){ 35 FileOutputStream out=null; 36 try { 37 out=new FileOutputStream(new File(address)); 38 out.write(97); //97 -->a //98 -->b 39 out.write(98); 40 41 byte []bs=new byte[10];//借助字节数组 42 bs[0]='A'; 43 bs[1]='A'; 44 bs[2]='A'; 45 bs[3]='A'; 46 out.write(bs); 47 } catch (FileNotFoundException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } catch (IOException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 }finally{ 54 try { 55 out.close(); 56 } catch (IOException e) { 57 // TODO Auto-generated catch block 58 e.printStackTrace(); 59 } 60 } 61 } 62 63 64 }