txt to cvs format

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 format
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
convert .txt file in .csv format - Java Beginners
convert .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
CVS
CVS  Which CVS are you used? What are the steps are you going to follow to deploy a code in server
CVS
CVS  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 - CVS
cvs  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 - CVS
CVS  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 CVS
learn CVS  How i can use cvs in eclipse, How i can check in and cheack out my project
what is tagging - CVS
what is tagging  what is tagging process,cvs vendors
.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
Uploading and download pdf or .txt file.
Uploading and download pdf or .txt file.  I want admin user to upload pdf file into database and the users can download those pdf format from database
Tool In JAVA - CVS
Tool 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
cvs - CVS
".txt" to sql with JDBC
".txt" to sql with JDBC  how to insert ".txt" into sql result[BLOB] with JDBC
Thread for reading txt file
Thread 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-to-html'
ModuleNotFoundError: No module named 'txt-to-html'  Hi, My Python... 'txt-to-html' How to remove the ModuleNotFoundError: No module named 'txt-to-html' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'txt-to-html'
ModuleNotFoundError: No module named 'txt-to-html'  Hi, My Python... 'txt-to-html' How to remove the ModuleNotFoundError: No module named 'txt-to-html' error? Thanks   Hi, In your python environment
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 'txt-wrangler'
ModuleNotFoundError: No module named 'txt-wrangler'  Hi, My Python... 'txt-wrangler' How to remove the ModuleNotFoundError: No module named 'txt-wrangler' error? Thanks   Hi, In your python
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
ModuleNotFoundError: No module named 'eventb-to-txt'
ModuleNotFoundError: No module named 'eventb-to-txt'  Hi, My... 'eventb-to-txt' How to remove the ModuleNotFoundError: No module named 'eventb-to-txt' error? Thanks   Hi, In your python
Version of com.jianpage>txt-util dependency
List of Version of com.jianpage>txt-util dependency
ModuleNotFoundError: No module named 'hyuki-cvs-graph'
ModuleNotFoundError: No module named 'hyuki-cvs-graph'  Hi, My... named 'hyuki-cvs-graph' How to remove the ModuleNotFoundError: No module named 'hyuki-cvs-graph' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'hyuki-cvs-graph'
ModuleNotFoundError: No module named 'hyuki-cvs-graph'  Hi, My... named 'hyuki-cvs-graph' How to remove the ModuleNotFoundError: No module named 'hyuki-cvs-graph' error? Thanks   Hi, In your python
Convesion of txt file to doc file.??????
Convesion of txt file to doc file.??????  how to convert text file to doc file using java
Adding .txt files in a given directory
Adding .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 Beginners
Shifting 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
ModuleNotFoundError: No module named 'django-humans-txt'
ModuleNotFoundError: No module named 'django-humans-txt'  Hi, My... named 'django-humans-txt' How to remove the ModuleNotFoundError: No module named 'django-humans-txt' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'odoo10-addon-report-qweb-txt'
ModuleNotFoundError: No module named 'odoo10-addon-report-qweb-txt'  ...: No module named 'odoo10-addon-report-qweb-txt' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-report-qweb-txt' error
ModuleNotFoundError: No module named 'txt_when_done'
ModuleNotFoundError: No module named 'txt_when_done'  Hi, My... 'txt_when_done' How to remove the ModuleNotFoundError: No module named 'txt_when_done' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-humans-txt'
ModuleNotFoundError: No module named 'django-humans-txt'  Hi, My... named 'django-humans-txt' How to remove the ModuleNotFoundError: No module named 'django-humans-txt' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'robots-txt-parser'
ModuleNotFoundError: No module named 'robots-txt-parser'  Hi, My... named 'robots-txt-parser' How to remove the ModuleNotFoundError: No module named 'robots-txt-parser' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'sloth-ci.ext.robots_txt'
ModuleNotFoundError: No module named 'sloth-ci.ext.robots_txt'  Hi...: No module named 'sloth-ci.ext.robots_txt' How to remove the ModuleNotFoundError: No module named 'sloth-ci.ext.robots_txt' error? Thanks   
ModuleNotFoundError: No module named 'txt-util-kr'
ModuleNotFoundError: No module named 'txt-util-kr'  Hi, My Python... 'txt-util-kr' How to remove the ModuleNotFoundError: No module named 'txt-util-kr' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'change_lrc_to_txt'
ModuleNotFoundError: No module named 'change_lrc_to_txt'  Hi, My... named 'change_lrc_to_txt' How to remove the ModuleNotFoundError: No module named 'change_lrc_to_txt' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-ads-txt'
ModuleNotFoundError: No module named 'django-ads-txt'  Hi, My... named 'django-ads-txt' How to remove the ModuleNotFoundError: No module named 'django-ads-txt' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-robots-txt'
ModuleNotFoundError: No module named 'django-robots-txt'  Hi, My... named 'django-robots-txt' How to remove the ModuleNotFoundError: No module named 'django-robots-txt' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'file_open_gips_txt'
ModuleNotFoundError: No module named 'file_open_gips_txt'  Hi, My... named 'file_open_gips_txt' How to remove the ModuleNotFoundError: No module named 'file_open_gips_txt' error? Thanks   Hi, In your
image format
image format  what are the different format available in image
image format
image format  what are the different format available in image
Maven Dependency txt-util >> 0.1-RELEASE
You should include the dependency code given in this page to add Maven Dependency of com.jianpage >> txt-util version0.1-RELEASE in your project
Convert pdf to rtf and txt - XML
Convert 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
java - CVS

Ads