473,748 Members | 2,219 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 11748
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
3512
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 me....but if the file size is unknown then how can i read in equal chunks so that at the last call i...
5
17700
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 mess. Lots of whitespace after and "\r" and "\n" after each element. The problem is that I cannot...
2
3110
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
2229
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
2228
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 sys
2
5197
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
2793
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 intermediate steps. And being just introduced to JAVA, my understanding of processing large plain text...
4
1932
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
5028
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
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9552
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.