Java: Reading part of a text file -
i'm trying print out sections of text file. right text file consists of
jack <id 123.456> doug <id 231.345>
this have far in terms of code:
bufferedreader reader = new bufferedreader(new filereader("file.txt")); string readbuff = reader.readline(); string tempstring = ""; while (readbuff != null) { if (tempstring.equals("<id ") && !readbuff.equals(">")) { tempstring = readbuff; system.out.println(tempstring); } readbuff = reader.readline(); } reader.close();
i hoping print out id section (i.e. "123.456" , "231.345") of each line right doesn't print anything. appreciated.
try code:
fileinputstream fis = new fileinputstream(new file("file.txt")); bufferedreader br = new bufferedreader(new inputstreamreader(fis)); string line = null; while ((line = br.readline()) != null) { string[] parts = line.split(" "); string theid = parts[2].substring(0, parts[2].length()-1); system.out.println(theid); } br.close();
Comments
Post a Comment