开发者

Question About Copying Text File in Java

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 Scanne开发者_运维知识库r(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 outputFile within the loop has no effect on writer, 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.


I am not yet sure what exactly you want to do, but the line

        writer.append(reader.next());

Seems to have a flaw. The writer may still have reference to original outputFile instead of the new file.

So, you may have to do:

               if (c%15 == 0)
                {
                    outputFile = new File(extractedInfo.getParentFile() + "/Extracted Info " + ((index2++) + 1) + ".txt");
                    writer = new BufferedWriter(new FileWriter(outputFile));
                }


Just thought I would post this code in case it might be of help to someone else with a similar problem.

  //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);
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜