Note that there are some explanatory texts on larger screens.

plurals
  1. POJava exceptions at different functions
    text
    copied!<p>Hello there/ I am new to java and I am trying to make a program that parses through a txt file and scan its content line by line. Well,in the begging I designed the program without any object or classes apart the main and it worked fine. Then I wanted to design a new class with the content from the main function but it fails to compile and I get many exeption errors. Here is the code:</p> <pre><code>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()&gt;2 &amp;&amp; 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()&gt;2 &amp;&amp; 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()&gt;2 &amp;&amp; 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()&gt;2 &amp;&amp; strLine.substring(0,3).equals("D3:") ){ System.out.print("DATE"+date); strLine = br.readLine(); date=strLine; test.set_date(date); } if(strLine.length()&gt;2 &amp;&amp; strLine.substring(0,3).equals("R1:") ){ strLine = br.readLine(); reference=strLine; System.out.print("Reference"+reference); test.set_reference(reference); } br.close(); } } } </code></pre> <p>and these are the errors I receive:</p> <pre><code> 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.&lt;init&gt;(Reader.java:28) at FileReader.main(FileReader.java:14) </code></pre> <p>What confuses me is the fact that I received these errors when I changed the file the code was. Before I got no errors at compilation and the program worked fine. The new file (Reader.java) is at the same directory like the file the main function is. I work at the Eclipse IDE. Thx in advnace</p> <p>Edit: I added the throw exception at the function and thus changed the console output</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload