1. Gunakan FileInputStream dan FileOutputStream untuk menyalin fail
public void fileCapy(String src, String dest) { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(new File(src)); fos = new FileOutputStream(new File(dest)); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) != -1) { fos.write(bytes, 0, length); } } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2 Strim bait)
public void textCapy(String src, String dest) { FileReader fr = null; FileWriter fw = null; try { fr = new FileReader(new File(src)); fw = new FileWriter(new File(dest)); char[] chars = new char[1024]; int length; while ((length = fr.read(chars)) != -1) { fw.write(chars, 0, length); } } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } if (fr != null) { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Atas ialah kandungan terperinci Analisis contoh kaedah menggunakan aliran IO untuk menyalin fail dalam Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!