
i wrote a program that reads and writes multiple files and combines them and writes them as one single file in as CVS format(bin.txt). After the program is done then another class reads the new file and removes extra delimiter(,) that the first file had at the end for some reason. the problem comes in when i have large file > 40mb and my program has this error "using java.lang.OutOfMemoryError: Java heap space " when the second class tries and reads and write the (bin.txt). here is my code
:::::::::main class:::::: package elite.tech.com;
/*********** * program Cvs * @author Alain ndayishimiye * @version 1 * @since 2012 **********/ import java.io.File; import java.io.IOException;
public class testing{ static String outpath = "C:\Users\ndayala\bin\bin.txt"; public static void main(String[]args) throws IOException, InterruptedException{ Firstread t1; Secondread t2; t1 = new Firstread(); t2 = new Secondread();
t1.runn1();
System.out.println("done about to start 2");
File file = new File(outpath);
long filesize = file.length();
long filesizeInKB = filesize / 1024;
System.out.println("The output file is of size : " + filesizeInKB + " KB");
if(t1.isAlive()==false){
System.out.println("second thread is about to start");
Thread.sleep(10000);
System.out.println("starting the second");
t2.runn2();
}
}
}
::::::::::::::first class:::::::::::::::::::::::::::::::::::::;;; package elite.tech.com; /*********** * program Cvs * @author Alain ndayishimiye * @version 1 * @since 2012 **********/ import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter;
public class Firstread extends Thread{ static String inpath = "C:\Users\ndayala\asc"; static String outpath = "C:\Users\ndayala\bin\bin.txt"; public void runn1() throws IOException{
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
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","" );
line = line.replace("callIdentificationNumber","" );
line = line.replace("recordSequenceNumber","" );
line = line.replace("typeOfCallingSubscriber","" );
line = line.replace("callingPartyNumber","" );
line = line.replace("callingSubscriberIMSI","" );
line = line.replace("callingSubscriberIMEI","" );
line = line.replace("dateForStartofCharge","" );
line = line.replace("->0|0|6","" );
System.out.println("writting:........"+files[i]);
char delim =',';
pw.print(line+delim);
line = br.readLine();
}
br.close();
}
pw.close();
System.out.println("your file have been concatenaded into one file under directory "+outpath);
//convert(outpath);
}
}
::::::::::::second class::::::::::::::::::::::::::::::::::::::::::
package elite.tech.com; /*********** * program Cvs * @author Alain ndayishimiye * @version 1 * @since 2012 **********/ import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter;
public class Secondread extends Thread{ static String inpat = "C:\Users\ndayala\bin",outpat = "C:\Users\ndayala\bin.txt"; public void runn2() throws IOException{ PrintWriter pw = new PrintWriter(new FileOutputStream(outpat ));// write to the outpath dir
File file = new File(inpat);//files from the inpath
File[] files = file.listFiles();//listFiles all file in inpath dir
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(",,","" );
System.out.println("writting:........"+files[i]);
pw.print(line);
line = br.readLine();
}
br.close();
}
pw.close();
System.out.println("your file has been created in: "+outpat);
//convert(outpath);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.