我需要使用多個線程讀取兩個文件和打印文件的內容在console.The用戶將輸入的文件路徑,並使用線程讀取文件的內容。我很難理解文件。任何人都可以建議我在運行方法中應該做什麼?文件和多線程
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();
}
}
}
使用線程讀取文件的要點是什麼?它不會加速操作,這是I/O限制... – assylias
我正在做一些練習,在線爲JAVA.This問在問題中,所以我需要使用線程。 – Robin