azsat
asked on
BufferedReader returning Stream closed when passed a valid Reader
Hi ,
I have the 2 clasess below. Stock is basically instantiated in class stockPrices, properties are obtained sucessfully from a properties file and then an attemtempt is made to read a flat file of tck prices via the getDataFileFromDir method.
However, as soon as I return from //get the stock File I see the message back from "getting file ref" and suddenlyI get a java message - Stream closed. Not sure why this is happening. strTargetfile is just fine. I know that the test if (inString.ready()) may not be necessary, I don't get far as IN DATA READ.
Please help!
Code snippets (simplified for clarity):
public class stockPrices
{
Stock datastock = new Stock();
//get the properties;
datastock.getProperties();
//get the stock File
BufferedReader inString = datastock.getDataFileFromD ir(sDate);
System.Out.println("back from getting file ref");
if (inString.ready()) // data to read
{
System.Out.println("IN DATA READ");
...... inString manipulation stuff }
else
{
returnCode = 0;
System.out.println("No data for stock file date: " + sDate);
}
}
public class Stock
{
public BufferedReader getDataFileFromDir(String strDate) throws FileNotFoundException
{
BufferedReader inString = null;
try
{ // desFolder is obtained from a properties file entry is desFolder= C:\\batch_data\\
String strTargetfile= this.desFolder + strDate + "_v4.dat";
inString = new BufferedReader(new FileReader(strTargetfile)) ;
}
catch (FileNotFoundException e) {
System.out.println(" file not found for date: " +strDate);
}
catch (IOException ioe) {
System.out.println(ioe.get Message()) ;
}
return inString;
}
}
I have the 2 clasess below. Stock is basically instantiated in class stockPrices, properties are obtained sucessfully from a properties file and then an attemtempt is made to read a flat file of tck prices via the getDataFileFromDir method.
However, as soon as I return from //get the stock File I see the message back from "getting file ref" and suddenlyI get a java message - Stream closed. Not sure why this is happening. strTargetfile is just fine. I know that the test if (inString.ready()) may not be necessary, I don't get far as IN DATA READ.
Please help!
Code snippets (simplified for clarity):
public class stockPrices
{
Stock datastock = new Stock();
//get the properties;
datastock.getProperties();
//get the stock File
BufferedReader inString = datastock.getDataFileFromD
System.Out.println("back from getting file ref");
if (inString.ready()) // data to read
{
System.Out.println("IN DATA READ");
...... inString manipulation stuff }
else
{
returnCode = 0;
System.out.println("No data for stock file date: " + sDate);
}
}
public class Stock
{
public BufferedReader getDataFileFromDir(String strDate) throws FileNotFoundException
{
BufferedReader inString = null;
try
{ // desFolder is obtained from a properties file entry is desFolder= C:\\batch_data\\
String strTargetfile= this.desFolder + strDate + "_v4.dat";
inString = new BufferedReader(new FileReader(strTargetfile))
}
catch (FileNotFoundException e) {
System.out.println(" file not found for date: " +strDate);
}
catch (IOException ioe) {
System.out.println(ioe.get
}
return inString;
}
}
The stream is getting closed, can you post the full code for getDataFileFromDir()
ASKER
I am indeed doing:
String line = null;
while ((line = inString.readLine()) != null)
but IN the if (inString.ready()) // data to read
block.
Here is the full method getDataFileFromDir()
public BufferedReader getDataFileFromDir(String strDate) throws FileNotFoundException
{
BufferedReader inString = null;
try
{
String strTargetfile= this.desFolder + strDate + "_v4.dat";
inString = new BufferedReader(new FileReader(strTargetfile)) ;
}
catch (FileNotFoundException e) {
System.out.println(" file not found for date: " +strDate);
try
{
inString.close();
}
catch (IOException ioe) {}
}
catch (IOException ioe) {
cat.error(ioe.getMessage() );
//System.out.println(ioe.g etMessage( ));
try
{
inString.close();
}
catch (IOException e) {}
}
catch (Exception e)
{ cat.error(e.getMessage());
//System.out.println(e.get Message()) ;
try
{
inString.close();
}
catch (IOException ie) {}
}
return inString;
}
String line = null;
while ((line = inString.readLine()) != null)
but IN the if (inString.ready()) // data to read
block.
Here is the full method getDataFileFromDir()
public BufferedReader getDataFileFromDir(String strDate) throws FileNotFoundException
{
BufferedReader inString = null;
try
{
String strTargetfile= this.desFolder + strDate + "_v4.dat";
inString = new BufferedReader(new FileReader(strTargetfile))
}
catch (FileNotFoundException e) {
System.out.println(" file not found for date: " +strDate);
try
{
inString.close();
}
catch (IOException ioe) {}
}
catch (IOException ioe) {
cat.error(ioe.getMessage()
//System.out.println(ioe.g
try
{
inString.close();
}
catch (IOException e) {}
}
catch (Exception e)
{ cat.error(e.getMessage());
//System.out.println(e.get
try
{
inString.close();
}
catch (IOException ie) {}
}
return inString;
}
You need to check (as i mentioned earlier) that no excpetions are thrown
You should probably simply return null if there's a problem and then do not
>>if (inString.ready()) // data to read
but
if (inString != null) // data to read
>>if (inString.ready()) // data to read
but
if (inString != null) // data to read
> cat.error(ioe.getMessage() );
is this error occurring?
is this error occurring?
> public BufferedReader getDataFileFromDir(String strDate) throws FileNotFoundException
instead of checking ready(), it would be safer to have that method return an IOException
ready() could be false for more reasons than it just failing to open.
public BufferedReader getDataFileFromDir(String strDate) throws IOException
And in the method re-throw the exception if it occurs
instead of checking ready(), it would be safer to have that method return an IOException
ready() could be false for more reasons than it just failing to open.
public BufferedReader getDataFileFromDir(String strDate) throws IOException
And in the method re-throw the exception if it occurs
ASKER
Thanks, I guess there could be an exception with new FileReader(strTargetfile)) ; however,
I'ven been able to verify that strTargetfile is a correct directory path to the file.
Incidentially, this code is running as a java application in Websphere Application Developer.
and the data file is on my C:\ drive.
The same classes run just fine on Linux!!! where I have simply changed the desFolder to point to a file on Linux and of course ftp'd the properties file as well.
Does this help?
I'ven been able to verify that strTargetfile is a correct directory path to the file.
Incidentially, this code is running as a java application in Websphere Application Developer.
and the data file is on my C:\ drive.
The same classes run just fine on Linux!!! where I have simply changed the desFolder to point to a file on Linux and of course ftp'd the properties file as well.
Does this help?
ASKER
Sorry Objects,
The cat. stuff has been removed from the code (not using log4j stuff at the mo), I give you the wrong commented version, but the System.out messages are all there.
ie.
//cat.error(ioe.getMessage ());
System.out.println(ioe.get Message()) ;
Are you stuspecting that System.out.println(ioe.get Message()) is giving me Stream closed?
The cat. stuff has been removed from the code (not using log4j stuff at the mo), I give you the wrong commented version, but the System.out messages are all there.
ie.
//cat.error(ioe.getMessage
System.out.println(ioe.get
Are you stuspecting that System.out.println(ioe.get
> Are you stuspecting that System.out.println(ioe.get Message()) is giving me Stream closed?
No, I'm suspecting an error has occurred somwwhere,
Check it is actaully finding the file and opening it correctly.
No, I'm suspecting an error has occurred somwwhere,
Check it is actaully finding the file and opening it correctly.
add some debug after it is opened:
String strTargetfile= this.desFolder + strDate + "_v4.dat";
inString = new BufferedReader(new FileReader(strTargetfile)) ;
System.out.println("opened successfully "+strTargetfile);
String strTargetfile= this.desFolder + strDate + "_v4.dat";
inString = new BufferedReader(new FileReader(strTargetfile))
System.out.println("opened
you don't read from the stream at all in getDataFileFromDir() do you?
>>instead of checking ready(), it would be safer to have that method return an IOException
? The method that gets the Reader should return null if there's a problem as i mentioned earlier
? The method that gets the Reader should return null if there's a problem as i mentioned earlier
Your method can be simplified as follows:
public BufferedReader getDataFileFromDir(String strDate)
{
try
{
String strTargetfile = this.desFolder + strDate + "_v4.dat";
return new BufferedReader(new FileReader(strTargetfile)) ;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
public BufferedReader getDataFileFromDir(String strDate)
{
try
{
String strTargetfile = this.desFolder + strDate + "_v4.dat";
return new BufferedReader(new FileReader(strTargetfile))
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
ASKER
> The same classes run just fine on Linux!!! where I have simply changed the desFolder to point to a file on Linux and of course ftp'd the properties file as well.
I expected near enough complete portability, and was suprised to get the error as the code
goes into if (inString.ready()) // data to read block and I see the data lines read!
However, I won't be able to put in the extra messages to trap the exceptions right now as I've had to leave work for the day.
I'll provide feedback tommorow around 2:00AM PDT . In the mean time if something looks obvious - please say!
NB the file is is just a flat file of string data.
I expected near enough complete portability, and was suprised to get the error as the code
goes into if (inString.ready()) // data to read block and I see the data lines read!
However, I won't be able to put in the extra messages to trap the exceptions right now as I've had to leave work for the day.
I'll provide feedback tommorow around 2:00AM PDT . In the mean time if something looks obvious - please say!
NB the file is is just a flat file of string data.
Don't forget you need forward slashes in paths on Unix ;-)
Theres nothing wrong with the code. Run this (create test.txt):
import java.io.*;
public class ExpTest {
public static void main(String[] args){
Stock datastock = new Stock();
//get the stock File
try{
BufferedReader inString = datastock.getDataFileFromD ir("x");
if (inString.ready()) // data to read
{
System.out.println("IN DATA READ"); }
else
{
//returnCode = 0;
System.out.println("No data for stock file date: ");
}
}catch(Exception e){
System.out.println("urrghh h");
}
}
}
class Stock
{
public BufferedReader getDataFileFromDir(String strDate) throws FileNotFoundException
{
BufferedReader inString = null;
try
{ // desFolder is obtained from a properties file entry is desFolder= C:\\batch_data\\
String strTargetfile= "test.txt";
inString = new BufferedReader(new FileReader(strTargetfile)) ;
}
catch (FileNotFoundException e) {
System.out.println(" file not found for date: " +strDate);
}
catch (IOException ioe) {
System.out.println(ioe.get Message()) ;
}
return inString;
}
}
Therefore it must be either that the file you're trying to open is empty, in which case it will return that is not 'ready()' or the file doesn't exist in which case you'll get an exception.
import java.io.*;
public class ExpTest {
public static void main(String[] args){
Stock datastock = new Stock();
//get the stock File
try{
BufferedReader inString = datastock.getDataFileFromD
if (inString.ready()) // data to read
{
System.out.println("IN DATA READ"); }
else
{
//returnCode = 0;
System.out.println("No data for stock file date: ");
}
}catch(Exception e){
System.out.println("urrghh
}
}
}
class Stock
{
public BufferedReader getDataFileFromDir(String strDate) throws FileNotFoundException
{
BufferedReader inString = null;
try
{ // desFolder is obtained from a properties file entry is desFolder= C:\\batch_data\\
String strTargetfile= "test.txt";
inString = new BufferedReader(new FileReader(strTargetfile))
}
catch (FileNotFoundException e) {
System.out.println(" file not found for date: " +strDate);
}
catch (IOException ioe) {
System.out.println(ioe.get
}
return inString;
}
}
Therefore it must be either that the file you're trying to open is empty, in which case it will return that is not 'ready()' or the file doesn't exist in which case you'll get an exception.
Hi zasat,
You've specified in code that getDataFileFromDir() function will throws the FileNotFoundException, but inside the function, in the catch block, do closing stream instead and no such exception throwing.
I very quite sure that the command
try
{
inString.close();
}
catch (IOException ioe) {}
cause your stream closed. This mean that an exception occur while openning the file .dat. Maybe it was locked by another application or did not exists on path.
You've specified in code that getDataFileFromDir() function will throws the FileNotFoundException, but inside the function, in the catch block, do closing stream instead and no such exception throwing.
I very quite sure that the command
try
{
inString.close();
}
catch (IOException ioe) {}
cause your stream closed. This mean that an exception occur while openning the file .dat. Maybe it was locked by another application or did not exists on path.
ASKER
Hi,
Well I've discovered that the problem is with the entry for desFolder in the properties file.
The previous entry was desFolder= C:\\batch_data\\ as mentioned above.
What I did was a bit of hard coding and put
new FileReader("C:\\batch_data \\D2004091 0_v4.dat")
this worked so in the properties file I put desFolder= C:\\\batch_data\\\ and the code works OK.
Note the three back slashes. Now this works. My only question is - is this the right thing to do for reading in a file sitting on my C drive?
Well I've discovered that the problem is with the entry for desFolder in the properties file.
The previous entry was desFolder= C:\\batch_data\\ as mentioned above.
What I did was a bit of hard coding and put
new FileReader("C:\\batch_data
this worked so in the properties file I put desFolder= C:\\\batch_data\\\ and the code works OK.
Note the three back slashes. Now this works. My only question is - is this the right thing to do for reading in a file sitting on my C drive?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
Ok, but I'm still unclear what the entry should be in properties file , I do not want to hard code the file name in te properties file, I just want the path of the location in there.
desFolder= ?
NB
desFolder= C:\batch_data\
or desFolder= C:\\batch_data\\
did not work.
Ok, but I'm still unclear what the entry should be in properties file , I do not want to hard code the file name in te properties file, I just want the path of the location in there.
desFolder= ?
NB
desFolder= C:\batch_data\
or desFolder= C:\\batch_data\\
did not work.
try:
desFolder=C:/batch_data/
desFolder=C:/batch_data/
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok Guys, let me try these options out - hopefully I'll be able to tie this up tommorow.
8-)
like i said, the problem was not with the code, just the file retrieval mechanism. thanks for the acknowledgment.
should be replaced by
String line = null;
while ((line = inString.readLine()) != null) // data to read
Are you sure you're not getting exceptions beforehand?