温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

IO流-输入输出流:字节流、字符流、缓冲流、转换流

发布时间:2020-08-02 01:55:47 来源:网络 阅读:447 作者:glblong 栏目:开发技术

 

字节输入输出流:

  1. FileInputStream fis = new FileInputStream(filePath); 
  2. FileOutputStream fos = new FileOutputStream(savePath); 
  3.  
  4. byte[] buffer = new byte[1024]; 
  5. int length = 0
  6. while (-1 != (length = fis.read(buffer))) 
  7. fos.write(buffer); 
  8. /** 
  9.   * 等价于下面的写法: 
  10.   * String str = new String(buffer, 0, length); 
  11.   * fos.write(str.getBytes()); 
  12.   */ 

 

字符输入输出流:

  1. FileReader fr = new FileReader(fileName);  
  2. char[] buffer = new char[1024];//使用字符数组  
  3. int length = 0;  
  4. while (-1 != (length = fr.read(buffer)))  
  5. {  
  6.     for (int i = 0; i < length; i++)  
  7.     {  
  8.         System.out.print(buffer[i]);  
  9.     }  
  10. }  
  11.  
  12.  
  13. FileWriter fw = new FileWriter(fileName); 
  14. fw.write("这段字符串按字符写入good"); 
  15. fw.flush();//写完清空缓存 

 

转换流、缓冲流:

  1. FileInputStream fis = new FileInputStream(fileName); 
  2. InputStreamReader isr = new InputStreamReader(fis); 
  3. BufferedReader br = new BufferedReader(isr); 
  4.  
  5. FileOutputStream fos = new FileOutputStream(saveName); 
  6. OutputStreamWriter osw = new OutputStreamWriter(fos); 
  7. BufferedWriter bw = new BufferedWriter(osw); 
  8.  
  9.  
  10. String str = null
  11. while (null != (str = br.readLine()))//读取每行直到'\r'或'\t'则为空 
  12.     bw.write(str);//write可以写入字符串或者字符 
  13.     bw.newLine();//换行 
  14.     bw.flush();//清空缓存 


文件拷贝方法:练习字节流、字符流、缓冲流、转换流

  1. public class CopyMethod 
  2.     public static void main(String[] args) 
  3.     { 
  4.         CopyMethod cm = new CopyMethod(); 
  5.         String scr = "F:/java/tt.txt"
  6.         String dest = "F:/java/xx.txt"
  7.         cm.copyMethod(scr, dest); 
  8.     } 
  9.      
  10.     public void copyMethod(String scr,String dest) 
  11.     { 
  12.         BufferedReader fr = null
  13.         BufferedWriter bw = null
  14.         try 
  15.         { 
  16.             FileInputStream fis = new FileInputStream(scr);//文件输入流 
  17.             InputStreamReader isr = new InputStreamReader(fis);//转换流,将字节流转换为字符流 
  18.             fr = new BufferedReader(isr);//创建字符缓冲流,每次读取一行字符 
  19.              
  20.             FileOutputStream fos = new FileOutputStream(dest); 
  21.             OutputStreamWriter osw = new OutputStreamWriter(fos); 
  22.             bw = new BufferedWriter(osw); 
  23.              
  24.             String str = null;//缓冲流读取和写入的都是字符串 
  25.             while(null != (str = fr.readLine()))//使用readLine(),读取到\r或\n结束 
  26.             { 
  27.                 bw.write(str);//写入一行字符串 
  28.                 bw.newLine();//换行 
  29.                 bw.flush();//清空缓存,使用writer需要flush 
  30.             } 
  31.         } 
  32.         catch (FileNotFoundException e) 
  33.         { 
  34.             e.printStackTrace(); 
  35.         } 
  36.         catch (IOException e) 
  37.         { 
  38.             e.printStackTrace(); 
  39.         } 
  40.         finally//关闭最外层的流 
  41.         { 
  42.             if(fr != null
  43.             { 
  44.                 try 
  45.                 { 
  46.                     fr.close(); 
  47.                 } 
  48.                 catch (IOException e) 
  49.                 { 
  50.                     e.printStackTrace(); 
  51.                 } 
  52.             } 
  53.             if(null != bw) 
  54.             { 
  55.                 try 
  56.                 { 
  57.                     bw.close(); 
  58.                 } 
  59.                 catch (IOException e) 
  60.                 { 
  61.                     e.printStackTrace(); 
  62.                 } 
  63.             } 
  64.         } 
  65.     } 

 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI