Java I/O 总结

.read()) != -1){ System.out.print((char)b); } in.close();} catch (IOException e) { e.printStackTrace();}

方法三:(有可能出现乱码)

FileInputStream in = new FileInputStream("AtomicityTest.java");int n = 50;byte[] buffer = new byte[n];try {    while ((in.read(buffer,0,n) != -1 && n > 0)){        System.out.print(new String(buffer));    }    in.close();} catch (IOException e) {    e.printStackTrace();}
  • 将文件A的内容拷贝到文件B
FileInputStream in = new FileInputStream("AtomicityTest.java");FileOutputStream out = new FileOutputStream("copy.txt");int b;while ((b = in.read()) != -1){    out.write(b);}out.flush();in.close();out.close();