JAVA程序-实现本机上文件的复制粘贴(读取写入)

    xiaoxiao2023-09-30  162

    采用字节流的方式对文件进行读取写入,首先将文件内容读入字节数组buffer,再将buffer里的内容写入文件,从而实现文件的复制粘贴。

    public class OpenFile { public static void main(String[] args) throws IOException { try{ FileInputStream rf = new FileInputStream("D:\\myjava\\test.txt");//绝对路径,注意这里是转义字符\\ int n=512,c=0; byte buffer[] = new byte[n]; FileOutputStream wf = new FileOutputStream("D:\\myjava\\test1.txt");//绝对路径,注意这里是转义字符\\ while((c=rf.read(buffer,0,n))!= -1){ wf.write(buffer,0,n); } rf.close(); wf.close(); System.out.println("Save to test1.txt!"); }catch(IOException e){ System.out.println(e); } } }

    最新回复(0)