java - remove a line that contains a specific character -
i have text file structured this, sample:
# timestamp, x, y, mac address of ap, rss 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:85:a4:01 -77 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:85:a4:02 -77 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:8e:e9:a2 -72 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:04:c2 -69 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:04:d1 -86 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:04:d2 -87 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:85:a4:12 -96 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:03:f1 -65 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:03:f2 -67 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:03:e2 -61 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:84:92:12 -95 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:85:a4:00 -77 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:04:d0 -87 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:03:f0 -70 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:8e:e9:b1 -90 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:8e:e9:b2 -92 1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:8e:e9:b0 -91
i write java program searches following characters: 00:1a:1e:85:a4:00 , 00:1a:1e:87:04:d0 , removes lines containes characters.
i have following program:
public class extract { public static void main (string[] args) throws java.lang.exception { file inputfile = new file("myfile.txt"); file tempfile = new file("mytempfile.txt"); bufferedreader reader = new bufferedreader(new filereader(inputfile)); bufferedwriter writer = new bufferedwriter(new filewriter(tempfile)); string linetoremove = "00:1a:1e:87:04:d0"; string currentline; while((currentline = reader.readline()) != null) { // trim newline when comparing linetoremove string trimmedline = currentline.trim(); if(trimmedline.equals(linetoremove)) continue; writer.write(currentline); } writer.close(); boolean successful = tempfile.renameto(inputfile); } }
but can remove characters matches entire line , output file contained in 1 line, have recommendations on have modify obtain desired output?
this think should like. may have formatting wrong on try () bit, see if compiles.
java 1.6:
public class extract { public static void main (string[] args) throws java.lang.exception { final file inputfile = new file("myfile.txt"); final file tempfile = new file("mytempfile.txt"); bufferedreader reader = null; bufferedwriter writer = null; try { reader = new bufferedreader(new filereader(inputfile)); writer = new bufferedwriter(new filewriter(tempfile)); final string linetoremove1 = "00:1a:1e:87:04:d0"; final string linetoremove2 = "00:1a:1e:85:a4:00"; string currentline; while((currentline = reader.readline()) != null) { // trim newline when comparing linetoremove final string trimmedline = currentline.trim(); if (!currentline.contains(linetoremove1) && !currentline.contains(linetoremove2)) { writer.write(currentline); writer.newline(); } } } { if (reader != null) { reader.close(); } if (writer != null) { writer.close(); } } } }
java 1.7+ :
public class extract { public static void main (string[] args) throws java.lang.exception { final file inputfile = new file("myfile.txt"); final file tempfile = new file("mytempfile.txt"); try( final bufferedreader reader = new bufferedreader(new filereader(inputfile)); final bufferedwriter writer = new bufferedwriter(new filewriter(tempfile)); ) { final string linetoremove1 = "00:1a:1e:87:04:d0"; final string linetoremove2 = "00:1a:1e:85:a4:00"; string currentline; while((currentline = reader.readline()) != null) { // trim newline when comparing linetoremove final string trimmedline = currentline.trim(); if (!currentline.contains(linetoremove1) && !currentline.contains(linetoremove2)) { writer.write(currentline); writer.newline(); } } } { if (reader != null) { reader.close(); } if (writer != null) { writer.close(); } } } }
Comments
Post a Comment