简体   繁体   中英

How to edit an specific word from a I/O File Java

So I'm trying to modify a line from my textFile but when i put the new values and print the result, it prints in a whole new line at the bottom as if it were the last line instead of replacing the line i want to replace.

 try {


                 LineNumberReader readers = new LineNumberReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
                 PrintWriter output = new PrintWriter(new FileOutputStream(file, true));
                 boolean finder = false;

                 String oldContent = "";

                 while((target = readers.readLine()) != null)
                 {
                     if(target.contains(CL.getID()))
                     {
                         finder = true;
                         target = readers.readLine() + 1;
                         System.out.println("Escriba el Nuevo presupuesto");
                         CL.setPresupuesto(reader.next());
                         output.write("Presupuesto: " + CL.getID() + "$");

                         System.out.println("Nuevo Presupuesto");



                         output.close();
                         readers.close();
                         break; 
                     }


                 }

                 if(finder == false)
                 {
                     System.out.println("ID no encontrado!!");
                 }


             }catch(IOException ex) {

             }

I don't think there's any such way to delete a specific line in the original file. What you can try to do is create a new file without that specific line. This would create a new file but serve the purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM