Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-27-2008, 08:57 AM
Member
 
Join Date: Jun 2008
Posts: 1
shilpa.krishna is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-27-2008, 03:52 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 526
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Concurrency
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-27-2008, 05:18 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,493
Norm is on a distinguished road
I'd suggest you start with a simple program that uses a thread to do a println("Hello world");
If you have problems, bring them here for help.
When you've got that working, you can replace the println() code with whatever processing you want done.
Read about the Thread and Runnable classes.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why use multithreading? JavaForums Java Blogs 0 12-17-2007 01:21 PM
Multithreading (basics) JavaForums Java Blogs 0 12-17-2007 01:21 PM
socket Multithreading - & - Obtaining the IP of a client! bluebarca Networking 1 11-16-2007 11:09 AM
Multithreading + Networking (desperate) bluebarca New To Java 1 11-07-2007 03:14 PM


All times are GMT +3. The time now is 12:42 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org