案例1:FOS覆盖方式写文件
FileOutputStream fos = new FileOutputStream("fos.txt");
案例2:FOS追加模式写文件
FileOutputStream fos = new FileOutputStream("fos.txt", true);
案例3:FIS读取文件数据
FileInputStream fis = new FileInputStream("fos.txt"); int d = -1; while((d=fis.read())!=-1){ System.out.print((char)d); } fis.close();
案例4:BOS演示
//创建缓冲字节输出流 FileOutputStream fos = new FileOutputStream("fos.txt"); //所有字节被存入缓冲区,等待一次性写出 BufferedOutputStream bos = new BufferedOutputStream(fos); String str = "Hello Java"; byte[] date = str.getBytes(); bos.write(date); //关闭流之前,缓冲输出流会将缓冲区内容一次性写出 bos.close(); System.out.println("写出完毕");
案例5:flush演示
FileOutputStream fos = new FileOutputStream("fos.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); String str = "Hello Java"; byte[] date = str.getBytes(); bos.write(date); bos.flush(); //强制将缓冲区已缓存数据一次性写出 System.out.println("写出完毕"); bos.close();
案例6:BIS演示
//创建缓冲字节输入流 FileInputStream fis = new FileInputStream("fos.txt"); BufferedInputStream bis = new BufferedInputStream(fis); int d = -1; //缓冲读入,实际上并非是一个字节一个字节从文件读取 while((d=bis.read())!=-1){ System.out.println(d); } bis.close();