Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件和多线程_Java_Eclipse_Multithreading - Fatal编程技术网

Java 文件和多线程

Java 文件和多线程,java,eclipse,multithreading,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);

我需要使用多线程读取两个文件,并在控制台中打印文件内容。用户将输入文件路径并使用线程读取文件内容。我很难理解文件。谁能建议我在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();
    }
  }
 }

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

请注意,您需要创建
Ch3Ex4
类的实例,并调用
start
方法以启动新线程

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


希望下面的代码就是你想要的

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

如前所述,您应该将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();
       }
    }
 }

使用线程读取文件有什么意义?它不会加快操作速度,这是I/O限制的…我正在为JAVA做一些在线练习。这是问题中提出的,所以我需要使用线程。是的,我已经创建了实例并调用了start方法。我只是试图找出读取和打印的步骤。我现在已经将其添加到我的答案中。但sCurrentLine在这里有什么意义?它只是一个声明的字符串。当您阅读下面的代码时,
readLine()
方法返回的
string
被分配给
sCurrentLine
,然后打印到输出控制台。但还有一个疑问……发送字符串怎么样?
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();
       }
    }
 }