Reading data from a file

import java.io.*;

public class NewFile {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//Properties prop = new Properties();

File file = new File("properties.txt");

String username = null;

String user = null;

try {

FileReader fi = new FileReader(file);

BufferedReader bf = new BufferedReader(fi);

//StringBufferInputStream sb = new StringBufferInputStream("properties.txt")

username = bf.readLine();

String password = bf.readLine();

System.out.println(username);

System.out.println(password);

bf.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Output

dnbconn condnbadmin

null

But I want putput as

dnbconn

condnbadmin

[1023 byte] By [Ravi_Singh83a] at [2007-11-27 6:57:22]
# 1

Check the contents of the "properties.txt" file. Also check the API for BufferedReader's readLine() method. You have to find out why this method is assigning the value it does to username.

If you can't figure this out, repost. But, please, use the "code" tags described here: http://forum.java.sun.com/help.jspa?sec=formatting Also consider deleting the commented out lines as these are just noise and will distract you from seeing a solution. A do post the contents of the properties.txt file as it is critical in determining the output of your program.

pbrockway2a at 2007-7-12 18:34:38 > top of Java-index,Java Essentials,Java Programming...
# 2

> import java.io.*;

> public class NewFile {

>

> /**

> * @param args

> */

> public static void main(String[] args) {

> // TODO Auto-generated method stub

> //Properties prop = new Properties();

>File file = new File("properties.txt");

>String username = null;

>String user = null;

>try {

> FileReader fi = new FileReader(file);

> BufferedReader bf = new BufferedReader(fi);

> //StringBufferInputStream sb = new

> StringBufferInputStream("properties.txt")

> username = bf.readLine();

> String password = bf.readLine();

> System.out.println(username);

> System.out.println(password);

> bf.close();

> } catch (FileNotFoundException e) {

> // TODO Auto-generated catch block

> e.printStackTrace();

> } catch (IOException e) {

> // TODO Auto-generated catch block

> e.printStackTrace();

> }

>

> }

>

> }

>

> Output

> dnbconn condnbadmin

> null

>

> But I want putput as

> dnbconn

> condnbadmin

Hi

readline () method of BufferedReaderclass actually helps to read content of a file untill a newline character is found .

My properties.txt file is very simple currently it is having only two values.

dnbconn condndadmin

Thats it .

My requirement is that when i read through these two values and then finally store this value one value should be stored in username(i.e dnbconn should get stored in username) and and the next value should get stored in password( i.e condnbconnadmin should get stored in password).

But since i am using readline() method which reads the whole line I both dnbconn and condnbadmin gets stored in username and consequenlty password get value null.

Finally what I want !!!

So is there any way to the above stuff work properly either by modifying readline method or any other method which can solve the issue .

So that the required get stored in both the string variables.

Ravi_Singh83a at 2007-7-12 18:34:38 > top of Java-index,Java Essentials,Java Programming...
# 3

> My requirement is that when i read through these

> two values and then finally store this value one

> value should be stored in username(i.e dnbconn

> should get stored in username) and and the next

> value should get stored in password( i.e

> condnbconnadmin should get stored in

> password).

Use String's split() method to divide the string you read into two pieces based on the white space.

pbrockway2a at 2007-7-12 18:34:38 > top of Java-index,Java Essentials,Java Programming...