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);
}
}
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);}
}
}
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 :)
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