Results 1 to 3 of 3

Thread: multithreading

  1. #1
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    Unhappy 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

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    freedom exists in the world of ideas

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default

    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.

Similar Threads

  1. Replies: 1
    Last Post: 11-16-2007, 10:09 AM
  2. Multithreading + Networking (desperate)
    By bluebarca in forum New To Java
    Replies: 1
    Last Post: 11-07-2007, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •