這篇文章給大家分享的是java中文件的寫入和讀出的方法。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
石嘴山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!一、文檔讀取
1、將文件讀取為String
public static String TxtToString(File file) { String result = ""; try { BufferedReader br = new BufferedReader(new FileReader(file)); //構造一個BufferedReader類來讀取文件 String s = null; while ((s = br.readLine()) != null) {//使用readLine方法,一次讀一行 result = result + "\n" + s; } br.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
2、將文件讀取為List集合(按行)
public static List<String> TxtToStringList(File file) { List<String> result = new ArrayList<>(); try { if (!file.exists()){ return null; } BufferedReader br = new BufferedReader(new FileReader(file)); //構造一個BufferedReader類來讀取文件 String s = null; while ((s = br.readLine()) != null) {//使用readLine方法,一次讀一行 result.add(s); } br.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
二、Java存儲文件
1、將list按行寫入到txt文件中
public static void writeFileContext(List<String> strings) throws Exception { File file = new File("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\WordLibrary_index"); //如果沒有文件就創(chuàng)建 if (!file.isFile()) { file.createNewFile(); } BufferedWriter writer = new BufferedWriter(new FileWriter ("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\WordLibrary_index")); for (String l:strings){ writer.write(l + "\r\n"); } writer.close(); }
2、按照名字將string類型的集合存入文件
public static void writeFileContext_Find(List<String> strings,String name) throws Exception { File file = new File("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\wordIndex"); //如果沒有文件就創(chuàng)建 if (!file.isFile()) { file.createNewFile(); } BufferedWriter writer = new BufferedWriter(new FileWriter ("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\wordIndex\\"+name)); for (String l:strings){ writer.write(l + "\r\n"); } writer.close();
3、將Sting類型的list集合按文件地址存儲
public static void writeFileContext_Found(List<String> strings,String filename) throws Exception { File file = new File(filename); //如果沒有文件就創(chuàng)建 if (!file.isFile()) { file.createNewFile(); } BufferedWriter writer = new BufferedWriter(new FileWriter ("D:\\IntellijIDEAProject\\KeChenSheJi\\data\\file_index\\"+file.getName())); for (String l:strings){ writer.write(l + "\r\n"); } writer.close(); }
關于java中文件的寫入和讀出就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站題目:java中文件的寫入和讀出-創(chuàng)新互聯(lián)
鏈接地址:http://aaarwkj.com/article24/ddooje.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站策劃、品牌網(wǎng)站制作、外貿建站、小程序開發(fā)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容