文件和多线程

时间:2013-01-13 11:37:27

标签: java eclipse multithreading

我需要使用多个线程读取两个文件并在控制台中打印文件的内容。用户将输入文件路径并使用线程来读取文件的内容。我很难理解文件。任何人都可以建议我在run方法中应该做些什么?

import java.io.*;

public class Ch3Ex4 implements Runnable
 {
  public void ReadFile(String str,Thread thread)
   {
    try
     {
      File inputFile = new File(str);
      FileInputStream in = new FileInputStream(inputFile);
     }
    catch(Exception e)
    {
        e.printStackTrace();
    }
  } 
    public void run()
      {

      } 

 public static void main (String[] args)
  {
    try
    {
    Ch3Ex4 obj = new Ch3Ex4();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the two file paths:");
    String s1 = br.readLine();
    String s2 = br.readLine();
    Thread thread1 = new Thread(obj);
    Thread thread2 = new Thread(obj);
    obj.ReadFile(s1, thread1);
    obj.ReadFile(s2, thread2);
    thread1.start();
    thread2.start();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
  }
 }

3 个答案:

答案 0 :(得分:2)

run方法旨在包含该线程执行的代码。因此,当您想要从两个不同的线程中读取两个文件时,您需要使用run来执行您在ReadFile方法中所做的任何事情。

请注意,您需要创建Ch3Ex4类的实例并调用start方法才能开始创建新线程。

编辑:在这种情况下,您可以在BufferedReader方法中使用run,如下所示:(来自mkyong的网站)

    BufferedReader br = null;

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("C:\\testing.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

答案 1 :(得分:0)

如前所述,您应该将ReadFile中的代码移动到run方法中,或者从run()中调用ReadFile()方法。此外,您需要为文件名创建一个实例变量,并为两个线程创建两个对象。 请参阅highligted的更改:

import java.io.*;

public class Ch3Ex4 implements Runnable
{
     String s1;

     Ch3Ex4(String s){
           s1=s;
     }

     public void ReadFile(String str){
       //existing code 
     } 

     public void run()
     {
        ReadFile(s1);
     } 

     public static void main (String[] args)
     {
        try
        {
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           System.out.println("Enter the two file paths:");
           String s1 = br.readLine();
           String s2 = br.readLine();
           Ch3Ex4 obj1 = new Ch3Ex4(s1);
           Ch3Ex4 obj2 = new Ch3Ex4(s2);

           Thread thread1 = new Thread(obj1);
           Thread thread2 = new Thread(obj2);

           thread1.start();
           thread2.start();
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
    }
 }

答案 2 :(得分:-1)

希望下面的代码是您所期待的。

 public class Ch3Ex4 implements Runnable
 {
    String file;
    public void Ch3Ex4 (String file)
    {
       this.file = file;
    } 
    public void run()
    {
    try
       {
        File inputFile = new File(file);
        FileInputStream in = new FileInputStream(inputFile);
        int data;
        while((data = in.read()) != null){
                System.out.println(data);
           }
       }
       catch(Exception e)
      {
          e.printStackTrace();
      }
  } 

   public static void main (String[] args)
   {
    try
       {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the two file paths:");
        String s1 = br.readLine();
        String s2 = br.readLine();
        Ch3Ex4 thread1 = new Ch3Ex4(s1);
        Ch3Ex4 thread2 = new Ch3Ex4(s2);
        thread1.start();
        thread2.start();
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
    }
  }