import java.io.*;
public class CopyFile {
public static void main(String[] args) throws IOException{
//读取文件路径
String ReadFilePath = "./1.txt";
//写入文件路径
String WriteFilePath = "./2.txt";
//读取文件的内存大小
int readfileLimit = 1024;
copyfile(ReadFilePath, WriteFilePath, readfileLimit);
}
/*
* 复制文件
* */
public static void copyfile(String ReadFilePath, String WriteFilePath, int readfileLimit) throws IOException {
int n = 0;
//写入对象
OutputStream outputStream = null;
byte[] buffer = new byte[readfileLimit];
try(InputStream inputStream = new FileInputStream(ReadFilePath)){
outputStream = new FileOutputStream(WriteFilePath);
while (true){
n = inputStream.read();
if(n == -1){
break;
outputStream.write(n);
}catch (IOException e){
System.out.println("复制失败!");
e.printStackTrace();
}finally {
if(outputStream != null) {
outputStream.close();
Sign in to make a reply
iruto-omotteta
import java.io.*;
public class CopyFile {
public static void main(String[] args) throws IOException{
//读取文件路径
String ReadFilePath = "./1.txt";
//写入文件路径
String WriteFilePath = "./2.txt";
//读取文件的内存大小
int readfileLimit = 1024;
copyfile(ReadFilePath, WriteFilePath, readfileLimit);
}
/*
* 复制文件
* */
public static void copyfile(String ReadFilePath, String WriteFilePath, int readfileLimit) throws IOException {
int n = 0;
//写入对象
OutputStream outputStream = null;
byte[] buffer = new byte[readfileLimit];
try(InputStream inputStream = new FileInputStream(ReadFilePath)){
outputStream = new FileOutputStream(WriteFilePath);
while (true){
n = inputStream.read();
if(n == -1){
break;
}
outputStream.write(n);
}
}catch (IOException e){
System.out.println("复制失败!");
e.printStackTrace();
}finally {
if(outputStream != null) {
outputStream.close();
}
}
}
}