java中文件的寫入和讀出

這篇文章給大家分享的是java中文件的寫入和讀出的方法。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。

創(chuàng)新互聯(lián)是專業(yè)的八步網(wǎng)站建設(shè)公司,八步接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行八步網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

一、文檔讀取

1、將文件讀取為String

public static String TxtToString(File file) {
    String result = "";
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        //構(gòu)造一個(gè)BufferedReader類來(lái)讀取文件
        String s = null;
        while ((s = br.readLine()) != null) {//使用readLine方法,一次讀一行
            result = result + "\n" + s;
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

2、將文件讀取為L(zhǎng)ist集合(按行)

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));
        //構(gòu)造一個(gè)BufferedReader類來(lái)讀取文件
        String s = null;
        while ((s = br.readLine()) != null) {//使用readLine方法,一次讀一行
            result.add(s);
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

二、Java存儲(chǔ)文件

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集合按文件地址存儲(chǔ)

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();
}

關(guān)于java中文件的寫入和讀出就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

分享題目:java中文件的寫入和讀出
本文地址:http://bm7419.com/article30/gegjpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站收錄、虛擬主機(jī)、手機(jī)網(wǎng)站建設(shè)、外貿(mào)建站、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站