
how can i read and write say two different files at the same time using threads.

import java.io.*;
class ReadFileUsingThread
{
public void readFromFile(final String f, Thread thread) {
Runnable readRun = new Runnable() {
public void run() {
BufferedWriter pw=null;
FileInputStream in=null;
String text = null;
try{
pw = new BufferedWriter(new FileWriter("C:/New/"+new File(f).getName()));
Thread.sleep(5000);
File inputFile = new File(f);
in = new FileInputStream(inputFile);
byte bt[] = new byte[(int)inputFile.length()];
in.read(bt);
text = new String(bt);
pw.write(text);
pw.newLine();
pw.close();
} catch(Exception ex) {
}
}
};
thread = new Thread(readRun);
thread.start();
}
public static void main(String[] args)
{
ReadFileUsingThread f=new ReadFileUsingThread();
File file = new File("C:/Files/");
FilenameFilter filter = new FileFilter();
File[] files = file.listFiles(filter);
Thread thread[]=new Thread[files.length];
for(int i = 0; i < files.length; i++) {
thread[i]=new Thread();
f.readFromFile(files[i].getPath(),thread[i]);
}
}
}
class FileFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return name.endsWith("txt");
}
}

i did you example but it crashes with big file how come. i get this error
:Thread-1" java.lang.OutOfMemoryError: Java heap space
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.