By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

How to read from a .txt and match the strings.

P: 6
I Have a file name.txt.
Below is the format of names.txt.

1 SATYA;
2 KUMAR;
3 MOHAN;
4 Venkat;

In my java code i get an integer(say 3).
With that integer(3) i should pick the corresponding name from the names.txt and display on the screen.
How do i do that?

if input is 3 the display must be MOHAN.
if input is 4 the display must be Venkat.
if input is 2 the display must be KUMAR.


Please send few lines of code for this operation.
Jan 17 '07 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
I Have a file name.txt.
Below is the format of names.txt.

1 SATYA;
2 KUMAR;
3 MOHAN;
4 Venkat;

In my java code i get an integer(say 3).
With that integer(3) i should pick the corresponding name from the names.txt and display on the screen.
How do i do that?

if input is 3 the display must be MOHAN.
if input is 4 the display must be Venkat.
if input is 2 the display must be KUMAR.


Please send few lines of code for this operation.
You write the code first and post it. Use
Expand|Select|Wrap|Line Numbers
  1. BufferedReader br = new BufferedReader(new FileReader("fileName.txt")); 
to open the file
and
Expand|Select|Wrap|Line Numbers
  1. br.readLine(); 
to read a line from the file
Jan 17 '07 #2

P: 6
You write the code first and post it. Use
Expand|Select|Wrap|Line Numbers
  1. BufferedReader br = new BufferedReader(new FileReader("fileName.txt")); 
to open the file
and
Expand|Select|Wrap|Line Numbers
  1. br.readLine(); 
to read a line from the file

I want without BufferedReader. i.e, with InputStreamReader.

Can you suggest me a way to through this.
Jan 17 '07 #3

10K+
P: 13,264
I want without BufferedReader. i.e, with InputStreamReader.

Can you suggest me a way to through this.
That is very bad practice. You should use a Scanner, BufferedReader or RandomAccessFile for this.

Why do you want to use InputStreamReader?
Jan 17 '07 #4

P: 6
That is very bad practice. You should use a Scanner, BufferedReader or RandomAccessFile for this.

Why do you want to use InputStreamReader?
We have restricted pacakge access.We use our own packages derived from java api. Hence we cant use all the classes in the Java API.
Jan 17 '07 #5

10K+
P: 13,264
We have restricted pacakge access.We use our own packages derived from java api. Hence we cant use all the classes in the Java API.
I don't understand this. You have access to InputStreamReader but not to RandomAccessFile or FileReader? What other io classes are you not allowed to use then because the InputStreamReader alone cannot read a file.


Here is an exctract of the API specs


An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specifiedcharset .The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Jan 17 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.