将一个文件的数据复制到另一个文件 15 次的程序
Program to Copy data of one file into another file for 15 times
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class JavaCopyFile {
public static void main(String[] args) throws InterruptedException, IOException {
int i=0,count=0;;
while(i<15) {
File source = new File("error.txt");
File dest = new File("criteria.txt");
// copy file conventional way using Stream
//long start = System.nanoTime();
copyFileUsingStream(source, dest);
//System.out.println("Time taken by Stream Copy = " + (System.nanoTime() - start));
if(i<15) {
count++;
}
i++;
}
System.out.println(count);
}
private static void copyFileUsingStream(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
}catch(Exception e) {
System.out.println("File not found exception");/*finally {
input.close();
output.close();
*/
}
}
}
我已经编写了上面的代码 - 出于测试目的,我设置了计数变量。 count
给15就完美了。但是文件只复制一次。我想将文件复制 15 次到同一个目标文件。请帮我解决这个问题。我是 java 编程的初学者。
如评论中所述,您的逻辑是正确的,但您每次都在重写目标文件。
使用变量 i
表示您写入新目标文件的每个时间,如下所示:
File dest = new File("criteria" + i + ".txt");
这将在目标中创建 15 个文件,从而避免重写相同的目标。
编辑:
根据您的最新评论,将代码更改为如下:
public static void main(String[] args) throws InterruptedException, IOException {
int i = 0, count = 0;
File source = new File("error.txt");
File dest = new File("criteria.txt");
OutputStream output = new FileOutputStream(dest);
while (i < 15) {
copyFileUsingStream(source, output);
if (i < 15) {
count++;
}
i++;
}
System.out.println(count);
}
private static void copyFileUsingStream(File source, OutputStream output)
throws IOException {
InputStream input = null;
try {
input = new FileInputStream(source);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} catch (Exception e) {
System.out.println("File not found exception");
}
}
不是在循环的每次迭代中都创建 OutputStream
,而是创建一次并每次都将其传递给 copyFileUsingStream
,以便保持之前的流。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class JavaCopyFile {
public static void main(String[] args) throws InterruptedException, IOException {
int i=0,count=0;;
while(i<15) {
File source = new File("error.txt");
File dest = new File("criteria.txt");
// copy file conventional way using Stream
//long start = System.nanoTime();
copyFileUsingStream(source, dest);
//System.out.println("Time taken by Stream Copy = " + (System.nanoTime() - start));
if(i<15) {
count++;
}
i++;
}
System.out.println(count);
}
private static void copyFileUsingStream(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
}catch(Exception e) {
System.out.println("File not found exception");/*finally {
input.close();
output.close();
*/
}
}
}
我已经编写了上面的代码 - 出于测试目的,我设置了计数变量。 count
给15就完美了。但是文件只复制一次。我想将文件复制 15 次到同一个目标文件。请帮我解决这个问题。我是 java 编程的初学者。
如评论中所述,您的逻辑是正确的,但您每次都在重写目标文件。
使用变量 i
表示您写入新目标文件的每个时间,如下所示:
File dest = new File("criteria" + i + ".txt");
这将在目标中创建 15 个文件,从而避免重写相同的目标。
编辑:
根据您的最新评论,将代码更改为如下:
public static void main(String[] args) throws InterruptedException, IOException {
int i = 0, count = 0;
File source = new File("error.txt");
File dest = new File("criteria.txt");
OutputStream output = new FileOutputStream(dest);
while (i < 15) {
copyFileUsingStream(source, output);
if (i < 15) {
count++;
}
i++;
}
System.out.println(count);
}
private static void copyFileUsingStream(File source, OutputStream output)
throws IOException {
InputStream input = null;
try {
input = new FileInputStream(source);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} catch (Exception e) {
System.out.println("File not found exception");
}
}
不是在循环的每次迭代中都创建 OutputStream
,而是创建一次并每次都将其传递给 copyFileUsingStream
,以便保持之前的流。