不同函数的Java异常
您好/我是java新手,我正在尝试制作一个程序,通过txt文件进行解析并逐行扫描其内容。嗯,在《乞讨》中,我设计了一个程序,除了主程序之外没有任何对象或类,它工作得很好。然后我想用main函数中的内容设计一个新类,但它无法编译,并且我得到了许多验证错误。代码如下:不同函数的Java异常,java,exception,Java,Exception,您好/我是java新手,我正在尝试制作一个程序,通过txt文件进行解析并逐行扫描其内容。嗯,在《乞讨》中,我设计了一个程序,除了主程序之外没有任何对象或类,它工作得很好。然后我想用main函数中的内容设计一个新类,但它无法编译,并且我得到了许多验证错误。代码如下: import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; i
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Reader {
Reader(String file) throws Exception{
Entry test= new Entry();
File f= new File("C:/Users/Mario/Documents/Visual Studio 2013/Projects/Entry/Entry/a.txt");
System.out.print(f.exists()); // true is printed on the screen when this was at main
FileInputStream fstream = new FileInputStream("C:/Users/Mario/Documents/Visual Studio 2013/Projects/Entry/Entry/a.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
String tag="";
String date="";
int id;
String title="",description="",reference="";
while ((strLine = br.readLine()) != null){
System.out.print(strLine+ "\n");
if(strLine.length()>2 && strLine.substring(0,3).equals("I1:") ){
System.out.print("id detected\n");
id=-1;
strLine = br.readLine();
id=Integer.parseInt(strLine);
//System.out.print("ID:"+id+"\n");
test.set_id(id);
continue;
}
if(strLine.length()>2 && strLine.substring(0,3).equals("T1:") ){
System.out.print("title detected\n");
title="";
strLine = br.readLine();
title=strLine;
//System.out.print("Title:"+title+"\n");
test.set_title(title);
continue;
}
if(strLine.length()>2 && strLine.substring(0,3).equals("D1:") ){
System.out.print("description detected\n");
StringBuilder descrp=new StringBuilder();
description="";
while(true){
strLine = br.readLine();
if(strLine.equals("D2:")) break;
else descrp.append(strLine);
continue;
}
description=descrp.toString();
//System.out.print(description);
test.set_description(description);
}
if(strLine.length()>2 && strLine.substring(0,3).equals("D3:") ){
System.out.print("DATE"+date);
strLine = br.readLine();
date=strLine;
test.set_date(date);
}
if(strLine.length()>2 && strLine.substring(0,3).equals("R1:") ){
strLine = br.readLine();
reference=strLine;
System.out.print("Reference"+reference);
test.set_reference(reference);
}
br.close();
}
}
}
以下是我收到的错误:
true
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Reader.<init>(Reader.java:28)
at FileReader.main(FileReader.java:14)
true
线程“main”java.io.IOException中的异常:流已关闭
在java.io.BufferedReader.ensureOpen(未知源)
位于java.io.BufferedReader.readLine(未知源)
位于java.io.BufferedReader.readLine(未知源)
at Reader.(Reader.java:28)
位于FileReader.main(FileReader.java:14)
让我困惑的是,当我更改代码所在的文件时,我收到了这些错误。在此之前,我没有在编译和程序运行良好的错误。新文件(Reader.java)与主功能所在的文件位于同一目录中。我在EclipseIDE工作。广告中的Thx
编辑:我在函数中添加了抛出异常,因此更改了控制台输出,
br.close()
语句位于错误的位置。它在循环结束时被关闭,因此当您在循环开始时获得更多时,流就被关闭。您只需将br.close
移动到循环外部。那么…为什么要在构造函数中执行所有这些操作?此外,异常非常简单-因为您在那里使用的是一个文件,您可以将该文件的用法包装在try…catch
块中,或者将抛出IOException
添加到方法的签名中(在本例中,它将是您的构造函数).在我将这段代码移到另一个文件中之前,我不明白为什么没有异常。基本上没有这些问题。我愿意打赌,main
有一个throws
或try…catch
块。如果没有,您的main方法也不会编译。我尝试了您所说的,这是控制台中的输出:线程“main”java.io.IOException中的异常:流在java.io.BufferedReader.ensureOpen(未知源)在java.io.BufferedReader.readLine(未知源)在java.io.BufferedReader.readLine(未知源)在Reader。(Reader.java:28)在FileReader.main(FileReader.java:14)@Mario,在循环结束后关闭它。只需使用close语句,一个括号downYes,正如@peeskillet在评论中告诉我的那样。我非常了解C/C++,我想我会得到一个类似segmentation fault.Thx的错误来帮助我!