第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用Java將文本附加到一個(gè)目錄中的多個(gè)文件

如何使用Java將文本附加到一個(gè)目錄中的多個(gè)文件

梵蒂岡之花 2023-06-08 19:16:24
我有很多包含一些數(shù)據(jù)的 .txt 文件1.txt 2.txt 3.txt ...我想添加相同的文本,例如"hello world"添加到同一目錄中的每個(gè) txt 文件。我知道在那種情況下如何處理一個(gè)文件,但如何處理多個(gè)文件呢?我必須使用 Java 來(lái)做到這一點(diǎn)......
查看完整描述

3 回答

?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以java.nio結(jié)合使用 Java 8 功能來(lái)列出文件并為每個(gè)文件執(zhí)行一些操作。有一種方法可以將文本附加到文件。


請(qǐng)參閱此示例并閱讀代碼中的一些注釋:


public static void main(String[] args) {

    // define the directory that contains the text files

    String dir = "U:\\workspace\\git\\ZZ--Temp\\TextFiles";

    Path dirPath = Paths.get(dir);

    // predefine some lines to be appended to every file

    List<String> linesToBeAppended = new ArrayList<>();

    linesToBeAppended.add("Hello new line in the file!");


    try {

        // go through all files in the directory (tested with .txt files only)

        Files.list(dirPath)

            // filter only files

            .filter(Files::isRegularFile)

            .forEach(filePath -> {

                try {

                    // append the predefined text to the file

                    Files.write(filePath, linesToBeAppended, StandardOpenOption.APPEND);

                } catch (IOException e) {

                    System.err.println("Could not append text to file " 

                            + filePath.toAbsolutePath().toString());

                    e.printStackTrace();

                }

            });

    } catch (IOException e) {

        System.err.println("Could not list files in " 

                + dirPath.toAbsolutePath().toString());

        e.printStackTrace();

    }

}

不幸的是,由于 Java 8 功能的不同范圍,嵌套try-是必需的。它很丑陋,但優(yōu)點(diǎn)是您可以通過列出文件或訪問文件來(lái)區(qū)分拋出的 s 。catchforEachException


編輯

如果要在文件中添加新的第一行,則必須讀取并重寫文件。請(qǐng)參閱此示例,它與第一個(gè)示例略有不同:


public static void main(String[] args) {

    // define the directory that contains the text files

    String dir = "U:\\workspace\\git\\ZZ--Temp\\TextFiles";

    Path dirPath = Paths.get(dir);


    try {

        // go through all files in the directory (tested with .txt files only)

        Files.list(dirPath)

            // filter only files

            .filter(Files::isRegularFile)

            .forEach(filePath -> {

                // predefine some lines to be appended to every file

                List<String> linesToBeAppended = new ArrayList<>();

                // add the first line as predefined first line

                linesToBeAppended.add("Hello another line in the file!");


                try {

                    // then read the file and add its lines to the list with

                    // that already contains the new first line

                    linesToBeAppended.addAll(Files.readAllLines(filePath));

                    // append the extended text to the file (again),

                    // but this time overwrite the content

                    Files.write(filePath, linesToBeAppended,

                                StandardOpenOption.TRUNCATE_EXISTING);

                } catch (IOException e) {

                    System.err.println("Could not append text to file " 

                            + filePath.toAbsolutePath().toString());

                    e.printStackTrace();

                }

            });

    } catch (IOException e) {

        System.err.println("Could not list files in " 

                + dirPath.toAbsolutePath().toString());

        e.printStackTrace();

    }

}

另一個(gè)重要的區(qū)別是 中的標(biāo)志Files.write,它不再APPEND存在,但是TRUNCATE_EXISTING因?yàn)槟鷮⑽募x入String表示行的列表中,然后將該集合添加到已經(jīng)包含新的第一行的集合中。之后,您只需再次編寫這些行,包括新的第一行。


查看完整回答
反對(duì) 回復(fù) 2023-06-08
?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

我認(rèn)為您想要做的是列出目錄中的所有文件,然后處理每個(gè)文件。如果是這樣的話你可以做


File[] children = dir.listFiles();

for (File child: children) {

    if (child.isFile()) {

        // append text

    }

}

我已經(jīng)省略了附加數(shù)據(jù)的代碼,因?yàn)槟f(shuō)您已經(jīng)知道該怎么做。在這一點(diǎn)上,它只是將代碼應(yīng)用于每個(gè)文件的情況


查看完整回答
反對(duì) 回復(fù) 2023-06-08
?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以創(chuàng)建一個(gè)包含目錄的文件:


File directory = new File("pathOfYourDirectory");

然后你可以得到所有的從屬文件和目錄:


File[] subDirectories = directory.listFiles();

現(xiàn)在你可以遍歷這個(gè)數(shù)組了。但是你應(yīng)該檢查每個(gè)文件是否是一個(gè)文件。然后您可以獲取此文件并附加文本。


for (File subDir: subDirectories ) {

    if (subDir.isFile()) {

        // do your stuff

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-06-08
  • 3 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)