473,562 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading a Properties file using BufferedInputSt ream

I'm trying to read a properties file using BufferedInputSt ream and getResourceAsSt ream(),but getting "java.io.IOExce ption: Stream closed' exception.

My properties file is not set in the classpath, I even tried by incuding the file as a jar in the classpath but invain.

My code snippet is:

BufferedInputSt ream bufferedinputst ream =new BufferedInputSt ream(getClass() .getClassLoader ().getResourceA sStream(s));

where 's' is the name of the properties file.

I'm running as a standalone program.

Can anyone help me out in resolving the issue.
Jul 18 '07 #1
5 11726
mailanusha
6 New Member
I'm trying to read a properties file using BufferedInputSt ream and getResourceAsSt ream(),but getting "java.io.IOExce ption: Stream closed' exception.

My properties file is not set in the classpath, I even tried by incuding the file as a jar in the classpath but invain.

My code snippet is:

BufferedInputSt ream bufferedinputst ream =new BufferedInputSt ream(getClass() .getClassLoader ().getResourceA sStream(s));

where 's' is the name of the properties file.

I'm running as a standalone program.

Can anyone help me out in resolving the issue.



The class that has the code u have given above must throw IOException
public class Test
{


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


BufferedInputSt ream bis = new BufferedInputSt ream(new FileInputStream ("mydata.proper ties"));
DataInputStream dis = new DataInputStream (bis);

try {
String st=dis.readLine ();
if(st!=null){
System.out.prin tln(st);
}

} catch (Exception e) {
e.printStackTra ce();
}
}
This is a sample code that works properly.I have placed the property file in the root path itself.
Jul 18 '07 #2
prometheuzz
197 Recognized Expert New Member
...
This is a sample code that works properly.I have placed the property file in the root path itself.
No, that is not how you're supposed to read a Properties file. Here's how:

Expand|Select|Wrap|Line Numbers
  1.  FileInputStream fis = new FileInputStream("C:/Temp/test.properties");
  2. Properties props = new Properties();
  3. props.load(fis);
  4. fis.close();
  5.  
  6. // ...
  7.  
  8. System.out.println(props.get("key"));
@OP: Are you perhaps closing the stream before you call Properties' load(...) method?
Jul 18 '07 #3
JosAH
11,448 Recognized Expert MVP
Using getResourceAsSt ream() isn't that wrong. It allows for streams to be
opened in jars etc. Just get the path to the resource right and that's it.
Specify the path to the resource either somewhere external or just relative to
where the class itself (the one that invokes the resourceAsStrea m() method)
is located. It works for me; all the time ;-)

kind regards,

Jos
Jul 18 '07 #4
prometheuzz
197 Recognized Expert New Member
Using getResourceAsSt ream() isn't that wrong. It allows for streams to be
opened in jars etc. Just get the path to the resource right and that's it.
Specify the path to the resource either somewhere external or just relative to
where the class itself (the one that invokes the resourceAsStrea m() method)
is located. It works for me; all the time ;-)

kind regards,

Jos
I was talking about the solution proposed in reply #2 where someone suggested to read the properties file line by line like an ordinary (text) file.
; )
Jul 18 '07 #5
JosAH
11,448 Recognized Expert MVP
I was talking about the solution proposed in reply #2 where someone suggested to read the properties file line by line like an ordinary (text) file.
; )
Ah, yes; I missed that; sorry about that; reading the properties thing yourself is
certainly not the way to go; the Properties object does more than reading lines,
e.g. it also converts those \u unicode escape sequences. wheels and reinventions
and all that ;-)

kind regards,

Jos

ps. I removed some more noise words from your SV text; that size on disk is
down from ~ 3.5MB to ~ 3.1MB ;-)
Jul 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3486
by: syntax | last post by:
hi, i want to read a file using fread() in equal chunks through a for loop so that at the last call i dont get error..which way, i should read it? let me give an example, suppose i have 100 chars, i can read it in 50 chars for twice or 10 chars in ten times or 25 chars for 4 times...like this. Here, file size, i.e 100 chars is known to...
5
17688
by: Drew Yallop | last post by:
I read an XML file with a stream reader in VB.Net. When I look at the stream reader output in debug mode (by passing cursor over the stream reader object)the format is a perfect replica of the file as displayed when I open the xml file in VS .net 2003 IDE. When I perform the same procedure in C# the stream reader obkect displays a chaotic...
2
3100
by: Simon Verona | last post by:
Not sure if this is the best group... it may be better off in one of the ADO groups, but I'm sure somebody here knows the answer: I'm trying to load up a text file using ADO.net, as follows: Dim TextConnectionString As String TextConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & FilePath & ";" & _
1
2203
by: JM | last post by:
Hi, I have been trying to read a file, using "StreamReader" to pass the info to an string that is: ....dim stream as new FileStream(file,...) dim sr as new StreamReader(stream) dim data as string = sr.ReadToEnd... My problem is that in the file (is a text file) there is some data like:
3
2223
by: richard.kessler | last post by:
I have the simplest need...to read a file full of file names(unc) and then check to see if each of these files exists. I tried with the following program, but always get file not found, even when it is there. If I type in the file name as a literal it works... Little program: #This module checks for file existence import string import...
2
5192
tshabza
by: tshabza | last post by:
Could anyone give me idea on how to read a wave and convert in into an integer array file using C++, for now I can successfullly read text file.
1
2781
by: tourist.tam | last post by:
Hi, I am trying to read a file using a BufferedStream and avoiding at maximum the use of string manipulation. I need now some help understanding on how to use byte array to hold the data and manipulate those. I do understand that I will have to pass any of the array created into strings at the end of the process. But I am missing the...
4
1911
by: tushar.saxena | last post by:
Hi, I'm trying to read a file using the istearm class (I cant use ifstream since the input might be a file or it might be stdin). istream *input; // Add checks for file name here, else use input = &cin; while (!input->eof())
2
5015
by: tshad | last post by:
I have a program that is reading a csv file into a dataset. I want it to read the 1st line as data. But it ignores it. I have the Connection set up as: OleDbConnection csvConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvPath + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"");
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7874
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7940
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6228
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3609
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
907
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.