2013-01-13 26 views
0

我需要使用多個線程讀取兩個文件和打印文件的內容在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(); 
    } 
    } 
} 
+2

使用線程讀取文件的要點是什麼?它不會加速操作,這是I/O限制... – assylias

+0

我正在做一些練習,在線爲JAVA.This問在問題中,所以我需要使用線程。 – Robin

回答

2

run方法是爲了包含由該線程執行的代碼。因此,當你想從兩個不同的線程讀取兩個文件,你需要使用run做你在ReadFile方法做什麼。

請注意,您需要創建Ch3Ex4類的實例並調用start方法才能開始啓動新線程。

編輯:在這種情況下,你可以使用一個BufferedReaderrun方法是這樣的:(從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(); 
     } 
    } 
+0

是啊,我已經創建的實例,並調用啓動method.I我只是想弄清楚的步驟閱讀和打印。 – Robin

+0

我補充說,我現在答案。 – Swapnil

+0

但什麼是sCurrentLine持有嗎?它只是一個聲明的字符串。 – Robin

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