public static void main(String[] args) { //test1(); test2();//java7提供的自动资源关闭 }
public static void test2() { File oldFile = new File("F:/firefox download/SpringDataJPA从入门到精通_张振华(著)清华大学出版社2018-05.pdf"); File newFile = new File("F:/firefox download/sdj.pdf"); try ( InputStream is = new FileInputStream(oldFile); OutputStream os = new FileOutputStream(newFile); ){ byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1){ os.write(buffer, 0, len); } } catch (Exception e){ e.printStackTrace(); } }
注意语法格式!!!try后紧跟一对圆括号!!!