-1

我正在尝试通过 java 程序在特定的 txt 文件(实际上是 conf 文件)中添加几乎 2lac 行。但是当数字只有 189000 时,几乎需要 112 分钟!我为此编写了以下代码

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);

    }
}

任何人都可以帮助我在哪里出错?我问了类似的问题coderanch但在这里我很快就得到了线索,所以我也在这里问。抱歉(跨论坛询问)。谢谢。

4

1 回答 1

0

对于“\example\mandar.txt”中的每个“[stations]”,您循环 100,000 次:

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

并调用 fhe.insertintoExtensions 再次循环 "\example\mandar.txt" 以复制或复制该行的内容或 s1 参数的内容,直到达到实际的行号:

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

尝试改进您的代码并使用 BufferedWriter 而不是 PrintWriter。

于 2013-03-27T20:42:00.370 回答