2016-11-22 85 views
0

我看到下面的代碼,下面的代碼查看目錄,然後打印所有不同的文件名。現在我的問題是,我將如何去改變我的代碼,以便它能打印出它找到/打印的文件中的所有內容?例如,假設代碼在目錄中找到3個文件,那麼它會打印出這3個文件中的所有內容。從目錄中的多個文件打印所有內容

import java.io.File; 
import java.io.IOException; 

public class EScan { 


static String usernamePc = System.getProperty("user.name"); 
final static File foldersPc = new File("/Users/" + usernamePc + "/Library/Mail/V2"); 

public static void main(String[] args) throws IOException { 

    listFilesForFolder(foldersPc); 

} 

public static void listFilesForFolder(final File foldersPc) throws IOException { 
    for (final File fileEntry : foldersPc.listFiles()) { 
     if (fileEntry.isDirectory()) { 
      listFilesForFolder(fileEntry); 
     } else { 
      System.out.println(fileEntry.getName()); 
     } 
    } 
} 

} 

回答

0

您可以使用掃描儀讀取文件

try { 
     Scanner sc = new Scanner(fileEntry); 

     while (sc.hasNextLine()) { 
      String s = sc.nextLine(); 
      System.out.println(s); 
     } 

     sc.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
} 
+0

我欣賞代碼,我會在哪裏添加此代碼? – fsharpsquare

+0

我剛剛測試過,我把它放在else語句中,它似乎在工作!非常感謝你。如果您有足夠的時間,您是否知道是否有辦法打印文件中的電子郵件。所以就像是如果它找到包含.com或@的單詞,那麼它會打印該單詞? – fsharpsquare

+0

如果字符串是有效的電子郵件,您可以先使用正則表達式進行驗證。 http://stackoverflow.com/questions/8204680/java-regex-email –

1

我發佈前測試它的內容。這是工作。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* @author EdwinAdeola 
*/ 
public class TestPrintAllFiles { 

    public static void main(String[] args) { 
     //Accessing the folder path 
     File myFolder = new File("C:\\Intel"); 
     File[] listOfFiles = myFolder.listFiles(); 
     String fileName, line = null; 
     BufferedReader br; 
     //For each loop to print the content of each file 
     for (File eachFile : listOfFiles) { 
      if (eachFile.isFile()) { 
       try { 
        //System.out.println(eachFile.getName()); 
        fileName = eachFile.getAbsolutePath(); 
        br = new BufferedReader(new FileReader(fileName)); 

        try { 
         while ((line = br.readLine()) != null) { 
          System.out.println(line); 
         } 
        } catch (IOException ex) { 
         Logger.getLogger(TestPrintAllFiles.class.getName()).log(Level.SEVERE, null, ex); 
        } 
       } catch (FileNotFoundException ex) { 
        Logger.getLogger(TestPrintAllFiles.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
     } 
    } 
} 
+0

@fsharpsquare請測試代碼,讓我。 – Edwinfad

+0

我欣賞代碼,我剛剛測試過,雖然它沒有任何問題編譯。代碼的輸出是一些非常奇怪的人物,像一個黑色的方塊,在裏面。我不確定它是否與它正在閱讀的實際文件有關。我的文件夾中的文件是.emlx – fsharpsquare

0

你可以嘗試另一種方式,如果你尋找合適的:

package com.grs.stackOverFlow.pack10; 

import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.util.Arrays; 
import java.util.List; 

public class EScan { 

    public static void main(String[] args) throws IOException { 

     File dir=new File("C:/your drive/"); 
     List<File> files = Arrays.asList(dir.listFiles(f->f.isFile())); 
//if you want you can filter files like f->f.getName().endsWtih(".csv") 

     for(File f: files){ 
      List<String> lines = Files.readAllLines(f.toPath(),Charset.defaultCharset()); 
      //processing line 
      lines.forEach(System.out::println); 
     } 
    } 
} 

上面的代碼可以我像生產線的方式號碼利用,可以修改,添加圍繞如下線路報價:

lines.stream().map(t-> "'" + t+"'").forEach(System.out::println); 

或打印唯一的錯誤消息系

lines.stream().filter(l->l.contains("error")).forEach(System.out::println); 

以上代碼和變體都經過測試。

+0

我欣賞代碼,我剛剛測試過它。它在開始時沒有任何錯誤,但編譯代碼時會遇到一些不同的錯誤。請看看這個屏幕截圖。 (https://snag.gy/keN3Wo.jpg) – fsharpsquare

+0

@fsharpsquare:我已經添加了新的更新,以便在讀取文件時使用默認字符集。它現在應該工作。但如果問題仍然存在,你可以嘗試改變Files.readAllLines(f.toPath(),Charset.forName(「UTF-8」)等字符集; – grsdev7

相關問題