0

i need a sample code for this

if text file is not empty then string s = first line of text of the text file.

please help me out.

ok not a homework question i am not looking for the solution just point me to the correct reference material.

3
  • Is this a homework assignment? Commented Nov 24, 2009 at 19:05
  • @rover12 - I'll take your "this is not homework" assertion at face value. But based on your other questions, it looks like you are trying to learn how to program in Java by asking questions on SO. I suggest that you go to your local bookshop (or Amazon.com), leaf through the "Learn to program in Java" texts, and buy one. Or do one of the online Java tutorials. Commented Nov 25, 2009 at 0:09
  • @rover12 - also when you DO have questions to ask (that you couldn't find answers to in your textbook / tutorial) ... take the time to write your questions clearly, in detail, with decent formatting, and proper grammar, spelling and PUNCTUATION. If you want people to spend time answering, you should spend time asking questions properly. Commented Nov 25, 2009 at 0:15

3 Answers 3

4

This sounds like homework, do the following and ask about specific problems.

  1. Open the file using FileReader, and wrap that in a BufferedReader
  2. Call ReadLine on the BufferedReader, set the return value to your string.
  3. if any of the above throws an exception, catch it and set your string to ""
Sign up to request clarification or add additional context in comments.

Comments

0

Some hints (because this smells homework and I'm not going to do it for you): decorate a FileReader with a BufferedReader and check BufferedReader#readLine().

Comments

0

The sun java tutorials have examples and explanations for tasks like this.

The IO Tutorial has a section in the File I/O section called Reading, Writing and Creating Files which looks like what you are looking for

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.