FileOutputStream 和 FileInputStream

FileOutputStream and FileInputStream
Files are common stream destinations and sources. The concrete FileOutputStream class lets you
write a stream of bytes to a file; the concrete FileInputStream class lets you read a stream of bytes

from a file.

FileOutputStream subclasses OutputStream and declares five constructors for creating file output
streams. For example, FileOutputStream(String name) creates a file output stream to the existing
file identified by name. This constructor throws FileNotFoundException when the file doesn’t exist
and cannot be created, it is a directory rather than a normal file, or there is some other reason why
the file cannot be opened for output.
The following example uses FileOutputStream(String pathname) to create a file output stream with
employee.dat as its destination:
FileOutputStream fos = new FileOutputStream("employee.dat");
Tip FileOutputStream(String name) overwrites an existing file. To append data instead of
overwriting existing content, call a FileOutputStream constructor that includes a boolean append
parameter and pass true to this parameter.
FileInputStream subclasses InputStream and declares three constructors for creating file input
streams. For example, FileInputStream(String name) creates a file input stream from the existing
file identified by name. This constructor throws FileNotFoundException when the file doesn’t exist,
it is a directory rather than a normal file, or there is some other reason for why the file cannot be
opened for input.
The following example uses FileInputStream(String name) to create a file input stream with
employee.dat as its source:
FileInputStream fis = new FileInputStream("employee.dat");


FileOutputStream and FileInputStream are useful in a file-copying context. Listing 11-11 presents
the source code to a Copy application that provides a demonstration.

FileOutputStream 和 FileInputStream在文件拷贝的上下文中有用。
Listing 11-11. Copying a Source File to a Destination File
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy
{
public static void main(String[] args)
{
if (args.length != 2)
{
System.err.println("usage: java Copy srcfile dstfile");
return;
}
FileInputStream fis = null;
FileOutputStream fos = null;
575 CHAPTER 11: Performing Classic I/O
try
{
fis = new FileInputStream(args[0]);
fos = new FileOutputStream(args[1]);
int b; // I chose b instead of byte because byte is a reserved word.
while ((b = fis.read()) != -1)
fos.write(b);
}
catch (FileNotFoundException fnfe)
{
System.err.println(args[0] + " could not be opened for input, or " +
args[1] + " could not be created for output");
}
catch (IOException ioe)
{
System.err.println("I/O error: " + ioe.getMessage());
}
finally
{
if (fis != null)
try
{
fis.close();
}
catch (IOException ioe)
{
assert false; // shouldn't happen in this context
}
if (fos != null)
try
{
fos.close();
}
catch (IOException ioe)
{
assert false; // shouldn't happen in this context
}
}
}
}
Listing 11-11’s main() method first verifies that two command-line arguments, identifying the names
of source and destination files, are specified. It then proceeds to instantiate FileInputStream and
FileOutputStream and enter a while loop that repeatedly reads bytes from the file input stream and
writes them to the file output stream.
Of course something might go wrong. Perhaps the source file doesn’t exist, or perhaps the
destination file cannot be created (a same-named read-only file might exist, for example). In either
scenario, FileNotFoundException is thrown and must be handled. Another possibility is that an I/O
error occurred during the copy operation. Such an error results in IOException.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值