java code for reading text
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 he开发者_C百科lp me out.
ok not a homework question i am not looking for the solution just point me to the correct reference material.
This sounds like homework, do the following and ask about specific problems.
- Open the file using FileReader, and wrap that in a BufferedReader
- Call ReadLine on the BufferedReader, set the return value to your string.
- if any of the above throws an exception, catch it and set your string to ""
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()
.
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
精彩评论