0

複数のスレッドを使用して 2 つのファイルを読み取り、ファイルの内容をコンソールに出力する必要があります。ユーザーはファイル パスを入力し、スレッドを使用してファイルの内容を読み取ります。ファイルを理解するのに苦労しています。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();
    }
  }
 }
4

3 に答える 3

2

メソッドは、そのrunスレッドによって実行されるコードを含むことを意図しています。したがって、2 つの異なるスレッドから 2 つのファイルを読み取りたい場合runは、メソッドで行っていることを行うために を使用する必要がありますReadFile

新しいスレッドを開始するには、Ch3Ex4クラスのインスタンスを作成してメソッドを呼び出す必要があることに注意してください。start

編集:その場合、次のようにメソッドBufferedReader内で使用できます:( mkyongのウェブサイトから)run

    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() メソッドを呼び出す必要があります。さらに、ファイル名のインスタンス変数を作成し、2 つのスレッドに対して 2 つのオブジェクトを作成する必要があります。強調表示された変更を参照してください。

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 に答える