java 文件io流输入、输出、复制

tech2023-09-08  89

文件输入

package com.mtlk.wd; import java.io.FileInputStream; import java.io.IOException; public class InputDemo { public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("E:/1.txt"); byte[] context = new byte[fis.available()]; fis.read(context); String s = new String(context); System.out.println(s); } }

文件输出

package com.mtlk.wd; import java.io.FileOutputStream; import java.io.IOException; public class OutputDemo { public static void main(String[] args) throws IOException{ String context = "你好啊"; FileOutputStream fos = new FileOutputStream("E:/2.txt"); fos.write(context.getBytes()); fos.flush(); fos.close(); } }

文件复制

package com.mtlk.wd; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyDemo { public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("E:/1.txt"); byte[] context = new byte[fis.available()]; fis.read(context); FileOutputStream fos = new FileOutputStream("E:/3.txt"); fos.write(context); fos.flush(); fos.close(); } }
最新回复(0)