Io 使用while循环写入文件
这是我的第一篇帖子,如果不是最好的格式,我很抱歉。我正在写一个代码来导入一个文档,读取文档中的一行,然后反转每个单词中的字母。新单词将被打印到新文件中。例如,“Jon 123”将被存储并写入“321 noJ”。我已经得到了工作的输入,但是这行的书写有问题。程序只写入存储的最后一个字 主要方法代码摘要如下:Io 使用while循环写入文件,io,while-loop,filewriter,Io,While Loop,Filewriter,这是我的第一篇帖子,如果不是最好的格式,我很抱歉。我正在写一个代码来导入一个文档,读取文档中的一行,然后反转每个单词中的字母。新单词将被打印到新文件中。例如,“Jon 123”将被存储并写入“321 noJ”。我已经得到了工作的输入,但是这行的书写有问题。程序只写入存储的最后一个字 主要方法代码摘要如下: //get first line of text line = bw.readLine(); //while string is not null 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.");
有关方法如下:
//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
//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
//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
公共静态字符串flipWord(StringTokenizer标记行)
{
//局部常数
//local variables
StringTokenizer tokenLine; //store tokenized line
/******************* Start lineToken Method ***************/
tokenLine = new StringTokenizer(line); //tokenize the current line of text
return tokenLine;
}//end lineToken
//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
//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
公共静态字符串marginCheck(字符串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
我不确定,但我最好的猜测是,每次写入该文件时,都会覆盖该文件,而不是附加到该文件 尝试FileWriter(outFile,true)
回答:完美。解决了一些其他问题,但解决了大问题。谢谢。