• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading from a file

 
Vijayanand Jayaraj
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have data in a text file as the below format,
name string 20
city string 20
address string 20
age int
DOB date
id int

I want to read the information from this file as
name
city
address
age
DOB
id
I mean I want only the first column... How to get this information from the text file. Any help would be appreciated.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to read and write files? A good place to start is the IO Chapter of the Java Tutorial. Once you figure out how to read and write you can move on to parsing your input.
 
Lukasz Bajzel
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of reading lines from a file

import java.io.*;

BufferedReader fileHandle = null;
try{
//Use buffering to speed up the process
fileHandle = new BufferedReader(new FileReader("file_path"));
/*
* Note: Make sure to use System.getProperty("file.separator")
* to seperate directories in any path being passed.
*/
String line;
//When file is available and ready to read.
if(fileHandle != null && fileHandle.ready() ){

//read one line at a time.
while((line = fileHandle.readLine()) != null){
//work with the line
System.out.println(line);
}
}
}catch(Throwable t){
t.printStackTrace();
}
finally{
try{
if (fileHandle != null) {
fileHandle.close();//close the reader.
}
}catch (IOException ie) {
ie.printStackTrace();
}

}


Hope this helps!

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The place where your questions are answered directly.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ January 18, 2008: Message edited by: Joe Ess ]
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunny, though we all love to help people around here, It is important that you just don't give code away. There's only one way to learn how to program, and that's to write code. Not to mention that many people (not necessarily Vijayanand ) post their homework assignments.
Have a look at our FAQ, How To Answer Questions On JavaRanch, in particular Let Them Do Their Own Homework
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i apologize, will take care in future..!!
thanks a lot to remind me..!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!