-1

I am trying to add almost 2lac lines in particular txt file(actually conf file) by java program. But it takes almost 112 min when number is only 189000! I write following code for that

import java.io.*;
public class Fileshandling_example {
    static long s1;
    static long e1;
    static long e2;
    static Fileshandling_example fhe=  new Fileshandling_example();
    public static void main(String args[]) {
        try {           
            s1 = System.nanoTime();         
            File file1 = new File("\example\mandar.txt");
            LineNumberReader lnr1 = new LineNumberReader(new FileReader(file1));
            BufferedReader br1 = new BufferedReader(new FileReader(file1));
            lnr1.skip(Long.MAX_VALUE);
            int a = 1;
            StringBuffer sb1 = new StringBuffer("[stations]");
            String sCurrentline1 = br1.readLine();
            while ((sCurrentline1 = br1.readLine()) != null) {
                a++;
                if (sCurrentline1.contentEquals(sb1) == true) {
                    int count = a;  
                    int arraycount = 100000;
                    for(int i =0; i< (arraycount+1); i++){
                        if(0 == (i%10000)){
                            e2 = System.nanoTime();
                            System.out.println("Time  = "+(e2-s1));
                        }
                        String abc ="extern => 00"+(1000 + (arraycount-i))+",1,Wait(0.05)";
                        fhe.insertintoExtensions(file1, (count+1),abc);
                    }
                }       
            }               
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        e1 = System.nanoTime();
        System.out.println("Time  = "+(e1-s1));
    }
    public void insertintoExtensions(File inFile1, int lineno, String s1)throws Exception {
        File outFile1 = new File("\example\111.tmp");
        FileInputStream fis  = new FileInputStream(inFile1);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));        
        FileOutputStream fos = new FileOutputStream(outFile1);
        PrintWriter out = new PrintWriter(fos);
        String thisLine = "";
        int i =1;
        while ((thisLine = in.readLine()) != null) {
            if(i == lineno) out.println(s1);
            out.println(thisLine);
            i++;
        }
        out.flush();
        out.close();
        in.close();
        inFile1.delete();
        outFile1.renameTo(inFile1);

    }
}

Can any one help me where i get wrong? I asked similar question coderanch but here i get clue very fast so i ask here also. Sorry for that (cross forum asking). Thanks.

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
Mandar Khire
  • 49
  • 2
  • 10

1 Answers1

0

You are loop 100,000 times for every '[stations]' found on "\example\mandar.txt":

if (sCurrentline1.contentEquals(sb1) == true) {
    int count = a; 
    int arraycount = 100000;
    for(int i =0; i< (arraycount+1); i++){

and call fhe.insertintoExtensions which loops "\example\mandar.txt" again to copy or the content of the line or the content of s1 parameter until the actual line number is reached:

while ((thisLine = in.readLine()) != null) {
    if(i == lineno) out.println(s1);
    out.println(thisLine);
    i++;
}

Try to improve you code and use BufferedWriter instead of PrintWriter.

DiogoSantana
  • 2,404
  • 2
  • 19
  • 24
  • Thanks DiogoSantana, i tried as you says with the help of http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/ But i can not do properly. can you help me more please? – Mandar Khire Mar 28 '13 at 05:36
  • Sure. But you need to do more homework. Debug your code, think a better way to accomplish your goal and when you get stucked then you can ask a more specific question. – DiogoSantana Mar 28 '13 at 05:40
  • You ask why this code is slow. The answer is there. How to make it fast it is just a matter of change the code logic to avoid 100,000 loop. – DiogoSantana Mar 28 '13 at 05:41
  • Thanks once again, I found http://stackoverflow.com/questions/1062113/fastest-way-to-write-huge-data-in-text-file-java I tried given code in few sec it write huge data, but it store in tmp folder. Can anyone help me to how to use in my code or by this given example how i write particular line at particular line number? – Mandar Khire Mar 28 '13 at 06:43