wood burning stoves*
The moose likes Java in General and the fly likes need help to read from a file and print it on console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "need help to read from a file and print it on console" Watch "need help to read from a file and print it on console" New topic
Author

need help to read from a file and print it on console

Naga Niranjan
Greenhorn

Joined: Aug 02, 2005
Posts: 25
Hi all,
I need help to read from a file and print it on console. Plz help me.
Bye
Steve McCann
Ranch Hand

Joined: Oct 20, 2004
Posts: 81
The Java Tutorial covers file IO in an easy to understand way.
Naga Niranjan
Greenhorn

Joined: Aug 02, 2005
Posts: 25
Thank you Steve.

My problem solved with your advise by using RandomAccessFile. But iam facing some problem at the end of file while reading. It is showing NullPointerException. How can i overcome it.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It depends on what is causing the NullPointerException. We will be able to more easily able to help you if you post your code and the full error message.

Layne


Java API Documentation
The Java Tutorial
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name
here.
Thanks!


[Jess in Action][AskingGoodQuestions]
Naga Niranjan
Greenhorn

Joined: Aug 02, 2005
Posts: 25
Originally posted by Layne Lund:
It depends on what is causing the NullPointerException. We will be able to more easily able to help you if you post your code and the full error message.

Layne


The following is my code

import java.io.*;

class Commands
{
public static void main(String[] args)
{
try{
RandomAccessFile raf=new RandomAccessFile("Woc.txt","r");
long len=raf.length();
for(long clen=0;clen<len
{
String s=raf.readLine();
System.out.println(s);
clen+=s.length();
}
raf.close();
}
catch(IOException e)
{
}



}
}

this is the error:
Exception in thread "main" java.lang.NullPointerException
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You're not counting the newlines, so the number of bytes read will actually be larger than "clen". The idiomatic way to read all the lines in a file in Java looks like this:

Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
please use code tags.
Naga Niranjan
Greenhorn

Joined: Aug 02, 2005
Posts: 25
Originally posted by Matt Fielder:
please use code tags.


What are code tags? I didn't understand what you are saying?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help to read from a file and print it on console