import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class Demo { public static void main(String[] args) throws Exception { File file = new File("D://测试文件.txt"); InputStream in = new FileInputStream(file); int fileLength = (int) file.length(); byte b[] = new byte[fileLength]; in.read(b); in.close(); System.out.println("读取的内容是:" + new String(b)); } }
import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class Demo { public static void main(String[] args) throws Exception { File file = new File("D://测试文件.txt"); OutputStream out=new FileOutputStream(file); String str="Java你好!"; byte b[]=str.getBytes(); out.write(b); out.close(); } }