簡體   English   中英

用Java從txt寫入特定行

[英]Write in a specific line from txt in Java

這是我的代碼:

public static void modif(String name, String color, int k)
    {
        try {
        File input= new File("file1.txt");
        File output= new File("temp.txt");

        String correctLine = (String) FileUtils.readLines(input).get(k-3);

        BufferedReader br = new BufferedReader(new FileReader(input));
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        String line="";


        while ((ligne = br.readLine()) != null){

            String lineConcat = line  + "\n";

         if(ligne.startsWith("\""+name+"\"")){

         System.out.println(correctLine); // i can display the line to change

             bw.write("\"" + name + "\"" + ": " + "\"" + color + "\"" + "\n"); // this is the way i replace my line
             System.out.println("Awesome !");
             bw.flush();
         }else{
             bw.write(lineConcat);
             bw.flush();
         }
        }
        bw.close();
        br.close();

        output.renameTo(new File("file2.txt"));

        } catch (IOException e) {
            e.printStackTrace();
        }

}

基本上,我想在檢測到條件時替換特定行。 我知道要更改的行,但我不知道如何替換。 因為現在我只能檢測到行的開頭,並且不能更改當前行,而是更改當前行。

我試圖使用FileUtils.writeStringToFile,但是它不起作用,所以我在這里尋求幫助:(

多謝你們 !

編輯:我的輸入是這樣的:

 { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [ 6.07721374522497, 43.08716485432151 ], [ 6.051202617426629, 43.07969629888752 ], [ 6.032563261594762, 43.07758570385911 ] ] ] }, "properties": { "_storage_options": { "color": "Orange" }, "name": "CARQUEIRANNE" } } 

我實際上在做的是當我找到“名稱”行:“ CAREQUEIRANNE”時,我想替換他的color屬性,所以我必須轉到當前行-3,而我真的不知道該怎么做

編輯2:

我的方法被這樣調用:

  BufferedReader in2 = new BufferedReader(new FileReader("file1.txt")); String line; String lineConcat = null; int k=1; while ((line = in2.readLine()) != null) { lineConcat = line + "\\n"; String myPattern = tabCity[21]; Pattern p = Pattern.compile(myPattern); Matcher m = p.matcher(lineConcat); //Matcher m2 = p2.matcher(lineConcat); if (m.find()){ System.out.println("Found !"); System.out.println(k); modif("color", "Orange", k); } else { System.out.println("Not Found"); } k++; } in2.close(); 

當我發現我的圖案與文件中的研究相匹配時,我調用函數以替換城市的顏色屬性

編輯3:

@BrettWalker這是新代碼:

 public static void modif(String nom, String color, int k) { try { File input = new File("file1.txt"); File output = new File("temp.txt"); BufferedReader br = new BufferedReader(new FileReader(input)); BufferedWriter bw = new BufferedWriter(new FileWriter(output)); String line=""; Stack st = new Stack(); st.push(br.readLine()); st.push(br.readLine()); st.push(br.readLine()); while ((ligne = br.readLine()) != null){ String ligneConcat = ligne + "\\n"; st.push(ligneConcat); String oldLine = (String) st.pop(); if(ligne.startsWith("\\""+nom+"\\"")){ oldLine = "\\"" + nom + "\\"" + ": " + "\\"" + color + "\\"" + "\\n"; } bw.write(oldLine); } bw.write((int) st.pop()); bw.write((int) st.pop()); bw.write((int) st.pop()); bw.close(); br.close(); output.renameTo(new File("file2.txt")); } catch (IOException e) { e.printStackTrace(); } } 

您需要使用隊列(緩沖區)在Java中臨時保存文件的一小段,以允許您檢測時間並允許更改相應的行。

無需寫出該行,而是立即將其推入隊列(緩沖區)並從隊列中彈出下一項。 如果剛彈出的那個要更改,則將修改后的行寫入文件,否則將原始行寫入文件。

較差的偽代碼有助於表達這一想法。

// Open up the readers/writers

// Prime the queue with the first few line of the file.
// Need to add safeguard to protect against small files!!
queue.push(br.readLine());
queue.push(br.readLine());
queue.push(br.readLine());

while ((line = br.readLine()) != null) {
    String lineConcat = line  + "\n";

    queue.push(lineConcat);

    String oldLine = queue.pop();

    if (line.startsWith("\""+name+"\"")) {
         oldLine = < Modify oldLine >
    }

    bw.write(oldLine)
}

// Empty the queue
bw.write(queue.pop());
bw.write(queue.pop());
bw.write(queue.pop());

// Close the readers/writer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM