黑马程序员-常用的IO类使用,PrintStream,SequenceInputStream,文件切割,ObjectStream

-------------------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;
    }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值