txt to cvs format
i am writing a simple program that read a text file from one directory and writes an new one in cvs format in another one. I am running into a problem on how to correctly format it . for now this is what i have can someone help me.. thank you :)
first class:
package elite.tech.com;
/*this program reads files from inpath
and writes them to outpath as
a single txt file*/
import java.io.*;
public class ConcatenatedFiles {
static public void main(String arg[]) throws IOException {
System.out.println("about to read");
threadrunner thr1, thr2;
System.out.println("thread 1 startin");
thr1 = new threadrunner();
System.out.println("thread 2 startin");
thr2 = new threadrunner();
thr1.reba();
//thr2.reba();
//run();
}
//this class converts the text file into binary
static void convert(String to_convert){
File file=new File(to_convert);
boolean exists = file.exists();
if (!exists) {
// It returns false if File or directory does not exist
System.out.println("the file or directory you are searching does not exist : " + exists);
}else{
// It returns true if File or directory exists
System.out.println("Status: "+to_convert+ " has been created .");
}
}
}
second class:
package elite.tech.com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class threadrunner extends Thread{
/*the following are my inpath which is the dir to the files to be read and the outpath which is the
dir for the out put text.*/
private int a;
static String inpath = "C:\\Users\\ndayala\\asc";
static String outpath = "C:\\Users\\ndayala\\bin\\bin.txt";
public threadrunner() { }
// public void start() {
// for (int i = 1; i <= a; ++i){
//
// System.out.println(getName() + " is " + i);
// try{
// sleep(1000);
// }
// catch(InterruptedException e){}
// }
// }
public void reba() throws IOException{
Random rn = new Random();
PrintWriter pw = new PrintWriter(new FileOutputStream(outpath ));// write to the outpath dir
File file = new File(inpath);//files from the inpath
File[] files = file.listFiles();//listFiles all file in inpath dir
int rndom = rn.nextInt(files.length);
for (int i = 0; i < files.length; i++) {
System.out.println("Processing " + files[i].getPath() + "... ");
BufferedReader br = new BufferedReader(new FileReader(files[i].getPath()));
String line = br.readLine();
while (line != null) {
line = line.replace(" ","" );
line = line.replace("mSOriginatingSMSinMSC","" );
line = line.replace("GSMPLMNCallDataRecord","" );
line = line.replace("tAC","" );
System.out.println("found it~~~~~~~~~~~~~~~~"+files[i]);
char com =',';
pw.print(com+line);
line = br.readLine();
}
br.close();
}
pw.close();
System.out.println("your file have been concatenaded into one file under directory "+outpath);
//convert(outpath);
}
}
View Answers
January 11, 2012 at 11:21 AM
Java convert text file to CSV file
import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class TextToCSV {
public static void main (String[] args) throws Exception {
try{
FileInputStream fstream = new FileInputStream("C:\\data.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
ArrayList list=new ArrayList();
while ((strLine = br.readLine()) != null){
list.add(strLine);
}
String id="",name="",course="",deptt="";
Iterator itr;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("Roll No");
rowhead.createCell((short) 1).setCellValue("Name");
rowhead.createCell((short) 2).setCellValue("Marks");
rowhead.createCell((short) 3).setCellValue("Grade");
int index=1;
for (itr=list.iterator(); itr.hasNext(); ){
String str=itr.next().toString();
String [] splitSt =str.split(" ");
for (int i = 0 ; i < splitSt.length ; i++) {
id=splitSt[0];
name=splitSt[1];
course=splitSt[2];
deptt=splitSt[3];
}
HSSFRow row = sheet.createRow((short)index);
row.createCell((short)0).setCellValue(id);
row.createCell((short)1).setCellValue(name);
row.createCell((short)2).setCellValue(course);
row.createCell((short)3).setCellValue(deptt);
index++;
}
FileOutputStream fileOut = new FileOutputStream("c:\\TextToCSV.xls");
wb.write(fileOut);
fileOut.close();
System.out.println("Data is saved in excel file.");
}
catch(Exception e){System.out.println(e);}
}
}
January 11, 2012 at 11:57 AM
how do i import the "import org.apache.poi.hssf." and my input file is different it looks something lik this:
https://docs.google.com/document/pub?id=1ZWPSkguOhd0o5A1l9_a-B1SlThfXiwX5jjx7QAwfruo
i want to extract all that is on the right side only and print it in cvs.
thank you :)
January 11, 2012 at 12:38 PM
okay i downloaded the the jar file and it works can i get a sample of the data.txt so i can try it out
Related Tutorials/Questions & Answers:
txt to cvs formattxt to
cvs format i am writing a simple program that read a text file from one directory and writes an new one in
cvs format in another one. I am running into a problem on how to correctly
format it . for now this is what i have
convert .txt file in .csv format - Java Beginnersconvert .
txt file in .csv format 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
Advertisements
CVSCVS Which
CVS are you used? What are the steps are you going to follow to deploy a code in server
CVSCVS Explain about control version system?
CVS is used... to "checkout" that collection.
CVS also helps you if you are part of a group of people.... Unfortunately, if someone is using another editor, that safeguard will not work.
CVS cvs - CVScvs can you please tell me how to work with
cvs integrated with eclipse 3.2??? Hi friend,
The goal of the
CVS component is to provide the best
CVS integration in the best IDE. Because the
CVS plug-in includes
CVS - CVSCVS can Anyone please tell me how to work with
cvs integrated with eclipse 3.2 ASAP. it's an urgent.
I'll be highly obliged
What is CVS What is
CVS hii,
What is
CVS ?
Hello,ADS_TO_REPLACE_1
CVS is used to keep track of collections of files in a shared
directory called "The Repository".
Each collection of files can be given a "module" name
learn CVSlearn CVS How i can use
cvs in eclipse,
How i can check in and cheack out my project
.cvs file - JSP-Servlet.
cvs file how can i update the contents of .
cvs(coma seperated) file to database? reading of
cvs file and insert each value in database. how can i update the contents of .csv(coma seperated) file to database? reading
Tool In JAVA - CVSTool In JAVA Hai this is JAGADHISH
Iam learning
cvs tool.If anybody have related material or examples for this
CVS tool plz send me.Thank you(in advance). Hi friend,
For read more information to visit this link
Thread for reading txt fileThread for reading
txt file how to use 3 thread to read 3
txt file?
To create three threads for reading the file and three threads for getting the strings out of the queue and printing them.
thanks
ModuleNotFoundError: No module named 'anchor-txt'ModuleNotFoundError: No module named 'anchor-
txt' Hi,
My Python... 'anchor-
txt'
How to remove the ModuleNotFoundError: No module named 'anchor-
txt' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'anchor-txt'ModuleNotFoundError: No module named 'anchor-
txt' Hi,
My Python... 'anchor-
txt'
How to remove the ModuleNotFoundError: No module named 'anchor-
txt' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'pygame-txt'ModuleNotFoundError: No module named 'pygame-
txt' Hi,
My Python... 'pygame-
txt'
How to remove the ModuleNotFoundError: No module named 'pygame-
txt' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'pygame-txt'ModuleNotFoundError: No module named 'pygame-
txt' Hi,
My Python... 'pygame-
txt'
How to remove the ModuleNotFoundError: No module named 'pygame-
txt' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'pygame-txt'ModuleNotFoundError: No module named 'pygame-
txt' Hi,
My Python... 'pygame-
txt'
How to remove the ModuleNotFoundError: No module named 'pygame-
txt' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'txt-util'ModuleNotFoundError: No module named '
txt-util' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
txt-util'
How to remove the ModuleNotFoundError: No module named '
txt-util
ModuleNotFoundError: No module named 'anchor-txt'ModuleNotFoundError: No module named 'anchor-
txt' Hi,
My Python... 'anchor-
txt'
How to remove the ModuleNotFoundError: No module named 'anchor-
txt' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'asv_txt'ModuleNotFoundError: No module named 'asv_
txt' Hi,
My Python..._
txt'
How to remove the ModuleNotFoundError: No module named 'asv_
txt... to install padas library.
You can install asv_
txt python with following command
Adding .txt files in a given directoryAdding .
txt files in a given directory I want to read number of XML... tag value as one .
txt file in a given directory? I have extracted tag values but plz help me how can I save each body tag as one .
txt file and add in a directory
Shifting txt file to database - Java BeginnersShifting
txt file to database Question Details:
I want to shift data from
txt file to Database. The data is written in the following text
format.
Record No. = 0001
Name : Abdul Rauf
Designation
Backup Into txt File
Backup Into
txt File
Backup Into
txt File is used to put the backup file from a ...
The Tutorial illustrate an example from 'Backup Into
txt File
image formatimage format what are the different
format available in image
image formatimage format what are the different
format available in image
Convert pdf to rtf and txt - XMLConvert pdf to rtf and txt Can we convert PDF to RTF and
TXT, and how? Hi Friend,
Try the following codes:
1)Convert PDF to RTF
import java.io.*;
import java.util.*;
import com.lowagie.text.*;
import