import java.io.*;
public class FileInputStreamDemo
{
public static void main(String[] args)
{ FileInputStream fis = null;
try{
fis = new FileInputStream("FileInputStreamDemo.java");
System.out.println("Available data: "+fis.available()+
" bytes.");
int c = fis.read();
while(c != -1)
{ System.out.print((char)c);
c = fis.read();
}
}
catch(IOException ie)
{ ie.printStackTrace(); }
finally{
try
{ if(fis != null)
fis.close();
}
catch(IOException ie)
{ ie.printStackTrace(); }
}
}
}
import java.io.*;
public class FileInputStreamDemo2
{
public static void main(String[] args)
{ // try-with-resources statement
try(FileInputStream fis =
new FileInputStream("FileInputStreamDemo2.java"))
{
System.out.println("Available data: "+fis.available()+
" bytes.");
int c;
while((c = fis.read()) != -1)
{ System.out.print((char)c);
//c = fis.read();
}
}
catch(IOException ie)
{ ie.printStackTrace(); }
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter