//FileOutputStream FileInputStream类(实现文件的复制) //方法一 package work; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class dhasdh { public static void main(String[] args) { FileInputStream fs = null; FileOutputStream fx = null; try { fs = new FileInputStream("H:\\Text.txt"); fx = new FileOutputStream("H:\\red.txt"); int num = 0;//定义一个变量 byte[] b = new byte[1024];//定义一个字节数组 while ((num = fs.read(b)) != -1) {//fs.read(b):读取数据并放入字节数组b中, fx.write(b);//向fx中写入数据 System.out.println("复制成功"); } } catch (IOException e) { e.printStackTrace(); } } }
//方法二
package work; import java.io.*; public class filebety { public static void main(String[] args) throws IOException { FileInputStream fs = new FileInputStream("H:\\Text.txt");//创建一个FileInputStream对象,路径"H:\\Text.txt" FileOutputStream fx = new FileOutputStream("H:\\red.txt");//创建一个FileInputStream对象,路径"H:\\red.txt" int num =0;//定义一个变量 byte[] b = new byte[1024];//定义一个字节数组 while ((num = fs.read(b))!=-1){//fs.read(b):读取数据并放入字节数组b中, fx.write(b);//向fx中写入数据 System.out.println("复制成功"); } fs.close(); fx.close(); } }