Hello,
I'm writing a piece of code with should modify my file. I have a string :
And I would like to change only one variable,but I'm not able to do it. Can you help me?Sat Jan 25 21:00:00 CET 25557feee
Here is the code:private void modify(String[] tokens) { if (tokens.length < 3) { // System.out.println("Illegal arguments"); System.out.println("modify [id] [field] [newValue] --> Modify the event with the given id and set the value of the specified field to [newValue]."); System.out.println(); return; } // TODO: Add additional input validity checks String id = tokens[0]; String field = tokens[1]; String newValue = tokens[2]; // TODO: Modify the given event locally as well as remote System.out.print("Your choice was: " + choose); // if(choose.equals(id)){ // System.out.println("You choosed : " + choose); // } 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{ BufferedReader reader = new BufferedReader(new FileReader(file)); FileWriter fileWriter1 = new FileWriter(file); writer = new BufferedWriter(fileWriter1); String line; while (( line = bufferedReader.readLine()) !=null) { System.out.print(line); } // 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. } }
Tank you for your help