Click to See Complete Forum and Search --> : Java File Input Question


lskuff
September 16th, 2004, 06:59 PM
Hi I was wondering if someone could help me with a file input question. I have some data in a file and I need to get it out in a certain format. Here is the format it is in the file:



value1: this, that, thou
special2: get, cat, meow, through
extra3: true, false

I need to get the data out so I can add the values to String variables with no spaces before or after the string so it would be the equivalent of going like:

string = "value1"
sting2 = "this"
string3 = "that"
string4 = "thou"

string5 = "special2"
string6 = "get"
etc...

Here is what I have so far:

BufferedReader in = new BufferedReader(new FileReader(fileName));
string = in.readLine();
StringTokenizer st = new StringTokenizer(string, ":" );
string1 = st.nextToken();
StringTokenizer st2 = new StringTokenizer(st.nextToken(), "," );
string2 = st2.nextToken()
string3 = st2.nextToken()
etc....

This is just a start of the code I know I need to add loops and stuff but mainly I just need some help on how to get the data out how I want it to be and how to make sure there are no spaces before or after the strings.


Thanks

Drain
September 16th, 2004, 09:16 PM
Just loop until in.readLine() is null

Split it at ": " and ", " instead of ":" and ","

Use String.split() instead of StringTokenizer. It will spit out an array with your tokens already in it.

cma
September 16th, 2004, 10:18 PM
Here's how you would read the first line using StringTokenizer:
BufferedReader console = new BufferedReader(new FileReader("data.txt"));
String string = console.readLine();
StringTokenizer st = new StringTokenizer(string, " :,");

String string1 = st.nextToken();
String string2 = st.nextToken();
String string3 = st.nextToken();
String string4 = st.nextToken();

System.out.println("string1 = " + string1);
System.out.println("string2 = " + string2);
System.out.println("string3 = " + string3);
System.out.println("string4 = " + string4);

and how you would probably read it with String.split():
BufferedReader console = new BufferedReader(new FileReader("data.txt"));
String string = console.readLine();

String[] strs = string.split("[:\\s][,\\s]");

System.out.println(Arrays.asList(strs));

lskuff
September 16th, 2004, 10:58 PM
Thanks! That worked.

cjard
September 17th, 2004, 08:16 AM
String[] strs = string.split("[:\\s][,\\s]");

[/code]

cma; split doesnt work like that.. the regex you posted reads as:

"[match a colon or a whitespace], followed by [a comma or a whitespace]"

what you actually want is:

"[match a colon or a comma] followed by a whitespace"


i.e. String.split("[;,]\\s");

here's the results of the above regex, when passed into my SplitTest program (see my sig, String.split() for a copy of the program):

C:\javawork>java SplitTest "special2: get, cat, meow, through" "[:,]\s"
Usage: java SplitTest "string to split" "regex to split by"
array[0] length(8) >special2<
array[1] length(3) >get<
array[2] length(3) >cat<
array[3] length(4) >meow<
array[4] length(7) >through<

Your delimiter expression contains at least one \ character.
To prevent the java compiler interpreting this as an escape
code, you should use the following split() command in your
.java sourcecode:

myString.split("[:,]\\s");

cjard
September 17th, 2004, 08:18 AM
note that when runnign the program, i pass the argument in as "[;,]\s" witha single slash. no double slash is needed because the program is already compiled, and hence wont suffer the java compiler trying to use \s as an escape code. when writing code though, the regex WILL suffer, hence the guidance note at the end of the programs output

cma
September 17th, 2004, 12:35 PM
Heh, I'm not very experienced with regular expression matching. At first, I was trying to get it to read: colon followed by space or comma followed by space, but the result wasn't what I expected (it stored the space).