java建立文本、檔案、讀檔案、寫檔案

來源:互聯網
上載者:User

標籤:java   write   dir   stat   new   建立   imp   generate   int   

 1 package Head18; 2  3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 10 public class Mkdirs_FileRW {11     public static void main(String [] args) {12         String a0="D:\\abc";13         String a1=a0+"\\abc.txt";14         File file0= new File(a0); 15         File file = new File(a1);16         file0.mkdirs();17         //建立檔案    18         if (!file.exists()){19             try {20                 file.createNewFile();21                 System.out.println("檔案建立成功");22             } catch (IOException e) {23             24                 e.printStackTrace();25             }26         }else{27             System.out.println("檔案已經存在");28         29         }30         31         //寫檔案32         try {33             FileWriter fWriter = new FileWriter(a1, true);34             fWriter.write("測試0"+"\r\n");;35             fWriter.flush();36             fWriter.close();37         } catch (IOException e) {38             // TODO Auto-generated catch block39             e.printStackTrace();40         }41         42         //讀檔案43         try {44             FileReader fReader=new FileReader(file);45             //字元流46             BufferedReader bfReader=new BufferedReader(fReader);47             try {48                 String bf=null;49                 while((bf=bfReader.readLine())!=null){50                     System.out.println(bf);51                 }52                 53             } catch (IOException e) {54                 e.printStackTrace();55             }56         } catch (FileNotFoundException e) {57             e.printStackTrace();58         }59         60         61         62         63         64     }65 }

 

java建立文本、檔案、讀檔案、寫檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.