File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Converting/reformatting data from text file(with delimiters) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Converting/reformatting data from text file(with delimiters)" Watch "Converting/reformatting data from text file(with delimiters)" New topic
Author

Converting/reformatting data from text file(with delimiters)

Bob Lomax
Greenhorn

Joined: Aug 25, 2001
Posts: 11
Hello ppl,
here's a question for u guys to figure out.
I have a text file which i want to read from. I will name it as input.txt, and I want to convert the delimiters in it to a user specified output delimiter. The input delimiters are specified by the user. How would i go about implementing a program to do this?
Here are the contents of the file:
The-cat+sat&on*the%mat
The+cat-got$struck#by%a*bat
For eg, i specify the input delimiters as -+&*%$#
and i want to convert the data so that the original delimiters are converted to a new specified delimiter(eg a space).
Therefore the converted data should be like this:
The cat sat on the mat
The cat got struck by a bat
can anyone come up with a good conversion algorithm? thanks.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
If you're converting big files the creation of StringTokenizer's in a loop might have some bad performance/memory issues.
Bob Lomax
Greenhorn

Joined: Aug 25, 2001
Posts: 11
thanks for the swift reply David.
I tried compiling the code and out popped an error:
--------------------------- Compiler Output -------------
DataConverter.java:37: incompatible types
found : java.lang.String
required: char
(tok.hasMoreTokens() ? ndelim : el)
^
1 error
by the way, what is the "?" for?
thanks.
Bob Lomax
Greenhorn

Joined: Aug 25, 2001
Posts: 11
sorry i forgot to mention another thing. suppose i wanna bullet proof the algorithm so that if the user specified input delimiters as +-* and the output delimiter as <space>(by the way how do u represent this in ASCII?)and the input text file is as shown;
John+*Wayne
Bob-C*+-Riley
Burt*D-+Jackson
I want to be able to, in some way convert the input delimiters to the output delimiter so that the result is as shown:
John Wayne
Rob C Riley
Burt D Jackson
Each and every one of the input delimiters are to be converted.
Would this be possible using StringTokenizer? or would a more complex algorithm have to be thought of?
I would appreciate furthur help on this.
thanks all.
Bob Lomax
Greenhorn

Joined: Aug 25, 2001
Posts: 11
oops the output shouldve been:
assume underscore represents a space character.
John__Wayne(2 spaces)
Bob_C___Riley(1 then 3 spaces)
Burt_D__Jackson(i then 2 spaces)
assuming the output delimiter is a space character(ASCII representation?).....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting/reformatting data from text file(with delimiters)