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 convert .txt file in .csv format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "convert .txt file in .csv format" Watch "convert .txt file in .csv format" New topic
Author

convert .txt file in .csv format

Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
Dear all,

I hope you are doing good.
I am looking to convert .txt file in .csv format.
The contents might have different names and values.
e.g.

start:
id:XXXX
name:abc
address:xyz

start:
age:29
height:5'9

start:
accountno:xxx
emailid:xxxxx

end:

I want export these contents to a .csv file. I am aware that I need to use delimiter. But I am not getting the exact out put I do wish to. It would be a great help anyone has got an idea/sample code. Thanks in advance.

Best regards.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
What did you come up with so far ?


JDBCSupport - An easy to use, light-weight JDBC framework -
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

Thank you for your reply.
I have got logical steps. I am not 100% confident about it.

while ((text = br.readLine()) != null){
String splitarray[] = strRead.split(":");
String firstentry = splitarray[0];
String secondentry = splitarray[1];
System.out.println(firstentry + " " + secondentry);


here is what i have written

import java.util.regex.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;


public class fileRead
{
public static void main(String[] args)
{
File file = new File("C:\\source.txt");
StringBuffer str = new StringBuffer();
BufferedReader br = null;

try
{
br = new BufferedReader(new FileReader(file));
String text = null;

// repeat until all lines is read
while ((text = br.readLine()) != null)
{

//think i need to implement delimiter logic here.

}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}

// show file contents here
System.out.println(str.toString());
}
}



Best regards
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Please put your code between code tags, so it's fun to read ;-)
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

import java.util.regex.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class fileRead
{
public static void main(String[] args)
{
File file = new File("C:\\source.txt");
StringBuffer str = new StringBuffer();
BufferedReader br = null;

try
{
br = new BufferedReader(new FileReader(file));
String text = null;

// repeat until all lines is read
while ((text = br.readLine()) != null)
{
String splitarray[] = text.split(":");
String firstentry = splitarray[0];
String secondentry = splitarray[1];
System.out.println(firstentry + " " + secondentry);

}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}

// show file contents here
System.out.println(str.toString());
}
}



I am learning the java language. I am not sure about colon delimiter's use and what is to be next?

Best regards
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Please, please use those tags to format your code --> [code] [/code]
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

Sorry, I got your point pretty lately

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Ok, good :-) ..

Now, what is the result you would expect and what is the result you get ??
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

I am not sure about the logic for the further enhancement.
E.g. suppose source.txt file contains something like this

start:

id:xxxx
name:

start:

age:xxx
sex:xxx

start:

name1:xxx
name2:xxx


I am confused about the further looping.


Best regards.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Is 'start:' your delimiter for a new data block ?
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

Yes. Kind of. Actually, it must be a selimiter as it occurs frequently. I have to take information contained under start:delimiter and parse name:value pair delimited by ":".

Best regards.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Ok, first it is a good decision to do the processing while you are looping as opposed to reading the whole file into a StringBuilder first and later working with it (just imagine a 500MB file).

What you want to do is check for the 'start:' delimiter in your while loop and then parse the name value pairs.

As for blank lines, simply check each line if it contains something using line.trim().equals("")
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

Thank you very much. I will try it now. I will ask you again if I face any difficulty.

Best regards.
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

Hello there,


I tried the way you advised me. It's not working from my side. I mean I am mistaking in looping. I am clear about how I am splitting start: from the file and how would I do processing before using stringbuffer/stringbuilder?
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

have you got any sample code using for delimiter using parser or tokenizer? It would be a great help if I get some guidelines from a sample code.


Best regards.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
lets see



I did not test the code so it might be flawed but the concept is okay i guess.

The code would be much simpler if you read the complete file first and then do the parsing, but that can give you problems when working with large files.
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

Hello there,

Thank you very much for the example.
I got everything except readLine() especially if I am using StringBuilder. Getting confused a bit ( ) but thank you very much for your help.
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

hello again. What if I wish to use StringBuffer instead of StringBuilder.

1.look for the start:
2.once you catch first start:, look for the name:value pair using : delimiter.
3.store this name:value pair in local variable like string etc.
4.repeat the steps until we see end:
5.extract the name:value pair in a .csv file.

Am I thinking correctly? I would welcome and appreciate suggestions as this is a learning curve for me (beginner).
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
the readLine is from the BufferedReader object, which is used to read the file.

The way you parse the file depends on what you want to do with the contents.

Do you want to create a bean object for each data 'chunk' separated by the start: delimiter, or do you plainly want to output the messages to the console ?
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

Got your point. amended my code using your sample code. I am interested in message at console. At the same time, I also want the stored name:value pairs should be exported to .csv file and I should be able to display contents of the .csv file. this is what i am trying to do with your code.



can you let me know what should be my flow control?
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

Hello. I can see contents of the file on the console. I have 2 doubts.
1.How come should I test End: while reading the file?
2.I do not wish to consider repeatable occurrences of the name: (i.e. id,age,email etc). I believe I need to use sets for that. So how I am going to store these name: in the string and loop it and store them in a header of the .csv (id,age,email,account etc format)?
Thanks in advance.

Best regards.
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

This is what I have done, unsuccessful though. Just not able place set and add elements in right place.

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I am not quite sure what you are doing . ... You have key value pairs. Why would you throw those into a Set ?
Rajubhai Kotwal
Ranch Hand

Joined: Nov 03, 2009
Posts: 47
@Sebastian Janisch

I will explain. Although I have key:value pair, it might happen that same key appear several times. For e.g.

begin:

name:xxx
age:89
doctypename:xxx

begin:

sex:x
id:xxxx
doctypename:xxx

begin:

doctypename:xxx
id:xxx

In this classical scenario, I wish to take keys only once and store in the header of .csv file. I am using sets in order to avoid duplication. Once I achieve that, i can take these keys in header of .csv and print respective values beneath it. At present, I am doing something wrong in main while loop. I am getting many []s (square brackets) printed followed by file contents.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert .txt file in .csv format