File I/O handling

FileInputStream fle =new FileInputStream("test.txt");

java.io.BufferedReader inFile =new java.io.BufferedReader(new java.io.InputStreamReader(fle));

String cm ="";

p.load(fle);//p - Properties class object

while((cm = inFile.readLine()) !=null){

System.out.println(p.getProperty("key"));

}

fle.close();

In the above code i am using Properties to display property name "key". But i dont know why its not getting inside loop itself. Please suggest me.

[834 byte] By [ArpanaKa] at [2007-11-27 11:23:37]
# 1

Probably because readLine() returns null. Or you get an exception that you ignore.

CeciNEstPasUnProgrammeura at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 2

> Probably because readLine() returns null. Or you get

> an exception that you ignore.

No, file is not empty . It has data. That code is inside try/catch block and in catch block i am printing stack trace. Nothing shows error.

ArpanaKa at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 3

Try

while (true) {

String line = in.readLine();

if (line == null) {

break;

}

// ....

}

Maybe with a system.out.println() or debugger breakpoint for verification. You can change it back later.

CeciNEstPasUnProgrammeura at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 4

If i ignore "p.load(fle)" line, then it works. But i really need that line. Any help

ArpanaKa at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 5

> Try

>

> [code]while (true) {

>String line = in.readLine();

> if (line == null) {

>break;

>

>// ....

> /code]

>

> Maybe with a system.out.println() or debugger

> breakpoint for verification. You can change it back

> later.

I tried it. Indeed its returning null. But where as file is not empty. How to solve the problem

ArpanaKa at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 6

Reopen the file. You're trying to read 1 file 2 times. The BufferedReader will never see the data in the file, since the p.load() empties the fileinputstream.

-Kayaman-a at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 7

public void printDetails() throws IOException {

FileInputStream fin = new FileInputStream(propertyFileName);

properties.load(fin);

System.out.println(properties.get("permission"));

Enumeration enum = properties.propertyNames();

String key = "";

while(enum.hasMoreElements()) {

key = (String)enum.nextElement();

System.out.println(" Key : " + key + " Value : " + properties.get(key));

}

}

This may be helpful for you.

Santhiyaa at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 8

> Reopen the file. You're trying to read 1 file 2

> times. The BufferedReader will never see the data in

> the file, since the p.load() empties the

> fileinputstream.

Please tell me when to reopen the file. It is after p.load() method or inside while loop

ArpanaKa at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 9

Actually what i am trying to do is,

1. Load the inputstream in properties and later retrieve property value

2. Read the file line by line and check for a matching property name

Hope I am clear ,

ArpanaKa at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 10

Thanks Joni, now I see it as well. You "use up" your file stream when loading the properties. trying to read anything afterwards will not result in anything.

Don't use the same stream to read the file and to load the properties.

CeciNEstPasUnProgrammeura at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 11

> > public void printDetails() throws IOException {

>

> FileInputStream fin = new

> ew FileInputStream(propertyFileName);

> properties.load(fin);

>

> System.out.println(properties.get("permission"));

> Enumeration enum = properties.propertyNames();

> String key = "";

> while(enum.hasMoreElements()) {

> key = (String)enum.nextElement();

> System.out.println(" Key : " + key + " Value : " +

> " + properties.get(key));

> }

>

> }

>

>

> This may be helpful for you.

Thanks Santhiya. Your code helped me . Bad me. I was trying to read a single file 2 times just for match checking. Above code worked out. thanks

Thanks to others who helped me by pointing towards my mistake

ArpanaKa at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...
# 12

What was the purpose of the exercise? to test Properties.load()? Sun having been doing that every build for ten years, you don't need to do that yourself.

ejpa at 2007-7-29 15:01:44 > top of Java-index,Java Essentials,Java Programming...