使用while循环写入文件

时间:2014-02-11 20:34:26

标签: io while-loop filewriter

这是我的第一篇文章,所以如果它不是最好的格式,我道歉。我是学生编写代码来导入文档,读取文档中的一行,然后反转每个单词中的字母。新单词将打印到新文件中。例如,“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

public static String flipWord(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)抛出异常     {         //本地常量         final int MARGIN = 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

1 个答案:

答案 0 :(得分:2)

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

尝试FileWriter(outFile,true);

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