所以我写了这段代码,我想知道为什么我没有收到我想要的输出类型......
//Method to copy over a file to a new file using a delimiter and breaking up the file if it is more than 15 images
public static void copyFile(File extractedInfo)
{
try
{
//Counter to track the # of images/lines
int c = 0;
File inputFile = extractedInfo;
File outputFile = new File(extractedInfo.getParentFile() + "/Extracted Info " + index2++ + ".txt");
Scanner reader = new Scanner(inputFile);
reader.useDelimiter("null(U) ");
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
//While there is a next line...
while (reader.hasNextLine())
{
//If the document has 15 images, create a new file to write to
if (c%15 == 0)
{
writer.close();
outputFile = new File(extractedInfo.getParentFile() + "/Extracted Info " + ((index2++) + 1) + ".txt");
writer = new BufferedWriter(new FileWriter(outputFile));
}
//Else, normally, just write out each individual character from the original file.
writer.append(reader.next());
c++;
}
reader.close();
writer.close();
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
原始文件基本上看起来像......
C:\Documents and Settings\workspace\Extracted Items\image2.jpeg;image0;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image3.jpeg;image1;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image4.jpeg;image2;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image5.jpeg;image3;null(U) keyword1, keyword2, keyword3, keyword4,
问题是,输出文件看起来完全一样,但我想要复制所有内容除了原始文档中的“null(u)”字符串。我只是想知道,我的代码是否符合我的要求,我应该如何重新设置分隔符以忽略原始文件中的“null(U)”引用?
感谢任何提供的意见。
答案 0 :(得分:3)
在循环中更改outputFile
对writer
没有影响,您还需要在相同的if中重新创建它(在重新分配后添加第二行);在打开新作家之前关闭旧作家。
答案 1 :(得分:1)
我还不确定你想要做什么,但行
writer.append(reader.next());
似乎有一个缺陷。作者可能仍然引用原始outputFile而不是新文件。
所以,你可能需要这样做:
if (c%15 == 0)
{
outputFile = new File(extractedInfo.getParentFile() + "/Extracted Info " + ((index2++) + 1) + ".txt");
writer = new BufferedWriter(new FileWriter(outputFile));
}
答案 2 :(得分:0)
我以为我会发布这段代码,以防它可能对有类似问题的其他人有所帮助。
//Method to copy over a file to a new file using a delimiter and breaking up the file if it is more than 15 images
public static void copyFile(File extractedInfo)
{
try
{
//Counter to track the # of images/lines
int c = 0;
File inputFile = extractedInfo;
File outputFile = new File(extractedInfo.getParentFile() + "/Extracted Info " + index2 + ".txt");
Scanner reader = new Scanner(inputFile);
reader.useDelimiter("null\\(U\\) ");
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
//While there is a next line...
while (reader.hasNextLine())
{
//If the document has 15 images, create a new file to write to
if (c%15 == 0)
{
writer.close();
outputFile = new File(extractedInfo.getParentFile() + "/Extracted Info " + index2++ + ".txt");
writer = new BufferedWriter(new FileWriter(outputFile));
writer.append(reader.next());
c++;
}
else
{
//Else, normally, just write out each individual character from the original file.
writer.append(reader.next());
c++;
}
}
reader.close();
writer.close();
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}