In Java, how do I overwrite a specific part of a line in a file? -


this question has answer here:

i have csv file thats formatted id,text. here example:

hellotext,how goodbyemessage,some new text change errormessage,oops went wrong 

now lets example want edit text part of goodbyemessage some new text change, see later

the resulting csv should this:

hellotext,how goodbyemessage,see later errormessage,oops went wrong 

i have code can write file when code finishes executing, resulting csv file:

hellotext,how goodbyemessage,some new text change errormessage,oops went wronggoodbyemessage,see later 

i know occurring because set filewriter's append value true. if don't gets wiped.

i have tried using filewriter.newline() make better not trying achieve. still want same number of line in file. myapp.java

public static void main(string[] args) throws filenotfoundexception, ioexception {     propswriter pw = new propswriter("test_props.txt");     pw.updateelementtext("goodbyemessage", "see later"); } 

propswriter.java

/**  * updates text of given element in properties file.  *   * @param id id of element  * @param newtext text replace original text.  *   * @throws ioexception if i/o error occurs  */ public void updateelementtext(string id, string newtext) throws ioexception {     assertions.checknotnull(id, "id must not null.");     assertions.checknotnull(id, "id must not empty string.");     file file = new file(pathname);     bufferedreader br = new bufferedreader(new filereader(file));     bufferedwriter wr = new bufferedwriter(new filewriter(file, true));     try {         string line;          while((line = br.readline()) != null) {             if(line.contains(id)) {                 //returns true                 system.out.println("is line there: " + line.contains(id));                  //returns hellotext                 system.out.println("id: " + extractid(line));                  //returns how                 system.out.println("text: " + extracttext(line));                  //returns new text change                 system.out.println("new_text: " + newtext);                  // trying replace old text                  // new text came main method.                 line = line.replaceall(extracttext(line), newtext);                  //wr.newline();                 wr.write(line);                          }         }     } catch(ioexception e) {         e.printstacktrace();     } {         wr.close();     } }  /**  * gets id part of line stored in   * properties file.  *   * @param element element id got from.  * @return string representation of id.  */ private static string extractid(string line) {     final int commaoccurence = getfirstcommaoccurrence(line);     return line.substring(0, commaoccurence); }  /**  * gets text part of line stored in   * properties file.  *   * @param element element text got from.  * @return string representation of text.  */ private static string extracttext(string line) {     final int commaoccurence = getfirstcommaoccurrence(line);     return line.substring(commaoccurence + 1, line.length()); }  /**  * gets first occurrence of comma in given line of text file.  * @param element  * @return  */ private static int getfirstcommaoccurrence(string line) {     return line.indexof(","); } 

you said it. not set filewriter true (as appends new stuff). need read whole file, save lines (for example in list<string>). manipulate data. , after rewrite whole text (for example using said list<string>).

in above code first replace true false:

bufferedwriter wr = new bufferedwriter(new filewriter(file, false)); 

second, write file, other lines lost:

if(line.contains(id)) {     ...     line = line.replaceall(extracttext(line), newtext);  } wr.write(line); 

however, if have big file , don't want rewrite all lines, can go more low-level filewriter, randomaccessfile. concept allows start file manipulation @ given position without rewriting before position. can search line (or jump, if know where), make change. need rewrite comes after change. here's usage example: randomaccessfile example

there no better solution platform dependent. more specific, depends on used filesystem, example ntfs or fat32 , on.
in general, store file splitting several packages. packages saved on hard drive, puts them best fit in. file system saves pointer each start package of file in master table. every package saves pointer next packages , on until eof reached.

you see, easy change in middle without changing before. need change stuff comes after, can not control how os splits new data packages. if go very low-level, may control single packages , inject data without changing after. don't think want :)


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -