简体   繁体   中英

Existing file slowing down java program

I'm running a few methods together that take in many text files, read their contents, then write things about their contents to a new file. The problem I have is that when the file exist, the program is very slow. If I delete the file and run the program it then is very fast. I'm using BufferedReader and BufferedWriter for my I/O. I feel like there's a simple answer that I'm just not finding. Thanks in advance! I'd rather not post code if possible, Sorry!

EDIT:

here's very generally what's going on

File path= new File("some path");
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(path, false));
            //do some string manipulation
            writer.append(string);
            writer.newLine();
                ...
            //once done
            writer.close();
        }catch(IOException e) {
            //... handle this ... 
        } 

The problem is that when this file exists, everything is slow. If it doesn't then it is fast.

I would revisit whatever it is you're doing when you say " //do some string manipulation".

Here is what I noticed with > 1000 iterations:

  • the time it takes to get the file handle and close the writer generally remain the same
  • the inner loop operation with the string "ABCDEFGHIJKLMNOPQRSTUVWXYZ" has a mean variance of 98ms
  • the same inner loop operation w/ a string quadruple that string's size causes much larger variety in terms of operation time. Sometimes the program finished in 2 seconds, sometimes it was 20 seconds.

I also did a version of this test where the file was always deleted first. It made no difference. Here's the code I ran:

public static void main(String[] args) {

    long s = System.currentTimeMillis();
    File path = new File("output.txt");

    long stop = System.currentTimeMillis();
    System.out.println("handle acquired " + (stop - s) );

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(path, false));
        //do some string manipulation
        s = System.currentTimeMillis();
        for (int i =0; i < 10000000; i++) {
            String string = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
            writer.append(string);
            writer.newLine();
        }
        stop = System.currentTimeMillis();
        System.out.println("loop end " + (stop - s) );

        s = System.currentTimeMillis();
        writer.close();
        stop = System.currentTimeMillis();
        System.out.println("writer closed " + (stop - s) );
    }catch(IOException e) {
        //... handle this ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM