Java Reference
In-Depth Information
FileInputStream subclasses InputStream anddeclaresthreeconstructorsfor
creatingfileinputstreams.Forexample, FileInputStream(String name) cre-
ates a file input stream from the existing file identified by name . This constructor
throws FileNotFoundException whenthefiledoesn'texist,itisadirectoryrather
thananormalfile,orthereissomeotherreasonforwhythefilecannotbeopenedfor
input.
Thefollowingexampleuses FileInputStream(String name) tocreateafile
input stream with employee.dat as its source:
FileInputStream fis = new FileInputStream("employee.dat");
Listing 8-8 presents the source code to a DumpFileInHex application that uses
FileOutputStream and FileInputStream tocreateafilethatcontainsahexa-
decimal representation of another file.
Listing 8-8. Creating a hexadecimal representation of a file
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class DumpFileInHex
{
final
static
String
LINE_SEPARATOR
=
Sys-
tem.getProperty("line.separator");
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("usage:
java
DumpFileInHex
pathname");
return;
}
String dest = args[0]+".hex";
try
(FileInputStream
fis
=
new
FileIn-
putStream(args[0]);
FileOutputStream
fos
=
new
FileOut-
putStream(dest))
{
 
Search WWH ::




Custom Search