Results 1 to 3 of 3
Thread: multithreading
- 06-27-2008, 07:57 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 1
- Rep Power
- 0
multithreading
Hi guys am new to java..pls help me with following...
The following is the code to read and display contents of file in a directory..how to do this using multithreading...
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class javafile{
public javafile() {
}
private void listFileNames(File dirPath) {
File directory = dirPath;
File[] filesInDir = directory.listFiles();
if (filesInDir != null) {
int length = filesInDir.length;
for (int i = 0; i < length; ++i) {
int j= 1;
File f = filesInDir[i];
if (f.isFile()) {
if (f.canRead()) {
System.out.println("Can Read File: " + f.getName());
//String fileName = f.getPath();
//fileName = fileName.substring(0, fileName.indexOf("."));
//fileName = fileName + j;
File fileIn = new File(f.getPath());
//File fOut = new File(fileName+".txt");
try {
//copyFile(fileIn,fOut);
displayFileContent(fileIn);
} catch (Exception e) {
System.out.println(e.getMessage());
}
j++;
}
else
System.out.println("Can NOT Read File: " + f.getName());
} else if (f.isDirectory()) {
System.out.println("Directory: " + f.getName());
}
}
}
}
/**
* Display the content of the file.
* @param fIn-> The file name of which the content is to be displayed.
*/
public void displayFileContent(File fIn)throws Exception {
byte[] buf = new byte[1024];
FileInputStream fis = new FileInputStream(fIn);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
try {
int filelength = dis.read(buf);
String filestring = new String(buf, 0, filelength);
System.out.println("CONTENT OF FILE =" +fIn+ filestring);
} catch(IOException iox) {
System.out.println("File read error...");
iox.printStackTrace();
}
}
public static void main(String args[]) {
javafile java1 = new javafile();
File dirPath = new File("D:\\Logs\\Files");
File dirPath1 = new File("D:\\Logs\\NewsmlUpdate");
java1.listFileNames(dirPath);
java1.listFileNames(dirPath1);
}
}
Thanx in advance
- 06-27-2008, 02:52 PM #2
- 06-27-2008, 04:18 PM #3
Similar Threads
-
socket Multithreading - & - Obtaining the IP of a client!
By bluebarca in forum NetworkingReplies: 1Last Post: 11-16-2007, 10:09 AM -
Multithreading + Networking (desperate)
By bluebarca in forum New To JavaReplies: 1Last Post: 11-07-2007, 02:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks