0

我需要使用多个线程读取两个文件并在控制台中打印文件的内容。用户将输入文件路径并使用线程读取文件的内容。我很难理解文件。谁能建议我在运行方法中应该做什么?

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();
    }
  }
 }
4

3 回答 3

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();
        }
    }
于 2013-01-13T11:41:49.470 回答
0

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

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();
       }
    }
 }
于 2013-01-13T12:11:08.820 回答
-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();
       }
    }
  }
于 2013-01-13T12:05:01.410 回答