I am not sure, but my best guess is that every time you write to the file, you are overwriting the file, instead of appending to it.
Try FileWriter(outFile,true);
Answer from: http://www.mkyong.com/java/how-to-append-content-to-file-in-java/
質問
This is my first post so i apologize if it's not the best format. I'm student writing a code to import a document, read a line in the document and then reverse the letters in each word. The new word will be printed into a new file. For example "Jon 123" would be stored and written as "321 noJ". I have gotten the input to work but there is a problem with the writing of the line. The program is only writing the last word that is stored.
The abridged main method code is as follows:
//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.");
The relevant methods as follows:
public static StringTokenizer lineToken(String line) { //local constants
//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 constants
//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) throws Exception { //local constants final int MARGIN = 60; //maximum characters per line
//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) throws Exception { //local constants
//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
解決
I am not sure, but my best guess is that every time you write to the file, you are overwriting the file, instead of appending to it.
Try FileWriter(outFile,true);
Answer from: http://www.mkyong.com/java/how-to-append-content-to-file-in-java/