2012-09-10 25 views
1

我想重新打開文件。我在輸入流中有一個文件。我曾嘗試使用掃描儀並使用BufferedReader。但是,使用close()方法關閉文件後,我無法再次打開該文件。請幫助如何再次打開文件。 我已經寫了下面的代碼:如何從輸入流重新打開文件

InputStream filename = getAttachstream(); 

     int rows =0 ; 

     BufferedReader br= new BufferedReader(new InputStreamReader(filename)); 
     String strLine = ""; 
      try { 
      while((strLine = br.readLine()) != null) { 
       rows++; 
       } 
      //br.reset(); 
      br.close(); 
      //br.reset(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
if(rows>0){ 
      InputStream filename1 = getAttachstream(); 
      Scanner inputStream1 = new Scanner(filename1); 
       for (int rowIncr = 1; inputStream1.hasNext(); rowIncr++) { 

       String data; 
       try { 
        data = br.readLine(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       String [] values = data.split(","); 
       String curRowPartNumber = values[0]; 
       String curRowQuantity = values[1]; 
       if(rowIncr == 1) 
       { 
        if((values[0]==null || values[0].trim().length()<=0) 
          || (values[1]==null || values[1].trim().length()<=0) 
          || (values[2] != "") || !"Part Number".equalsIgnoreCase(values[0].trim()) 
          || !"Quantity".equalsIgnoreCase(values[1].trim())){ 
         System.out.println("Invalid Excel sheet data"); 
         throw new ECApplicationException(ECMessage._ERR_CMD_INVALID_DATAFORMAT, CLASSNAME,methodName); 
        } 

       } 
+0

你寫道: 「但沒有任何工程」。我們問:有沒有錯誤,錯誤信息是什麼? – bpgergo

+0

getAttachstream()是什麼? – stacker

+0

您不應該需要讀取任何文件兩次。你想要解決什麼實際問題? – EJP

回答

6

一旦流,讀者,作家,插座或任何其他資源已關閉,無法再次打開它。

如果您想多次讀取文件,則需要具有其文件名。

+1

只需添加到您的答案,一旦流封裝器關閉,它也關閉所有基礎流也被關閉。 – gigadot

+0

要添加到彼得的答案:「如果你想多次讀取一個文件,你需要有它的文件名。」換句話說,您必須再次將'getAttachstream()''重新分配給'filename1',**和**您必須重新將'new Scanner(filename1)'重新分配給'inputStream1'。在你分析過一次文件(更多)之後執行這兩件事,然後回到文件的開頭。 – Andrei

1

我假定你的意思是重新打開你從getAttachstream得到的InputStream(即使它沒有顯示它來自文件)。

唯一的選擇是getAttachstream返回實現此類方法的類。請記住,即使FileInputStream不提供此類選項。而且,即使你找到了具體的類,它碰巧有這樣的方法,因爲方法的定義返回一個InputStream你不能確定它總是會返回相同的類(或者甚至在所有的環境中都會這樣是返回的類)。

唯一的選擇是使用原始inputStream並將其寫入臨時文件或ByteArrayOutputStream(如果文件足夠小以至於不能使用太多內存),那麼您可以多次訪問數據。

0

您可以使用掃描儀如果在此掃描器的輸入另一行數行Scanner.hasNextLine()

返回true。此方法可能會在等待輸入時阻塞。掃描儀不會超過任何輸入。

File file = new File("C:/test.txt"); 
    File file1 = new File("C:/test1.txt"); 

    Scanner scanner; 
    try { 
     FileInputStream fileInputStream = new FileInputStream(file); 
     FileInputStream fileInputStream1 = new FileInputStream(file1); 
     scanner = new Scanner(fileInputStream); 
     int count = 0; 
     while (scanner.hasNextLine()) { 
      String line = scanner.nextLine(); 
      count++; 
     } 
     scanner.close(); 
     System.out.println("File 1 Count:" + count); 
     scanner = new Scanner(fileInputStream1); 
     count = 0; 
     while (scanner.hasNextLine()) { 
      String line = scanner.nextLine(); 
      count++; 
     } 
     System.out.println("File 2 Count:" + count); 
     scanner.close(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
相關問題