【问题标题】:Using while loop to write a file使用while循环写入文件
【发布时间】:2014-02-11 20:34:26
【问题描述】:

这是我的第一篇文章,如果不是最好的格式,我深表歉意。我正在编写代码来导入文档,读取文档中的一行,然后反转每个单词中的字母。新词将被打印到一个新文件中。例如,“Jon 123”将被存储并写为“321 noJ”。我已经得到了工作的输入,但是行的书写有问题。程序只写入存储的最后一个字。

主要方法的删减代码如下:

    //get first line of text
    line = bw.readLine();

    //while string is not null
    while (line != null)
    {
        System.out.println ("Processing...");   //display message to show work being done

        tokenLine = lineToken(line);    //tokenize string

        //to prevent exception from no token found
        while (tokenLine.hasMoreTokens())
        {
            word = flipWord(tokenLine);     //get next token and reverse letters
            newLine = marginCheck(word);    //store or write line depending on margin

            flushClose(newLine);    //write and flush buffer then close file
        }


        //move to next line in file
        line = bw.readLine();
    }

    flushClose(newLine);    //write and flush buffer then close file

    //output completion message
    System.out.println("The new file has been written.");

相关方法如下:

public static StringTokenizer lineToken(String line) { //局部常量

    //local variables
    StringTokenizer tokenLine;      //store tokenized line

    /******************* Start lineToken Method ***************/

    tokenLine = new StringTokenizer(line);  //tokenize the current line of text

    return tokenLine;

}//end lineToken

公共静态字符串翻转字(StringTokenizer tokenLine) { //局部常量

    //local variables
    String word;        //store word for manipulation
    String revWord = "";        //store characters as they are flipped

    /******************************* Start flipWord Method******************/
    //store the next token as a string
    word = tokenLine.nextToken();

    //for each character store that character to create a new word
    for (int count = word.length(); count > 0; count--)     
        revWord = revWord + word.charAt(count - 1); //store the new word character by character

    return revWord;     //return the word reversed

}//end flipWord

public static String marginCheck(String revWord) 抛出异常 { //局部常量 最终 int 保证金 = 60; //每行最大字符数

    //local variables
    String newLine = "";            //store the new line
    FileWriter fw;      //writes to output file
    BufferedWriter bWriter;     //instantiate buffered writer object
    PrintWriter pw;     //instantiate print writer object
    String outFile  =  "RevWord.text";  //file to write to

    /************* Start marginCheck Method ************/
    //open the output file for writing  
    fw = new FileWriter(outFile);
    bWriter = new BufferedWriter(fw);
    pw = new PrintWriter(bWriter);

    //if the buffered line concatenated with the word is less than the margins
    if (newLine.length() + revWord.length() <= MARGIN)
        newLine = newLine + revWord + " ";      //the buffered line adds the word
    else
        //put an enline character at the end and write the line
        newLine = newLine + "\n";
        pw.println(newLine);

        //use this word as the first word of the next line
        newLine = revWord + " ";
    return newLine;     //return for use with flush
}//end marginCheck

public static void flushClose(String inLine) 抛出异常 { //局部常量

    //local variables
    FileWriter fw;      //writes to output file
    BufferedWriter bWriter;     //instantiate buffered writer object
    String outFile  =  "RevWord.text";  //file to write to

    /************ Start flushClose Method *********/
    fw = new FileWriter(outFile);
    bWriter = new BufferedWriter(fw);   //initialize writer object

    //write the last line to the output file then flush and close the buffer
    bWriter.write (inLine);
    bWriter.flush();
    bWriter.close();
}//end flushClose

【问题讨论】:

    标签: io while-loop filewriter


    【解决方案1】:

    我不确定,但我最好的猜测是,每次写入文件时,都会覆盖文件,而不是追加文件。

    试试 FileWriter(outFile,true);

    答案来自:http://www.mkyong.com/java/how-to-append-content-to-file-in-java/

    【讨论】:

    • 完美。打开了其他一些问题,但这解决了大问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2021-01-19
    相关资源
    最近更新 更多