copyfile.java

来自「java教程上的一段源码」· Java 代码 · 共 30 行

JAVA
30
字号
//源文件名:copyFile.java
//在下载源程序中的文件夹:1006复制文件
import java.io.*;
public class copyFile
{
    public static void main(String args[]) throws IOException
    {
        File f1,f2;
        f1 = new File(".","sourceFile.txt");
        f2 = new File(".\\subDir","targetFile.txt");
        copy(f1,f2);
        System.exit(0);
    }
    public static void copy(File f1,File f2) throws IOException
    {
        FileInputStream rf = new FileInputStream(f1);
        FileOutputStream wf = new FileOutputStream(f2);
        int count,n=512;
        byte buffer[] = new byte[n];
        count = rf.read(buffer,0,n);
        while (count != -1)
        {
            wf.write(buffer,0,count);
            count = rf.read(buffer,0,n);
        }
        System.out.println("已经把文件“"+f1.getPath()+"”复制为“"+f2.getPath()+"”");
        rf.close();
        wf.close();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?