簡體   English   中英

在java中寫入和讀取文件

[英]write and read in file in java

我正在嘗試從文件中讀取,獲取每一行的長度,將長度寫入另一個文件,然后打印第二個文件以查看寫入結果,但是當我打開第二個文件時,結果並不完全正確我想要的。 運行這段代碼后,文件中有很多數字:

    String line = null;

    boolean flag = false;

    BufferedReader bf = new BufferedReader(new FileReader("c:\\lm_giga_5k_nvp_2gram.arpa"));

    BufferedWriter index = new BufferedWriter(new FileWriter("c:\\index.txt"));
    int l;

    int counter=0;

     while (( line = bf.readLine()) != null)

     {

        l=line.length();

        index.write( l + "\n" );

     }

     BufferedReader bf1 = new BufferedReader(new FileReader("c:\\index.txt"));

     String line1=null;

     while (( line1 = bf1.readLine()) != null)


     {
         System.out.println(line1);


     }

     bf.close();

     bf1.close();

請幫助我使用這個例子。 我關閉了索引,但仍然有同樣的問題。

注意:不要關注 arpa 文件,您可以將其鏡像為 txt 文件。

在其他地方打開之前,您應該關閉 index.txt; 或至少flush它:

... 
index.close();

BufferedReader bf1 = new BufferedReader(new FileReader("c:\\index.txt"));

String line1=null;
...

這里是 Write 和 Read 方法。 您可以自定義它們以滿足您的需求。

public boolean writePublic(String strWrittenString, String writeFileName, String writeEncoding, boolean appendString) {

    try {
        //System.out.println("Writing to file named " + writeFileName + " ...");
        Writer out = new OutputStreamWriter(new FileOutputStream(writeFileName, appendString), writeEncoding);
        try {
            out.write(strWrittenString);

        } finally {
            out.close();
        }

        //System.out.println("Writing to file named " + writeFileName + "- success.");
        return true;
    } catch (IOException ioe) {
        System.out.println("file named " + writeFileName + "-Failed. cause: " + ioe.getMessage());
        return false;
    } catch (Exception e23) {
        System.out.println("file named " + writeFileName + "-Failed. cause: " + e23.getMessage());
        return false;
    }

}

和讀取方法:

public static String readWithoutEncoding(String readFileName) {
    StringBuilder text = new StringBuilder();
    try {
        //System.out.println("Reading from file named " + readFileName + " ...");

        String NL = System.getProperty("line.separator");
        Scanner scanner = new Scanner(new FileInputStream(readFileName));
        try {
            while (scanner.hasNextLine()) {
                text.append(scanner.nextLine() + NL);
            }
        } finally {
            scanner.close();
        }
    // System.out.println("Text read in: " + text);
    //System.out.println("Reading from file named " + readFileName + "- success.");
    } catch (IOException ioe) {
        System.out.println("file named " + readFileName + "-Failed. cause: " + ioe.getMessage());
    } catch (Exception e23) {
        System.out.println("file named " + readFileName + "-Failed. cause: " + e23.getMessage());


    } finally {
        return text.toString();
    }

}

另外不要忘記在包含上述方法的 Java 類的開頭放置所需的 Import 語句:

import java.io.*;
import java.util.Scanner;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM