3 回答

TA貢獻1836條經(jīng)驗 獲得超5個贊
您可以使用 a
FileFilter
僅讀取一種或另一種類型,然后做出相應(yīng)的響應(yīng)。它會給你一個List
僅包含所需類型的文件。第二個要求讓我感到困惑。我認為通過創(chuàng)建一個類來封裝您想要解析的數(shù)據(jù)和行為,您會得到很好的幫助
Resume
。編寫一個工廠類,它接受InputStream
并生成Resume
包含您需要的數(shù)據(jù)的工廠類。
您犯了一個典型的錯誤:您將所有邏輯嵌入到主方法中。這將使測試您的代碼變得更加困難。
所有的問題解決都是把大問題分解成小問題,解決小問題,然后組合起來最終解決大問題。
我建議您將這個問題分解為更小的類。例如,在您可以讀取和解析單個 PDF 和 DOC 文件之前,不必擔心循環(huán)遍歷目錄中的文件。
創(chuàng)建一個接口:
public?interface?ResumeParser?{ ????Resume?parse(InputStream?is)?throws?IOException; }
為 PDF 和 Word Doc 實施不同的實現(xiàn)。
ResumeParser
創(chuàng)建一個工廠以根據(jù)文件類型為您提供適當?shù)模?/p>
public class ResumeParserFactory {
? ? public ResumeParser create(String fileType) {
? ? ? ? if (fileType.contains(".pdf") {
? ? ? ? ? ?return new PdfResumeParser();? ? ? ? ? ??
? ? ? ? } else if (fileType.contains(".doc") {
? ? ? ? ? ?return new WordResumeParser();
? ? ? ? } else {
? ? ? ? ? ?throw new IllegalArgumentException("Unknown document type: " + fileType);
? ? ? ? }
? ? }
}
請務(wù)必在進行時編寫單元測試。您應(yīng)該知道如何使用JUnit。

TA貢獻1877條經(jīng)驗 獲得超6個贊
使用 a 的另一種替代方法FileFilter是使用 a DirectoryStream,因為Files::newDirectoryStream可以輕松指定相關(guān)的文件結(jié)尾:
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{doc,pdf}")) {
for (Path entry: stream) {
// process files here
}
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration, the cause is an IOException
throw ex.getCause();
}
}

TA貢獻1831條經(jīng)驗 獲得超4個贊
你可以做一些基本的事情,比如:
// Put the path to the folder containing all the resumes here
File f = new File("C:\\");
ArrayList<String> names = new ArrayList<>?
(Arrays.asList(Objects.requireNonNull(f.list())));
for (String fileName : names) {
? ?if (fileName.length() > 3) {
? ? ? ?String type = fileName.substring(fileName.length() - 3);
? ? ? ?if (type.equalsIgnoreCase("doc")) {
? ? ? ? ? ?// doc file logic here
? ? ? ?} else if (type.equalsIgnoreCase("pdf")) {
? ? ? ? ? ?// pdf file logic here
? ? ? ?}
? ? }
}
添加回答
舉報