hello,
I'm having issues with writing changed value to a file. In my file I have a string which contains
I want to change one field foe example I want to replace 15:00 to 18:00 but when I run my code it deletes all the contents and writes in only the new value. But I want to keep the original content and only replace one field.Fri Jan 14 15:00:00 CET 1898feeffeg
Can you please help me to detect th error in my code? Thank you very much
} File file = new File("D:\\...\\events\\" + id + ".events.txt"); try { BufferedReader bufferedReader = null; FileInputStream fileInputStream = null; InputStreamReader inputStreamReader =null; try { fileInputStream = new FileInputStream(file); inputStreamReader = new InputStreamReader(fileInputStream); bufferedReader = new BufferedReader(inputStreamReader); } catch (FileNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } System.out.println("Searching for :" + Changefield + " in file: " + file.getName()); while((field = bufferedReader.readLine()) != null) { int index = field.indexOf(Changefield); System.out.println("Position of " + Changefield + "is at the position: " + index); System.out.println(Changefield.replaceFirst(Changefield, newValue)); System.out.println("Old value " + Changefield + " new value : " + newValue); } Path from = Paths.get(String.valueOf(file)); Path to = Paths.get("D:\\...\\events\\" + id + ".events-Temp.txt"); CopyOption[] option = new CopyOption[]{ StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES }; for (Path path : Files.copy(from, to, option)) { } try{ FileWriter fileWriter1 = new FileWriter(file); writer = new BufferedWriter(fileWriter1); writer.write(newValue); // writer.flush(); }catch(Exception e){ System.err.println("Error: " +e.getMessage()); }finally { try{ writer.close(); }catch(IOException e){ System.err.println("Error " + e.getMessage()); } } } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }