//JDK7
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo07try { public static void main(String[] args) throws IOException { try( FileInputStream fis=new FileInputStream("F:\basic\untitled13\src\it\cast\day15\demo01\1.txt"); FileOutputStream fos=new FileOutputStream("F:\basic\untitled13\src\it\cast\day15\demo01\5.txt");) { byte[] bytes1=new byte[1024]; int len=0; while ((len=fis.read(bytes1))!=-1){ //System.out.println(new String(bytes1,0,len));//读取有效的几个 fos.write(bytes1,0,len); } }catch (IOException e){ System.out.println(e); } } }
JDK9:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo07try_JDK9 { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("F:\basic\untitled13\src\it\cast\day15\demo01\1.txt"); FileOutputStream fos=new FileOutputStream("F:\basic\untitled13\src\it\cast\day15\demo01\5.txt"); try( fis;fos){ byte[] bytes1=new byte[1024]; int len=0; while ((len=fis.read(bytes1))!=-1){ fos.write(bytes1,0,len); } }catch (IOException e){ System.out.println(e); } } }