[黑马程序员](第16-22天)IO流(上)
2015-07-25 14:23
405 查看
------- android培训、java培训、期待与您交流! ----------
inputStream常用方法: int read() int read(byte[] b) int read(byte[] b,int off, int len) void close() outputStream的常用方法: void write(int b) void write(byte[] b) void write(byte[],int off,int len) void flush(); void close(); inputStream的之类: ByteArrayInputStream,FileInputStream,FilterInputStream,PipedInputStream,SequenceInputStream,ObjectInputStream outputStream的子类: ByteArrayOutputStream,FileOutputStream.... 字节流读取文件: public class Example01{ public void static main(String[] args){ int i = 0; FileInputStream inputStream = new FileInputStream("test.txt"); while(true){ i = inputStream.read(); if(i = -1){ break; } System.out.println(i); } } 字节流输出文件: public class Example02{ public void static main(String[] args){ FileOutputStream outputStream = new FileOutputStream("example.txt); String str = "黑马程序员"; byte[] bytes = str.getBytes(); for(int i = 0 ; i < bytes.length ; i ++){ outputStream.write(bytes[i]); } outputStream.close(); } } 直接用write(byte b)要首先清空原来的文件 要想在源文件后面加,用write(byte b , boolean append) 同时使用FileInputStream和FileOutputStream实现文件的拷贝: class Example04{ public void static main(String[] args){ FileInputStream in = new FileInputStream("D:\BaiduYunGuanjian"); FileOutputStream out = new FileOutputStream(E:\新建文件夹); int len ; long oldTime = System.currentTimeMillis(); while( (len = in.read() ) != -1 ){ out.write(len); } long newTime = System.currentTimeMillis(); long time = newTime - oldTime; in.close(); out.close(); } } 字节流的缓冲区 复习两个知识点: int read(byte[] b):从输入流中读取若干字节,把它们保存在参数b指定的字节数组中,返回的整数表示读取数字节 void write(bytes[] b, int off, int len):将指定byte数组中从偏移量off开始的len个字节写入输入流 public class Example05 { Public void static main(String[] args){ FileInputStream in = new FileInputStream("D:\BaiduYunGuanjia"); FileOutputStream out = new FileOutputStream("D:\新建文件夹") ; int len ; //这个值是用来记录每次读取的最大字节数的 byte[] b = new byte[1024]; while((len = in.read(b)) != -1){ out.write(b,0,len); } in.close(); out.close(); } } |
相关文章推荐
- [黑马程序员](第16-22天)IO流(中)
- [黑马程序员](第10-15天)集合类(下)
- 35 岁前程序员要规划好的四件事
- 我来自阿里政委,一个神秘的组织
- 转载是一种凝聚(之一) 国内外程序员生态圈对比
- 转载是一种凝聚(之一) 国内外程序员生态圈对比
- 【面试加分项】运行时多态与编译时多态
- 黑马程序员——多线程(二)
- 黑马程序员——集合框架(泛型限定)
- 关于程序员面试的一点点看法
- 黑马程序员——String类常见的操作方法
- 面试题10_二进制中1的个数
- (剑指Offer)面试题32:从1到n整数中1出现的次数
- 黑马程序员——Java File类总结
- 黑马程序员---数据类型及内存占用字节
- 面试题09_斐波那契数列及其变种详解——剑指offer系列
- Java程序员之从菜鸟到职场高手的必看...
- 面试题08_旋转数组的最小数字——剑指offer系列
- 高效能程序员的修炼
- android面试总结01 activity生命周期