我一直在学习并叠加一个问题。我尝试从文本文件中搜索特定的名称和员工编号。我试图研究网上,但我没有找到特别的结果。如何从文本文件中提取文字
我怎样才能解决这个问题:
.\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。 –