Error reading in files

J

Jeffery Miller

Hi all. I am a novice Java programmer trying to read in a text file line by
line. I have successfully done this many times before, but trying to do it
now, I am having trouble. In the past, the files that I read in were only a
few kilobytes. This time, the file is about 500k so I think that's the
problem. It reads in about 90 lines and then I get Exception in thread
"main" java.io.IOException: Stream closed. Here's the relevant code that I
am using:

while ((nextLine = br.readLine()) != null){
System.out.println("Start");
len=nextLine.length();
System.out.println("Length = " + len );

System.out.println(nextLine);
if (len > 0){
System.out.println("yes");
while (j <= 14) {
a=nextLine.charAt(j);
tempbuff.append(a);
j++;
}

Any help would be greatly appreciated.

Thanks.
 
J

Jeffery Miller

Oh, and br is a BufferedReader defined as follows:

FileReader in = new FileReader(inputFile);
BufferedReader br = new BufferedReader(in);
 
P

Paul Lutus

Jeffery said:
Hi all. I am a novice Java programmer trying to read in a text file line
by
line. I have successfully done this many times before, but trying to do
it
now, I am having trouble. In the past, the files that I read in were only
a
few kilobytes. This time, the file is about 500k so I think that's the
problem. It reads in about 90 lines and then I get Exception in thread
"main" java.io.IOException: Stream closed. Here's the relevant code that
I am using:

while ((nextLine = br.readLine()) != null){
System.out.println("Start");
len=nextLine.length();
System.out.println("Length = " + len );

System.out.println(nextLine);
if (len > 0){
System.out.println("yes");
while (j <= 14) {

What's this? How can you be sure that the lines are this length? Never make
this kind of assumption. Instead:

while (j <= 14 && j < nextLine.length) {
a=nextLine.charAt(j);
tempbuff.append(a);
j++;
}

Also, always post a short, complete, compilable, working example when you
post code.
 
J

Jeffery Miller

I made your change but it still fails.

Paul Lutus said:
What's this? How can you be sure that the lines are this length? Never make
this kind of assumption. Instead:

while (j <= 14 && j < nextLine.length) {


Also, always post a short, complete, compilable, working example when you
post code.
 
P

Paul Lutus

Jeffery said:
I made your change but it still fails.

1. If by "your change" you mean my suggestion to move the Vector creation
step into the loop, what you have not noticed is that the previously
reported error, that every line is the same, is no longer true.

2. Do not top-post.

3. Please post the error message and the stack trace, along with a short,
complete, compilable example program.
 
J

John C. Bollinger

Jeffery said:
Oh, and br is a BufferedReader defined as follows:

FileReader in = new FileReader(inputFile);
BufferedReader br = new BufferedReader(in);

Very likely that is _not_ the relevant code, or at least not all of it.
Create a small, self-contained example and post the whole code.

<gaze into="crystal ball">
A good candidate for the problem is that an exception is being thrown
from the Reader, but you are sending the program back into the read
loop. An alternative might be that the output is pointed at the same
file as the input.
</gaze>


John Bollinger
(e-mail address removed)
 
J

Jeffery Miller

Paul Lutus said:
1. If by "your change" you mean my suggestion to move the Vector creation
step into the loop, what you have not noticed is that the previously
reported error, that every line is the same, is no longer true.

2. Do not top-post.

3. Please post the error message and the stack trace, along with a short,
complete, compilable example program.

The error message is as follows:
Exception in thread "main" java.io.IOException: Stream closed
at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder.java:37)
at sin.nio.cs.StreamDecoder.read(StreamDecoder.java:152)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at Exception.main(Exception.java:49)

The code is as follows:

import java.io.*;
import java.util.*;
import java.text.*;

public class Exception {

public static void main (String[] args) throws IOException {

//defining variables and opening files
String filename = new String();
filename = "Exception.S1";
File inputFile = new File("ARC45W_GB1S.001");
File outputFile = new File(filename);
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile,true);



BufferedReader br = new BufferedReader(in);
char a;
StringBuffer buffString = new StringBuffer();
StringBuffer accountnumbuff = new StringBuffer();
StringBuffer checknumbuff = new StringBuffer();
StringBuffer paiddatebuff = new StringBuffer();
StringBuffer pdamountbuff = new StringBuffer();
StringBuffer tempbuff = new StringBuffer();
StringBuffer descriptbuff = new StringBuffer();
StringBuffer issuedatebuff = new StringBuffer();
StringBuffer issueamountbuff = new StringBuffer();
StringBuffer testbuff = new StringBuffer();
String outString = new String();
String nextLine = null;
String tempstring = null;
int len;

int n=10;
int g=20;
int h=21;
int j=0;
int k=1;
int l=0;



//reading in file line by line
while ((nextLine = br.readLine()) != null){
System.out.println("Start");
len=nextLine.length();
System.out.println("Length = " + len );

System.out.println(nextLine);
if (len > 0){
System.out.println("yes");
while (j <= 14 && j < len) {
a=nextLine.charAt(j);
tempbuff.append(a);
j++;
}
tempstring = "" + tempbuff;
if (tempstring == " 435266900") {
j=6;
while (j <= 14 && j < len) {
a=nextLine.charAt(j);
accountnumbuff.append(a);
j++;
}

}


}
tempbuff.setLength(0);
System.out.println(k);
k++;
accountnumbuff.setLength(0);
checknumbuff.setLength(0);
paiddatebuff.setLength(0);
pdamountbuff.setLength(0);
descriptbuff.setLength(0);
j=0;
nextLine="";
System.out.println("END");
in.close();
out.close();



}

}
}
 
P

Paul Lutus

Jeffery said:
The error message is as follows:
Exception in thread "main" java.io.IOException: Stream closed
at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder.java:37)
at sin.nio.cs.StreamDecoder.read(StreamDecoder.java:152)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at Exception.main(Exception.java:49)

The code is as follows:

The error message says the file input stream is closed prematurely. But you
are closing the file input stream prematurely -- inside your "while" loop.
In other words, the computer is doing what you tell it to.

If you will properly indent your code, you will see that this:

in.close();
out.close();

Is located within this control structure:

while ((nextLine = br.readLine()) != null){

Notice how the "while" statement above is closer to the left margin than the
close statements that appear below it. This means the close statements are
within the while loop, not outside, which is what you probably want.

Expressed very simply, you are doing this:

while ((nextLine = br.readLine()) != null){

// lots of code here

in.close();
out.close();
}

This is why the stream closes -- you told it to.

Instead, do this:

while ((nextLine = br.readLine()) != null){

// lots of code here

}
in.close();
out.close();

Had you not posted your code, there is no way anyone could have possibly
helped you.
 
N

Nathan Zumwalt

Could another process be updating the file at the same time you're
trying to read it?

//Nathan
 
P

Paul Lutus

Nathan said:
Could another process be updating the file at the same time you're
trying to read it?

Actually, he was closing his files within his read loop. The reason this is
not obvious is because he originally posted a snippet, but one that excuded
the part he didn't think was important -- the part with the error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top