java.io 文件流操作读取、写入、复制文件 FileInputStream FileOutputStream

    技术2022-07-12  69

    public class Test { public static void main(String[] args) throws IOException { Test test = new Test(); test.outStream(); test.inStream(); test.copyFile(); } public void copyFile() throws IOException { FileInputStream fi = new FileInputStream("./demo.txt"); FileOutputStream fo = new FileOutputStream("./demo2.txt"); byte[] bytes = new byte[1024 * 8]; int len; while ((len = fi.read(bytes)) != -1) { fo.write(bytes, 0, len); } fo.close(); fi.close(); } public void inStream() throws IOException { FileInputStream fi = new FileInputStream("./demo.txt"); byte[] bytes = new byte[1024 * 8]; int len = fi.read(bytes); String string = new String(bytes, 0, len, "UTF-8"); System.out.println(string); fi.close(); } public void outStream() throws IOException { FileOutputStream fo = new FileOutputStream("./demo.txt"); fo.write("又蒙圈了".getBytes("UTF-8")); fo.close(); System.out.println("写入完成"); } }

     

    Processed: 0.015, SQL: 9