tags:

views:

173

answers:

3

i have created a file with printStream object as shown below.

PrintStream outPutOffice = new PrintStream(
   new BufferedOutputStream(new FileOutputStream(inDir+"/Office.txt")));
outPutOffice.print(fValue + (findx < agtOffColCount ? "|" : ""));

now i have to read it content and sepeate it tokens with "|" as i have written token with "|" separted. i have write code as shown below it will read line correctly but not separate token with "|" character.

BufferedReader inPutAgent = new BufferedReader(
   new InputStreamReader(new FileInputStream(inDir+"/Office.txt")));

String column=inPutAgent.readLine();
String []columnDetail = column.split("|");

columndetail array contains single character in each index instead i want single token in each index. so what is the problem.

thanks in advance

+3  A: 

You should look into the StringTokenizer, it's a very handy tool for this types of work.

Example

 StringTokenizer st = new StringTokenizer("this is a test");
 while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
 }

This will putput

 this
 is
 a
 test

You use the Second Constructor for StringTokenizer to set the delimiter:

StringTokenizer(String str, String delim)

You could also use a Scanner as one of the commentators said this could look somewhat like this

Example

 String input = "1 fish 2 fish red fish blue fish";

 Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");

 System.out.println(s.nextInt());
 System.out.println(s.nextInt());
 System.out.println(s.next());
 System.out.println(s.next());

 s.close();

The output would be

 1
 2
 red
 blue

Meaning that it will cut out the word "fish" and give you the rest, using "fish" as the delimiter.

examples taken from the Java API

Filip Ekberg
StringTokenizer(String str, String "|") This will tokenize the string with | as the separator.
Collin Price
Check out the Scanner, it's a lot better then the StringTokenizer http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
Tim Büthe
They both work very well and I would suggest any of them over .split() !
Filip Ekberg
@Filip Ekberg: I think split is fine and we should show @lakhaman what's wrong with his code, not just work around it...
Tim Büthe
From the 1.6 documentation for StringTokenizer ... "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead." ... split or regex are really the preferred methods of tokenizing.
tschaible
Split works fine, but StringTokenizer works better for the majority of the tokenizing I've had to do. No idea why they'd discourage it's use, but then again, it's not the first time I've found their "discouragements" and deprecations to be questionable (at best).
Brian Knoblauch
@Tim Büther, yes we should provide him a solution to the problem and teach him how to do it correct. Which might not be the same as showing him what was wrong with His code. You already provided the regex-solution, it would be redundant for me to say that aswell. I wanted to contribute with an alternative solution which in my opnion makes code more readable and easier to work with.
Filip Ekberg
@tschaible, I actually did not know that. But in earlier versions of Java, it was better to use the tokenizer. afaik.
Filip Ekberg
+5  A: 

the split method works with regular expressions and since the pipe symbol (|) has a special meaning and is reserved, you need to escape it like this:

split("\\|");

You should read about regex here or here

Tim Büthe
so can changing |(pipe symbol) will solve my problem?
lakhaman
I think so, give it a try
Tim Büthe
+2  A: 

The argument to split is a regex. You should do:

String []columnDetail = column.split("\\|");

or:

String []columnDetail = column.split("[|]");
Maurice Perry
thanks for your answer
lakhaman