Switch to full style
Project under GPL source codes are posted here
Post a reply

Multithreaded File Reading

Fri Oct 17, 2008 1:27 pm

* Project Name:   Multithreaded File Reading
* Type:   Multithreading
* Technology:  Java
* IDE:   Any
 Description:   This code is an example of how you can read File(I/O) with using threading.
java code
import java.io.*;
import java.lang.*;
class MultiThreadedFileRead extends Thread
{
InputStream in;
MultiThreadedFileRead(String fname) throws Exception
{
in=new FileInputStream(fname);
this.start();
}
public void run()
{
int i=0;
while(i!=-1)
{
try
{
i=in.read();
System.out.print((char)i);
}catch(Exception e){}
}
try
{
in.close();
}catch(Exception e){}
}
public static void main(String a[]) throws Exception
{
int n=2;
System.out.print("Enter the number of files : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
n=Integer.parseInt(br.readLine());
}catch(Exception e){}
MultiThreadedFileRead fr[]=new MultiThreadedFileRead[n];
long tim;
tim=System.currentTimeMillis();
for(int i=0;i<n;i++)
fr[i]=new MultiThreadedFileRead(a[i]);
for(int i=0;i<n;i++)
{
try
{
fr[i].join();
}catch(Exception e){}
}
System.out.println("Time Required : "+(System.currentTimeMillis()-tim)+" miliseconds.");
}
}




Post a reply
  Related Posts  to : Multithreaded File Reading
 Reading the all file in php     -  
 Reading file with integers     -  
 Reading and Writing To text file     -  
 Reading selected data from a source file     -  
 Reading a File Line by Line in php     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 Parsing and Reading RSS feeds     -  
 Reading a Specific Character in php     -  
 Java Object Reading     -  
 Reading email in Python     -  

Topic Tags

Java Files and I/O, Java Threads