java读写文件操作

  1. public static void readFileByBytes(String fileName) {  
  2.     // 一般先创建file对象  
  3.     FileInputStream fileInput = null;  
  4.     try {  
  5.         File file = new File(fileName);  
  6.         if (!file.exists()) {  
  7.             file.createNewFile();  
  8.         }  
  9.         byte[] buffer = new byte[1024];  
  10.         fileInput = new FileInputStream(file);  
  11.         int byteread = 0;  
  12.         // byteread表示一次读取到buffers中的数量。  
  13.         while ((byteread = fileInput.read(buffer)) != -1) {  
  14.             System.out.write(buffer, 0, byteread);  
  15.         }  
  16.   
  17.     } catch (Exception e) {  
  18.         // TODO: handle exception  
  19.     } finally {  
  20.   
  21.         try {  
  22.             if (fileInput != null) {  
  23.                 fileInput.close();  
  24.             }  
  25.         } catch (IOException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         }  
  29.   
  30.     }  
  31.   
  32. }  

 

  1. public static void readFileByChars(String fileName) {  
  2.         FileReader reader = null;  
  3.         try {  
  4.             File file = new File(fileName);  
  5.             if (!file.exists()) {  
  6.                 file.createNewFile();  
  7.             }  
  8.             reader = new FileReader(file);  
  9.             char[] buffer = new char[1024];  
  10.             int charread = 0;  
  11.             while ((charread = reader.read(buffer)) != -1) {  
  12.                 System.out.print(buffer);  
  13.             }  
  14.         } catch (IOException e) {  
  15.             // TODO: handle exception  
  16.   
  17.         } finally {  
  18.   
  19.             try {  
  20.                 if (reader != null) {  
  21.                     reader.close();  
  22.                 }  
  23.             } catch (IOException e) {  
  24.                 // TODO Auto-generated catch block  
  25.                 e.printStackTrace();  
  26.             }  
  27.         }  
  28.   
  29.     }  


Java代码 
  1. public static void readByBufferedReader(String fileName) {  
  2.         try {  
  3.             File file = new File(fileName);  
  4.             // 读取文件,并且以utf-8的形式写出去  
  5.             BufferedReader bufread;  
  6.             String read;  
  7.             bufread = new BufferedReader(new FileReader(file));  
  8.             while ((read = bufread.readLine()) != null) {  
  9.                 System.out.println(read);  
  10.             }  
  11.             bufread.close();  
  12.         } catch (FileNotFoundException ex) {  
  13.             ex.printStackTrace();  
  14.         } catch (IOException ex) {  
  15.             ex.printStackTrace();  
  16.         }  
  17.     }  

 

  1. public void writeByFileOutputStream() {  
  2.   
  3.         FileOutputStream fop = null;  
  4.         File file;  
  5.         String content = "This is the text content";  
  6.         try {  
  7.             file = new File("c:/newfile.txt");  
  8.             fop = new FileOutputStream(file);  
  9.             // if file doesnt exists, then create it  
  10.             if (!file.exists()) {  
  11.                 file.createNewFile();  
  12.             }  
  13.             // get the content in bytes  
  14.             byte[] contentInBytes = content.getBytes();  
  15.   
  16.             fop.write(contentInBytes);  
  17.             fop.flush();  
  18.             fop.close();  
  19.   
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         } finally {  
  23.             try {  
  24.                 if (fop != null) {  
  25.                     fop.close();  
  26.                 }  
  27.             } catch (IOException e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.         }  
  31.     }  

 

  1. public static void writeByFileReader() {  
  2.         try {  
  3.             String data = " This content will append to the end of the file";  
  4.   
  5.             File file = new File("javaio-appendfile.txt");  
  6.             // if file doesnt exists, then create it  
  7.             if (!file.exists()) {  
  8.                 file.createNewFile();  
  9.             }  
  10.   
  11.             // true = append file  
  12.             FileWriter fileWritter = new FileWriter(file.getName(), true);  
  13.             fileWritter.write(data);  
  14.             fileWritter.close();  
  15.   
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.   
  20.     }  

 

  1. public static void writeByBufferedReader() {  
  2.         try {  
  3.   
  4.             String content = "This is the content to write into file";  
  5.             File file = new File("/users/mkyong/filename.txt");  
  6.             // if file doesnt exists, then create it  
  7.             if (!file.exists()) {  
  8.                 file.createNewFile();  
  9.             }  
  10.             FileWriter fw = new FileWriter(file, true);  
  11.             BufferedWriter bw = new BufferedWriter(fw);  
  12.             bw.write(content);  
  13.             bw.flush();  
  14.             bw.close();  
  15.   
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值