• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do this Input Byte Streams program catch IOException?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

did they catch IOException in this program ? I don't think so they just throws IOException(Checked exception) am I correct ?

What should I use in finally? 1. try catch or 2.if( in !=null) condition?

OR

If try catch then should I write separate try catch for each closing of objects ? or one in common?

Rather importing this

If I import directly will this import slow down performance of my code. Though that could be in nanoseconds or byte memory usage difference ?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you run the code? What happened when you run the code, any exception comes?

finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}


This is better as otherwise NPE could come.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

If I import directly
import java.io.*;


Import specific class which requires not all
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it runs well. It is a program from JLS Byte Streams This is the link
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NPE ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:What should I use in finally? 1. try catch or 2.if( in !=null) condition?


Don't use either. Use try-with-resources instead:


If you want to copy a file, you should probably use java.nio.file.Files.copy(Path, Path, CopyOption...) instead.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:NPE ?


NullPointerException
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Stephan yes in my program I used try with resource while doing program and wrote try and catch( IOExeption) in that try and closed those objects by writing finally in that same try block as those FileInputOutput object is local to try. In JLS they mentioned
Since xanadu.txt contains character data, the best approach is to use character streams,
but as first program of I/O they recommended to use byte streams as stream types are built on byte streams. Here is the source link
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh NPE got it
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
though NPE occurs i throw that IOException and try, catch, finally in same try block(try with resource). Thank you Tushar
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:though NPE occurs i throw that IOException and try, catch, finally in same try block(try with resource). Thank you Tushar
No, that is quite incorrect. If there is a null exception you will not suffer an IOException.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerException will occur if specified file is not available, hope I'm correct?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You will suffer a file not found exception.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ops ! when an object of a class has NULL value in it and if I try to use that object to access its method then it will give NullPointerException.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to read and practice more and must make list of all concepts so I can understand and differentiate them .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!