473,765 Members | 1,958 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 11752
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
3112
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
5200
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
2795
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
1934
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
5029
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
9398
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
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
8831
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
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
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
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
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.