so with this piece of code I’ve written, I was wondering why I am not receiving the type of output I want…
//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);
}
}
The original file essentially looks like…
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,
The problem is, the output file looks exactly the same, but I want everything to be copied EXCEPT the “null(u)” string from the original document. I am just wondering, is my code correct in terms of what I want it to do, and how should I re-work my delimiter to ignore the “null (U)” references in the original file?
Thanks for any offered input.
Changing
outputFilewithin the loop has no effect onwriter, you also need to re-create that in the same if (add a second line after the re-assign); and close the old writer before you open the new one.