这是我的第一篇文章,所以我道歉,如果它不是最好的格式。我是学生编写代码导入文档,在文档中读取一行,然后在每个单词中翻转字母。新单词将被打印到一个新文件中。例如,“Jon 123”将被存储并写为“321 noJ”。我已经得到了工作的意见,但是在编写该行时遇到了问题。该程序仅写入存储的最后一个单词。使用while循环写入文件
的简略主方法的代码如下:
//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.");
相关方法如下:
公共静态的StringTokenizer lineToken(字符串线) { //局部常量
//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
公共静态字符串marginCheck(字符串revWord)抛出异常 { //局部常量 最终诠释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
公共静态无效flushClose(字符串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
完美。打开了一些其他问题,但解决了大问题。谢谢。 – wizeman02