在Java中使用 FileStreams 复制文件
复制文件的主要逻辑是读取与FileInputStream变量关联的文件,并将读取的内容写入与FileOutputStream变量关联的文件中。我们可以使用Java中的 FileInputStream 和 FileOutputStream 类将文件从一个位置复制到另一个位置。现在,在继续前进之前,让我们讨论将在程序中使用的基本方法。
方法一:read():读取一个字节的数据。存在于 FileInputStream 中。
返回类型:一个整数值
语法:其他 版本
int read(byte[] bytearray
or
int read(byte[] bytearray, int offset, int length)
方法二: write(int b) :写入一个字节的数据。存在于 FileOutputStream 中
句法:
void write(byte[] bytearray)
or
void write(byte[] bytearray, int offset, int length)
实现:我们将创建两个名为“demo.rtf”和“outputdemo.rtf”的文件作为另一个没有内容的文件。下面是作为示例输入图像的“demo.rtf”文件的图像。
- 首先,我们将创建File 类的两个对象,一个引用 FileInputClass,另一个引用 FileOutputStream Class。
- 现在我们将在创建变量并将 null 分配给相应的数据类型之前创建 FileInputStream 类和 FileOutputStream 类的对象。
- 传递 FileInputStream 和 FileOutputStream 对象各自的对象
- 现在使用循环继续读取文件并使用 FileOuputStream 使用 read() 和 write() 方法将其写入另一个文件。
Tip: It is good practice to close the streams to avoid memory leakage.
示例 1:
Java
// Java Program to Illustrate File InputStream and File
// Importing required classes
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of File class
// Passing files from directory of local machine
File file = new File(
"/Users/mayanksolanki/Desktop/demo.rtf");
File oFile = new File(
"/Users/mayanksolanki/Desktop/outputdemo.rtf");
// Now creating object of FileInputStream
// Here they are variables
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// Now we make them as objects of both classes
// and passed reference of file in directory
fis = new FileInputStream(file);
fos = new FileOutputStream(oFile);
}
// Catch block to handle exceptions
catch (FileNotFoundException e) {
// Display message if exception occurs
// File Not Found or Path is Incorrect
System.out.println(e.printStackTrace());
}
try {
// Now let us check how many bytes are available
// inside content of file
fis.available();
}
catch (Exception e) {
e.printStackTrace();
}
// Using while loop to
// write over outputdemo file
int i = 0;
while (i = fis.read() != -1) {
fos.write(i);
}
// It will execute no matter what
// to close connections which is
// always good practice
finally
{
// Closing the file connections
// For input stream
if (fis != null😉 {
fis.clsoe();
}
// For output stream
if (fos != null) {
fos.close();
}
}
}
}
Java
// Java Program Illustrating Copying a src File
// to Destination
// Importing required classes
import java.io.*;
// Main class
// src2dest
class GFG {
// Main driver method
public static void main(String args[])
throws FileNotFoundException, IOException
{
// If file doesnot exist FileInputStream throws
// FileNotFoundException and read() write() throws
// IOException if I/O error occurs
FileInputStream fis = new FileInputStream(args[0]);
// Assuming that the file exists and
// need not to be checked
FileOutputStream fos
= new FileOutputStream(args[1]);
int b;
while ((b = fis.read()) != -1)
fos.write(b);
// read() method will read only next int so we used
// while loop here in order to read upto end of file
// and keep writing the read int into dest file
fis.close();
fos.close();
}
}
输出:相同的内容将反映在“outputdemo.rtf”文件中,如下面的“demo.rtf”文件所示。
示例 2:
Java
// Java Program Illustrating Copying a src File
// to Destination
// Importing required classes
import java.io.*;
// Main class
// src2dest
class GFG {
// Main driver method
public static void main(String args[])
throws FileNotFoundException, IOException
{
// If file doesnot exist FileInputStream throws
// FileNotFoundException and read() write() throws
// IOException if I/O error occurs
FileInputStream fis = new FileInputStream(args[0]);
// Assuming that the file exists and
// need not to be checked
FileOutputStream fos
= new FileOutputStream(args[1]);
int b;
while ((b = fis.read()) != -1)
fos.write(b);
// read() method will read only next int so we used
// while loop here in order to read upto end of file
// and keep writing the read int into dest file
fis.close();
fos.close();
}
}
输出:
输出说明: src 文件和 dest 文件的名称必须使用命令行参数提供,其中 args[0] 是源文件的名称,args[1] 是目标文件的名称。