我一直在學習併疊加一個問題。我嘗試從文本文件中搜索特定的名稱和員工編號。我試圖研究網上,但我沒有找到特別的結果。如何從文本文件中提取文字
我怎樣才能解決這個問題:
.\txtFileReader.java:15: error: cannot find symbol while((line = filescan.readLine()) != null)^symbol: method readLine() location: variable filescan of type Scanner 1 error
這裏是我的代碼:
File oldFile = new File("C:\\Tesseract-OCR\\" + name + ".txt");
try (BufferedReader reader = new BufferedReader(new FileReader(oldFile)))
{
StringBuilder contents = new StringBuilder();
String text;
while ((text = reader.readLine()) != null)
{
String lineSeparator = System.getProperty("line.separator");
contents.append(text).append(lineSeparator);
System.out.println(text);
if (text.contains("*Department: @INFO. TECH"))
{
int index = text.indexOf("TECH");
text = text.substring(index, text.length());
System.out.println("Result :" + text);
}
}
}
這裏是我的Tesseract OCR轉換的文本文件:
ATTENDANCE SHEET
*Department: @INFO. TECH *Date: @18/08/2017
*Year: @THIRD *Division: @C
*Subject Code: @TBGOZ
*Teacher Code: @ITIOOZO
@IT001 @ITOOZ @IT003 @ITOO4
m5 @IT006 @IT007 @IT008
@IT009 @5910 m wm-
m3 @IT014 m @IT016
@IT017 @IT018 m @9219
@IT021 mz @593 @IT024
m @IT026 @IT027 @IT028
@IT029 @m @IT031 W
@IT033 m4 @lT035 @IT036
我想在控制檯上打印輸出:
Department: INFO. TECH
Year: THIRD
Division: C
Subject Code: @TBGOZ
Teacher Code:ITIOOZO
請告訴我上述問題的解決方案。
有幾千個關於如何使用java從文件中讀取文本的例子,因爲這是一個非常常見的(和初學者)任務。你有沒有試過看? –
我剛剛測試過你的代碼,它的工作原理。你的問題是什麼?我唯一添加的是異常處理。 –
我在編輯它之前粘貼了代碼。 「filescan」來自哪裏?我在代碼中使用閱讀器,而不是使用filescan。 –