2025-05-24

當逐行讀寫大於2G的文本文件時推薦使用以下代碼


void largeFileIO(String inputFile, String outputFile) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(inputFile)));
            BufferedReader in = new BufferedReader(new InputStreamReader(bis, “utf-8”), 10 * 1024 * 1024);//10M緩存
            FileWriter fw = new FileWriter(outputFile);
            while (in.ready()) {
                String line = in.readLine();
                fw.append(line + ”
“);
            }
            in.close();
            fw.flush();
            fw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *