Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i open and read a txt-file with BufferedReader??

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi, this is frag and i am a neewbie!

I am just fighting with my JBuilder...

I want to open a text-file with usernames and passwords.
I want to store the username aswell as the password to a string and save it into a vector.
It's one line for a name and one for the password, eg.:

user1
pwd1
user2
pwd2
user3
pwd3
...

Again: How do i open and read the file (until EndOfFile)?
Here is my code so far:

BufferedReader in = new BufferedReader(new FileReader file_path));

while (endOfFile == false)
{
username = in.readLine();
password = in.readLine();
rights = in.readLine();
usrec = new user_rec(username, password, rights);
userpwd.addElement(usrec);
}


PLEASE HELP ME!

thanx!

cya

frag
 
frag,

class user_rec{
public user_rec( BufferedReader in) throws IOException{
if ( null != (name = in.readLine()))
pass = in.readLine();
}
public boolean isValid(){
return null != name && null != pass;
}
public String name = "undefined";
public String pass = "undefined";
}

public void run(){
Vector userpwd = new Vector();
BufferedReader in = null;

try{
in = new BufferedReader(new FileReader ("user.txt"));
user_rec oRec = null;
do{
oRec = new user_rec(in);
if (oRec.isValid())
userpwd.addElement(oRec);

}while(oRec.isValid());
}catch(Exception e){
System.out.println( e);
}finally{
try{
in.close();
}catch(Exception ce){}
} // finally
System.out.println( userpwd.size() + " elements");
syswait(); // static function calls System.in.read()
}

Good luck
-pete
 
pete,

thank you for your help!

i am afraid that i haven't had the time yet to implement your code but it sounds really good. i have to do some nasty VBA-stuff before i can come back to java. :(

thanx.

frag
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top