java io读写文件相关阅读:
public class DemoIO { public static byte[] readForInputStream(File file) throws IOException{ InputStream in = new FileInputStream(file); byte b[]=new byte[(int)file.length()]; //创建合适文件大小的数组 int len = 0; int temp=0; //所有读取的内容都使用temp接收 while((temp=in.read())!=-1){ //当没有读取完时,继续读取 b[len]=(byte)temp; len++; } in.close(); return b; } public static void writeForOutputStream(File file) throws IOException { String content = "这是一段感人肺腑的文字"; FileOutputStream fop = new FileOutputStream(file,true);//第二个参数ture:表示在内容中追加 // 判断文件是否存在 if (!file.exists()) { file.createNewFile(); } byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); System.out.println("--------write success-------------"); } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { writeForOutputStream(new File("e:/testio.txt")); byte[] b = readForInputStream(new File("e:/testio.txt")); System.out.println(new String(b, 0, b.length)); }}