文件字节流
-
FileInputStream: extends InputStream
- public int read(byte[] b)://从流中读取多个字节,将读到内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1.
-
FileOutputStream: extends OutputStream
- public int write(byte[] b): //一次写多个字节,将b数组中的所有字节,写入输出流。
示例:
public class IOStream { public static void main(String[] args) throws Exception{ //1.创建文件file输入流, 指定文件路径(绝对路径), FileInputStream file = new FileInputStream("D:\Desktop\file.txt"); //2.读取文件 int read = file.read(); System.out.println(read); //49 System.out.println((char) read); //1 System.out.println("==============================="); //2.1单个字节读取 int data = 0; while ((data=file.read()) != -1) { System.out.println((char) data); } System.out.println("==============================="); //2.2一次性读取多个字符 byte[] bytes = new byte[3]; int count = 0; while ((count=file.read(bytes)) != -1){ System.out.println(new String(bytes, 0, count)); //从第0个数开始,到count个数结束。 } System.out.println("==============================="); //3.关闭流 file.close(); //节省资源,必须 *** 作 System.out.println(); System.out.println("执行完毕"); } //因为read,data,count是对同一个 FileInputStream进行 *** 作,所以,当read读取一位后,data的while循环将剩下的都读了,会导致count的 file.read(bytes) == -1,所以会直接跳过循环。 }
public static void main(String[] args) throws Exception{ //1.创建File字节输出流对象 , true则是在文件末尾追加,默认false: 若文件存在,直接进行覆盖 FileOutputStream file = new FileOutputStream("D:\Desktop\file2.txt", true); //2.写入文件 file.write(1); file.write(23); file.write(456); file.write('a'); file.write("ABCDEF".getBytes()); //3.关闭流 file.close(); System.out.println("流已经关闭"); } //文件内容:萢ABCDEF 这是由于没有指定流输出的编码,导致出现了乱码。
public static void main(String[] args) throws Exception{ //1.创建输入输出流 //1.1创建输ru字节流 FileInputStream file1 = new FileInputStream("D:\Desktop\file1.txt"); //1.2创建输chu字节流 会对file2的源文件进行覆盖 FileOutputStream file2 = new FileOutputStream("D:\Desktop\file2.txt"); //1.3创建输chu字节流 会对file2的源文件进行追加 FileOutputStream file3 = new FileOutputStream("D:\Desktop\file3.txt", true); //2.边读编写 byte[] bytes = new byte[1024]; //1024个字节 int count = 0; while((count=file1.read(bytes)) != -1){ //每次最大读取1024个字节 // file2.write(bytes, 0, count); //每次最大写入1024个字符 file3.write(bytes, 0, count); } //当file2和file3同时对流进行 *** 作时,流只对前一个写入产生作用 //3.关闭流 file1.close(); // file2.close(); file3.close(); System.out.println("复制完毕"); }缓冲字节流 输入
public static void main(String[] args) throws Exception{ FileInputStream file1 = new FileInputStream("D:\Desktop\file1.txt"); //1.创建字节缓冲输入流 BufferedInputStream buf1 = new BufferedInputStream(file1); //2.读取 // int read = buf1.read(); // System.out.println(read); //49 // System.out.println((char) read);//1 //3.创建缓冲区 byte[] bytes = new byte[1024]; int count = 0; while ((count=buf1.read(bytes)) != -1){ System.out.println(new String(bytes, 0, count)); } //4.关闭流(内部调用了flush方法),buf1关闭,file1也关闭了 buf1.close();输出
public static void main(String[] args) throws Exception{ FileOutputStream file2 = new FileOutputStream("D:\Desktop\file2.txt"); BufferedOutputStream buf2 = new BufferedOutputStream(file2); for (int i = 0; i <10 ; i++) { buf2.write("1234567890n".getBytes()); } buf2.close(); System.out.println("写入完毕"); }字节缓冲流的文件复制
public static void main(String[] args) throws Exception{ FileInputStream file1 = new FileInputStream("D:\Desktop\file1.txt"); FileOutputStream file2 = new FileOutputStream("D:\Desktop\file2.txt"); FileOutputStream file3 = new FileOutputStream("D:\Desktop\file3.txt", true); BufferedInputStream buf1 = new BufferedInputStream(file1); BufferedOutputStream buf2 = new BufferedOutputStream(file2); BufferedOutputStream buf3 = new BufferedOutputStream(file3); //创建缓冲区 byte[] bytes = new byte[1024]; int count = 0; while ((count= buf1.read(bytes)) != -1){ buf2.write(bytes, 0, count); buf3.write(bytes, 0, count); //缓冲区的流能进行多次 *** 作 } //方法write,把数据写入到内部缓冲区 // 方法flush,把内部缓冲区中的数据,刷新到文件中。 buf1.close(); buf2.close(); buf3.close(); //close内部调用了 flush方法 System.out.println("复制完毕"); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)