2016-05-30 20 views
-1

この問題についての記事は既にありますが、わかりません。 私の問題は、名前を持つtxtドキュメント内の行を探して、文字列の内容に次の行を変更したいということです。txtドキュメントの特定の行に書き込むjava

これは私が試したものです:

public void saveDocument(String name) { 

    String documentToSave = textArea1.getText(); 

    File file = new File("documents.txt"); 

    Scanner scanner; 

    BufferedWriter bw; 

    try { 

     scanner = new Scanner(file); 

     bw = new BufferedWriter(new FileWriter(file)); 

     while(scanner.hasNextLine()) { 

      if(scanner.nextLine().equals(name)) { 

       if(scanner.hasNextLine()) bw.write(scanner.nextLine() + "\n"); 
       bw.write(documentToSave + "\n"); 
       if(scanner.hasNextLine()) scanner.nextLine(); 

      } 

      if(scanner.hasNextLine()) bw.write(scanner.nextLine() + "\n"); 

     } 

     bw.flush(); 
     bw.close(); 

    } catch (IOException e) { 

     e.printStackTrace(); 

    } 

} 
+1

まずその後、頼む何かを試してみてください。 –

+1

Plsはあなたがまだ書いたコードを投稿します – Blobonat

+0

あなたが必要とするものを見てみましょう。これらの例について質問してください。あなたの問題が何であるかを細かく把握するのは難しいです。 – Sammyrulez

答えて

0

は、あなたがそれをこのようにしてみてください可能性があります。そしてそのリストの文字列をファイルに書き戻します。例:

public class NewClass { 
    public static void main(String[] args) { 
     List<String> list = new ArrayList<String>(); 
     list = readFile("uzochi"); 
     writeToFile(list); 
    } 

public static List<String> readFile(String name){ 
    List<String> list = new ArrayList<String>(); 
    try { 
     FileReader reader = new FileReader("C:\\users\\uzochi\\desktop\\txt.txt"); 
     BufferedReader bufferedReader = new BufferedReader(reader); 

     String line; 
     boolean nameFound = false; 

     while ((line = bufferedReader.readLine()) != null) { 
      if(line.equalsIgnoreCase(name)){ 
       nameFound = true; 
       System.out.println("searched name: "+line); 
      } 
       if(nameFound){       
        list.add(line); 

        line = bufferedReader.readLine(); 
        System.out.println("line to replace: " + line); 
        line = "another string"; 
        System.out.println("replaced line: "+line); 
        list.add(line); 
        nameFound = false; 
       } 
       else{ 
        list.add(line); 
       } 
     } 
     reader.close(); 

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

public static void writeToFile(List<String> list){ 
    try { 
      FileWriter writer = new FileWriter("C:\\users\\uzochi\\desktop\\txt.txt", false); 
      BufferedWriter bufferedWriter = new BufferedWriter(writer); 
      for(String s: list){ 
       bufferedWriter.write(s); 
       bufferedWriter.newLine(); 
      } 

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

}

txt.txt

hallo 
hello 
hola 
uzochi 
world 
java 
print 
0

このコードは、ファイル全体を読み込み、名前の行を交換する必要があります。

while(scanner.hasNextLine()) { 
    String line = scanner.nextLine(); 

    if(line.equals(name)) { 
     bw.write(documentToSave + "\n"); 
    } else { 
     bw.write(line + "\n"); 
    } 
} 
+0

thx私はこれを試します:) – sunnyboy4205

+0

ようこそ。 –

0

このプログラムは、指定された行の後の行を置き換えます。予想されるI/Oと使用法を定義できる場合など、私たちの仕事はもっと必要です。これでファイルから読み込んで行を置き換えますが、行の内容の代わりに行番号を使用して、置き換える行をマークすることができます。

import java.io.*; 
public class FileLineReplace { 
public static void replaceSelected(String replaceWith, String type) { 
    try { 
    String input2 = ""; 
    boolean replace = false; 

    String input = ""; 

    try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) { 
    for (String line; 
    (line = br.readLine()) != null;) { 
    // process the line. 


    System.out.println(line); // check that it's inputted right 


    if (replace) { 

     line = "*** REPLACED ***"; 
     replace = false; 
    } 

    if (line.indexOf("replaceNextLine") > -1) { 

     replace = true; 
    } 


    input2 = input2 += (line + "\n"); 

    } 
    // line is not visible here. 
    } 






    // check if the new input is right 
    System.out.println("----------------------------------" + '\n' + input2); 

    //   write the new String with the replaced line OVER the same file 
    FileOutputStream fileOut = new FileOutputStream("data.txt"); 
    fileOut.write(input2.getBytes()); 
    fileOut.close(); 

    } catch (Exception e) { 
    System.out.println("Problem reading file."); 
    e.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    replaceSelected("1 adam 20 M", "foobar"); 
} 
} 

あなたがコードを実行した場合、それは "replaceNextLine" である行の後に次の行を置き換えます:私の後

$ java FileLineReplace 
asd 
zxc 
xcv 
replaceNextLine 
wer 
qwe 
---------------------------------- 
asd 
zxc 
xcv 
replaceNextLine 
*** REPLACED *** 
qwe 

私のテストファイルがある(た)

asd 
zxc 
xcv 
replaceNextLine 
wer 
qwe 

プログラムを実行すると、ファイルは次のようになり、指定された行の後の行が置き換えられます。あなたはあなたが読んで次の行を置き換える探している名前を見つけた場合、あなたのファイルを読み込み、文字列のリスト内の各ラインを維持し、:

asd 
zxc 
xcv 
replaceNextLine 
*** REPLACED *** 
qwe 
+0

質問を解決する場合は、チェックマークを使用して回答を受け入れることを忘れないでください申し訳ありませんが、私はあなたのコードを理解していません。あなたは決して使用されない変数を宣言していますか? – sunnyboy4205

+0

私は改善されたコードで答えを更新し、それが何をしているのかを説明しました。 –

関連する問題