File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Naga Niranjan
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I need help to read from a file and print it on console. Plz help me.
Bye
 
Steve McCann
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Tutorial covers file IO in an easy to understand way.
 
Naga Niranjan
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Naga Niranjan
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please use code tags.
 
Naga Niranjan
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic