-1
我想将字符串写入Java中的文件。Java文件I/O故障
基本上,我应该从包含文本的文件(假定为字符串)读取。对于每行文本,如果字符串的格式为word1 word2(即,如果是两个单词),则必须将其写入一个文件(名称列表),而如果它不具有该格式,则为 ,我必须将它写入另一个文件,该文件应该被称为System.out(扩展名为.txt)
我有名单部分的列表工作得很好。这些名称会按照它们的意图写入输出文件,但是在写入System.out文件时,只会写入一行。
因此,格式化为普通句子(“不应该写入”等)的行会被跳过。我不确定这是为什么。下面是我的代码的一些摘录:
public static void processData(BufferedReader inputFile, PrintWriter outputFile)
{
// A modified version of
// the algorithm shown
// on the class page.
String inputLine = null;
try
{
// While loop that processes
// each line taken from the file.
// First it checks whether the
// line has the format word1 word2
while ((inputLine = inputFile.readLine()) != null)
{
// Splits the line into tokens,
// taking the delimiter to be
// anything that is not an
// upper or lowercase letter
// or the tab character (\t)
// in groups of at least one
// (using the regex [^a-zA-Z]+)
String[] tokens = inputLine.split("[^a-zA-Z\t]+");
// REMOVE LINES 215-216
System.out.println("inputLine is: " + inputLine);
System.out.println("The length of tokens is: " + tokens.length);
// If the line has the format
// word1 word2, the outputWriter
// method is invoked to process
// the words and write them to
// the output file
if (tokens.length == 2){
outputWriter(tokens, outputFile);
}
// Otherwise, the systemWriter
// method is invoked to write
// the line to the System.out file
// REMOVE LINE 234
else{
System.out.println("ENTERED SYSTEMWRITER TREE AT: " + inputLine);
systemWriter(tokens);
}
}
}