黑马程序员-常用的IO类使用,PrintStream,SequenceInputStream,文件切割,ObjectStream
2015-06-10 13:24
519 查看
-------------------Android培训 java培训 期待与您的交流!--------------------------
// /* 打印流: 该流提供了打印方法,可以将各种数据类型的数据都原样打印。 字节打印流: PrintStream 构造函数可以接受的参数类型: 1.file对象 2.字符串路径。String 3.字节输出流。OutputStream 字符打印流: PrintWriter 构造函数可以接受的参数类型: 1.file对象 2.字符串路径。String 3.字节输出流。OutputStream 4.字符输出流。Writer. 用起来和FileWriter,但是打印更加方便 */ import java.io.*; class PrintStreamDemo { public static void main(String[] args) throws IOException { BufferedReader bufr=new BufferedReader(new FileReader("F:\\Demo15.java")); PrintWriter pw=new PrintWriter(new FileWriter("F:\\a.txt"),true); //第一个参数只能是流对象,所以把路径封装,true,能标示println()自动清缓存 String line=null; //对文件的读取 while ((line=bufr.readLine())!=null) { pw.println(line); } bufr.close(); pw.close(); } } /* SequenceInputStream 可以将多个InputStream连接起来,变成一个大流 */ import java.util.*; import java.io.*; class SequenceInputStreamDemo { public static void main(String[] args) throws IOException { Vector<FileInputStream> v=new Vector<FileInputStream>(); //Vector有返回枚举elements的方法 v.add(new FileInputStream("F:\\TestTxt\\test1.txt")); //向里面添加流对象 v.add(new FileInputStream("F:\\TestTxt\\test2.txt")); v.add(new FileInputStream("F:\\TestTxt\\test3.txt")); Enumeration<FileInputStream> en=v.elements(); //返回向量的组件的枚举 SequenceInputStream sis=new SequenceInputStream(en); //用过合并流将枚举流对象合并 //测试将合并流对象打印出来 FileOutputStream fos=new FileOutputStream("F:\\TestTxt\\test4.txt"); byte [] buf=new byte[1024]; int len=0; while ((len=sis.read(buf))!=-1) { fos.write(buf,0,len); } fos.close(); sis.close(); } } /* 需求:对文件进行等份切割,最后将切割的结果还原成源文件 思路分析: 1.将一个完整文件,通过缓存数组确定分割文件的大小。 2.达到缓存的,就将结果写入一个新的文件中。知道所有都分割完。 3.利用SequenceInputStream将各个切割文件连接在一起。 4.合并成一个完整的源文件。 */ import java.io.*; import java.util.*; class SqlitFile { static int j=2; public static void main(String[] args) throws IOException { qieGe(); merge(); } public static void merge() throws IOException { ArrayList<FileInputStream> a1=new ArrayList<FileInputStream>(); //ArrayList是没有返回enumration的方法 for (int i=1;i<=j;i++ ) { a1.add(new FileInputStream("F:\\TestTxt\\"+i+".part")); //根据静态j知道分割了多少段 } final Iterator<FileInputStream>it=a1.iterator(); //根据iterator返回Enumerate对象 Enumeration<FileInputStream>en =new Enumeration<FileInputStream>() { public boolean hasMoreElements() { return it.hasNext(); } public FileInputStream nextElement() { return it.next(); } }; SequenceInputStream sis=new SequenceInputStream(en); FileOutputStream fos=new FileOutputStream("F:\\TestTxt\\music.mp3"); //将切割段,合成一首完整的歌曲 byte [] buf=new byte[1024]; int len=0; while ((len=sis.read(buf))!=-1) { fos.write(buf,0,len); } fos.close(); sis.close(); } public static void qieGe() throws IOException { FileInputStream fis=new FileInputStream("F:\\test.mp3"); //分割的文件 FileOutputStream fos=new FileOutputStream("F:\\TestTxt\\1.part"); //分割 byte[] buf=new byte[1024*1024]; int len=0; int count=0; //2M为一个单位 while ((len=fis.read(buf))!=-1) { if (count==2) //达到2M就 fos.close(); //完成一次写入, fos=new FileOutputStream("F:\\TestTxt\\"+(j++)+".part"); //重新创建一个流对象 count=0; //并将计数器置为0 } fos.write(buf,0,len); count++; } if(count!=0) //最后将不足2M的也创建出来 { fos.close(); } j=j-1; } } /* */ import java.io.*; class ObjectStreamDemo { public static void main(String[] args) throws Exception { //writeObj(); readObj();//必须将两个分开在两个不同的类执行,才能测试static不能序列化 } // public static void readObj() throws Exception { ObjectInputStream ois=new ObjectInputStream( new FileInputStream("F:\\TestTXT\\obj.txt")); //读取序列文件 Person p=(Person)ois.readObject(); System.out.println(p); ois.close(); } public static void writeObj() throws IOException { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("F:\\TestTXT\\obj.txt")); oos.writeObject(new Person("fsy",22,"cc")); //age是transient修饰的,而第三个参数是static oos.close(); } } import java.io.*; class Person implements Serializable //没有方法的接口,是标记接口 { static final long serialVersionUID=42L; private String name; transient int age; //transient保证在堆的变量不能被序列化 static String country="cn"; //static 存储在方法堆里面,不能被序列化 Person(String name,int age,String country) { this.name=name; this.age=age; this.country=country; } public String toString() { return name+":"+age+":"+country; } }
相关文章推荐
- Objective-C 实现2048算法类
- net.sf.json.JSONObject 和org.json.JSONObject 的差别
- QuartzCode,将iOS / MacOS 开发中使用的基础动画转换为Object-C / Swift代码
- Objective-C Autorelease Pool 的实现原理
- Windows下使用Objective-C
- JavaScript读书笔记(5)-Object Date
- Object-c内存管理简介
- Python Object Oriented Programming (面向对象编程) Generator
- Holding Your Object 5-14
- NSArray之makeObjectsPerformSelector方法论
- 第二十四篇 自作的高仿画板(1)
- java对象的内存布局(一):计算java对象占用的内存空间以及java object layout工具的使用
- IOS开发系列--Objective-C之类和对象
- 谈Objective-C Block的实现
- Object-C-自定义类型归档
- Swift项目兼容Objective-C问题汇总
- Object-C静态库代码运行报错: unrecognized selector sent to instance
- objective-c第二十一章课后练习题
- org.tinygroup.context2object-参数对象构建
- Objective-C规范注释心得——同时兼容appledoc(docset、html)与doxygen(html、pdf)的文档生成