aspose file tools*
The moose likes I/O and Streams and the fly likes Read a file line by line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Read a file line by line" Watch "Read a file line by line" New topic
Author

Read a file line by line

Abdul Ahadone
Greenhorn

Joined: Mar 21, 2012
Posts: 4
I have done this on linux...it's just printing first line....Why its not printing whole file....
Please help...
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

If you are reading a text file, then you can use BufferedReader.

Moreover the readLine method of DataInputStream is deprecated.

There's a method readLine in BufferedReader which you can use to read the contents.

In the example you have provided you are reading the content but the content has been assigned to some variable on once i.e before the loop. But in the loop there's readLine but the returned value is not assigned to any variable. So the change would be to assign the value of readLine to some variable.


Mohamed Sanaulla | My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Because you're doing it incorrectly. Most importantly, available() doesn't do what you think it does. Also, you only once assign anything to c. Finally, you have two different InputStream wrappers around ob. You really shouldn't do that. Either drop bis completely, or put it between ob and ds:
As for the reading, the technique I prefer is the following:
Now there is one more thing I'd like to improve. You're using DataInputStream for reading a text file. DataInputStream is actually meant for binary data. For text files, use Readers:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Abdul Ahadone
Greenhorn

Joined: Mar 21, 2012
Posts: 4
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.io.EOFException;;
import java.rmi.*;
import java.io.*;
import java.io.FilterInputStream.*;

public class BoyerMooreClient {
public static void main(String[] args) throws NullPointerException, FileNotFoundException, IOException, EOFException {
FileInputStream ob = new FileInputStream("/home/abdul/Desktop/a.txt");
BufferedInputStream bis = null;
DataInputStream dis = null;
System.out.println();
try {
dis = new DataInputStream(bis);
bis = new BufferedInputStream(ob);
while(dis.available()!=0){
System.out.println("File Content: "+dis.readLine());

BoyerMoore b = (BoyerMoore) Naming.lookup("rmi://localhost/BoyerMooreService");
System.out.println( b.Bo("sip") );
System.out.println( b.search(dis.readLine()) );
dis.close();
bis.close();
ob.close();
}
} catch (NullPointerException npe) {
System.out.println();
System.out.println("NullPointerException");
System.out.println(npe);
}

catch (MalformedURLException murle) {
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (ServerException murle) {
System.out.println();
System.out.println("ServerException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
}
}

i m getting this Error if i m running this:
Exception in thread "main" java.lang.NullPointerException
at java.io.FilterInputStream.available(FilterInputStream.java:159)
at BoyerMooreClient.main(BoyerMooreClient.java:23)
Anyone please help....

Regards
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

When you post code, please UseCodeTags(⇐click) so it will be readable.



So what's line 23 of BoyerMooreclient.java? Look at that line, figure out which references you're dereferencing, and then track down which of them are null.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Also, note that it's generally bad practice to catch RuntimeException, Error, or any of their descendants (such as NullPointerException). A RuntimeException indicates a bug in your code, and you generally can't (and shouldn't) recover from it. Best to let the program just die at that point.

And you should be aware that just printing out the exception is not actually proper handling. The problem occurred, and catching the exception doesn't magically fix that. If all you do is print it out, but you don't actually fix it or rethrow it or another exception, then your program has no way of knowing that anything went wrong, and it just continues along thinking eveyrthing is okay when it's not. (For simply academic exercises, just printing the stack trace can be sufficient, but for production code, if you're doing to catch and not rethrow, then you have to actually fix the problem.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Abdul Ahadone wrote:

When you create dis, bis is still null. Swap these two initialization lines. Then go ahead and read my previous post again, because you've obviously ignored everything I said in it. You're still using available() which you really, really, really shouldn't do. And you really should use FileReader and BufferedReader instead of FileInputStream, BufferedInputStream and DataInputStream as I had also previously suggested.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read a file line by line