• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read a file line by line

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done this on linux...it's just printing first line....Why its not printing whole file....
Please help...
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Abdul Ahadone
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic