how to replace strings in a file by other strings in java -
i have file every line contains strings :
usual,proper,complete,1,convenient,convenient,nonprob,recommended,recommend
i want replace every word such code :
1000, 100, 110, 110, 111, 001, 111, 111, 1000
here code used still incomplete :
public class codage { bufferedreader in; public codage() { try { in = new bufferedreader(new filereader("nursery.txt")); fileoutputstream fos2 = new fileoutputstream("nursery.txt"); dataoutputstream output = new dataoutputstream(fos2); string str; while (null != ((str = in.readline()))) { string delims = ","; string[] tokens = str.split(delims); int tokencount = tokens.length; (int j = 0; j < tokencount; j++) { if (tokens[j].equals("usual")) { tokens[j] = "1000"; output.writechars(tokens[j]); } //continue other cases } system.out.print(str); } in.close(); } catch (ioexception e) { system.out.println("there problem:" + e); } } public static void main(string[] args) { codage c = new codage(); } }
my code replace values incorrectly.
first of all, code wrote here not working, because when open outputstream exact file try read from, empty source file , statement in.readline()
returns null. if real code maybe problem.
i assume know should separate file opening read , 1 want write into. is, when open nursery.txt read from, should create outputstream temp file called nursery.tmp in same path, , after process finished, can delete nursery.txt , rename nursery.tmp nursery.txt.
also if you, wouldn't job using if-else structure. seams have unique keys like:
usual, proper, complete, convenient, convenient, nonprob, recommended, recommend
so maybe more convenient use map structure lookup replacing value:
usual, proper, complete, convenient, convenient, nonprob, recommended, recommend, ...
1000, 100, 110, 110, 111, 001, 111, 111, ...
but these guesses , know how manage business logic.
after part think better idea create output data string lines , write them line line nursery.tmp:
public class codage { private bufferedreader in; private bufferedwriter out; private hashmap<string, string> replacingvaluesbykeys = new hashmap<string, string>(); public codage() { initialize(); } private void initialize() { // assumed have rule key "proper" goes "100" // initialize map between keys , replacing values: replacingvaluesbykeys.put("usual", "1000"); replacingvaluesbykeys.put("proper", "100"); replacingvaluesbykeys.put("complete", "110"); replacingvaluesbykeys.put("convenient", "110"); replacingvaluesbykeys.put("nonprob", "111"); replacingvaluesbykeys.put("recommended", "001"); replacingvaluesbykeys.put("recommend", "1000"); } public void dorelpacementinfile(){ try { in = new bufferedreader(new filereader("c:/nursery.txt")); out = new bufferedwriter(new filewriter("c:/nursery.tmp")); string str = in.readline(); while (null != str) { iterator<string> = replacingvaluesbykeys.keyset().iterator(); while(it.hasnext()) { string tobereplaced = it.next(); string replacementvalue = replacingvaluesbykeys.get(tobereplaced); // \\b word boundary, because have both recommend , recommended // , not want replacing [recommend] part of recommended. str = str.replaceall("\\b"+tobereplaced+"\\b", replacementvalue); } // write replaced line temp file: out.append(str); out.newline(); // not forget read next line: str = in.readline(); } } catch (ioexception e) { system.out.println("there problem:" + e); } finally{ try { in.close(); out.close(); } catch (ioexception e) { e.printstacktrace(); } } file f = new file("c:/nursery.txt"); f.delete(); file f2 = new file("c:/nursery.tmp"); f2.renameto(new file("c:/nursery.txt")); } public static void main(string[] args) { codage c = new codage(); c.dorelpacementinfile(); } }
hope snippets helpful,
good luck.
Comments
Post a Comment