Thursday, 15 September 2011

file - What is the best way to parse a line using Java? -



file - What is the best way to parse a line using Java? -

i'm trying write little java application, , part of job read, write, , parse lines text files. problem though taking line text file, reading it, , breaking parts. example, illustration line is:

tuesday, 11/11/14 10:30:32: 3.93

i want programme able @ , fill illustration string day = "tuesday", , value = 3.93.

filereader filereader = new filereader(itemvalue); bufferedreader bufferedreader = new bufferedreader(filereader); stringbuffer stringbuffer = new stringbuffer(); string line; while ((line = bufferedreader.readline()) != null) { stringbuffer.append(line + "\n"); int[] value; } filereader.close(); system.out.println("contents of file:"); system.out.println(stringbuffer.tostring());

this code have - parse through file , print out line line console, don't know how process line. i've tried using bufferedreader.readline variable containing line, doesn't seem answer. i'm extremely new java , big roadblock in programming, if point me toward solution, awesome. in advance! :)

edit: of info looks same, here rest of it.

tuesday, 11/11/14 10:29:23: 4.48 tuesday, 11/11/14 10:29:27: 5.0 tuesday, 11/11/14 10:29:39: 5.95 tuesday, 11/11/14 10:29:46: 6.0 tuesday, 11/11/14 18:07:25: 4.0 tuesday, 11/11/14 18:07:27: 4.5 tuesday, 11/11/14 18:07:33: 5.0 tuesday, 11/11/14 18:07:39: 5.9 tuesday, 11/11/14 18:07:51: 20.0

two of popular approaches parsing kind of info string.split(), , using regular expression.

with string.split() might this:

while ((line = bufferedreader.readline()) != null) { string[] parts = line.split("\\s+"); // each part of input line (separated whitespace) in different element of parts array }

if take utilize regular expression, , wanted first , lastly parts, might seek regular look like:

^(a-za-z), .*? (0-9\.)$

of course of study there other code required execute it. google search "regular expressions".

java file io

No comments:

Post a Comment