sunnyboy4205 sunnyboy4205 - 6 months ago 23
Java Question

Write to a specific line in a txt document java

I know that there are already some posts about this problem but I don't understand them.
My problem is that I want to find a line in a txt document with a name and I then want to change the next line to the content of a string.

This is what I tried:

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

}

}

Answer

May be you try it this way: read your file and keep each line in a list of strings and if you find the name you are looking for replace the next line you read. And then write the strings from that list back to your file. Example:

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