黑马程序员_IO包中的其他类

----------- android培训java培训、java学习型技术博客、期待与您交流! ------------
IO包中的其他类
 1.PrintWriter与PrintStream
   --可以直接操作输入流和文件
 2.系列流
   --对多个流进行合并
 3.操作对象
   --ObjectInputStream与ObjectOutputStream
     --被操作的对象需要实现Serializable(标记接口)


一:打印流:   
    该流提供了打印方法,可以将各种数据类型的数据都原样打印
    ---字节打印流:
       PrintStream
       构造函数可以接受的参数类型
       1.file对象:File
       2.字符串路径:String
       3.字节输出流:OutputStream
    ---字符打印流
       PrintWriter
       构造函数可以接收的参数类型
       1.字符串路径:String
       2.file对象:File
       3.字节输出流:OutputStream
       4.字符输出流:Writer
 1.PrintWriter(OutputStream out, boolean autoFlush) 
   通过现有的 OutputStream 创建新的 PrintWriter,给它传递一个boolean类型的参数,他会自动刷新,但   一般只针对有标记换行符的打印语句
 2。实例:{
    BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
//PrintWriter out=new PrintWriter(System.out,true);
        PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("a.txt")) ,true);
String line=null;
while((line=bufr.readLine())!=null)
{
    if("over".equals(line))
break;
    out.println(line.toUpperCase());
    // out.write(line.toUpperCase());
             //out.flush();
}
out.close();
bufr.close();
    }


二:序列流:以Squence开始的一系列类
    eg1:将三个文件整合为一个文件,并输出到另一个文件
    public static void main(String[] args) throws IOException
{
 Vector<FileInputStream>v=new Vector<FileInputStream>();
 v.add(new FileInputStream("aa.txt"));
          v.add(new FileInputStream("bb.txt"));
          v.add(new FileInputStream("cc.txt"));


          Enumeration<FileInputStream>en=v.elements();
 SequenceInputStream sis=new SequenceInputStream(en);
 FileOutputStream fos=new FileOutputStream("dd.txt");


 byte[] buf=new byte[1024];
 int len=0;
          while((len=sis.read(buf))!=-1)
 {
             fos.write(buf,0,len);
 }
          fos.close();
 sis.close();
}
 
    eg2:将一幅图片或一首歌分成几部分后,又合并起来
        public static void main(String[] args) throws IOException
{
splitFile(); //该方法将一幅图片文件切割成一块一块的小部分
                merge();  //该方法用于将分割的文件合成一个整体
}
public static void merge() throws IOException
{
                ArrayList<FileInputStream>al=new ArrayList<FileInputStream>();
for(int x=1;x<=3;x++)
{
al.add(new FileInputStream                                                                ("D:\\Documents\\EditPlus\\splitfiles\\"+x+".bmp"));
}
final Iterator<FileInputStream>it=al.iterator();
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                                                         ("D:\\Documents\\EditPlus\\\splitfiles\\0.bmp");
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 splitFile() throws IOException
{
FileInputStream fis=new FileInputStream("D:\\Documents\\EditPlus\\03.bmp");
FileOutputStream fos=null;
byte[] buf=new byte[1024*1024];
int len=0;
int count=1;
while((len=fis.read(buf))!=-1)
{
fos=new FileOutputStream("D:\\Documents\\EditPlus\\splitfiles\\"+                                 (count++)+".bmp");
fos.write(buf,0,len);
fos.close();
}
fis.close();
}


 1.没有方法的接口称为标记接口
 2.静态对象是不能被系类化的,如果想非静态的对象也不被系列化,加上关键字transient,
 3.非静态对象一般放堆内存中,而静态对象放在文本文件中
    ObjectInputStream与ObjectOutputStream必须是成对出现的
    eg:class Person implements Serializable
    {
    public static final long serialVersionUID = 42L;
    //表示适合任何访问修饰符,给类定义一个固定标识。访问修饰符即public 、private、protector三种
    private String name; //这里加上private修饰后可能不能使用原来被系类化的对象,还得重新加载对象
    transient int age;
static String country="cn";
Person(String name,int age,String country)
{
   this.name=name;
   this.age=age;
this.country=country;
}
public String toString()
{
return name+":"+age+":"+country;
}
    }


三:管道流
 1.PipedInputStream和PipedOutputStream
   1.1:输入输出可以直接进行连接,通过结合线程使用
 2.PipedInInputStream:管道输入流应该连接到管道输出流;管道输入流提供要写入管道输出流的所有数据字   节
 3.PipedOutputStream:可以将管道输出流连接到管道输入流来创建通信管道。管道输出流是管道的发送端。
 4.通常,数据由某个线程写入 PipedOutputStream 对象,并由其他线程从连接的 PipedInputStream 读取。   不建议对这两个对象尝试使用单个线程,因为这样可能会造成该线程死锁。如果某个线程正从连接的管道输    入流中读取数据字节,但该线程不再处于活动状态,则该管道被视为处于 毁坏 状态。 
 eg:
    import java.io.*;
    class  Read implements Runnable
    {
private PipedInputStream in;
Read(PipedInputStream in)
{
this.in=in;
}
public void run()
{
  try
  {
byte[]buf=new byte[1024];
System.out.println("读取前没有数据就堵塞");
int len=in.read(buf);
System.out.println("读到数据,诸塞结束");
String s=new String(buf,0,len);
System.out.println(s);
in.close();
  }
  catch (IOException e)
  {
throw new RuntimeException("管道流读取失败");
  }
}
    }
    class Write implements Runnable
    {
private PipedOutputStream out;
Write(PipedOutputStream out)
{
  this.out=out;
}
public void run()
{
           try
  {
System.out.println("开始写入数据,等待6秒后");
try
{
  Thread.sleep(6000);
       }
catch(Exception e)
{
          e.printStackTrace();
}
  out.write("piped lai la".getBytes());
  out.close();
}
catch (IOException e)
{
  throw new RuntimeException("管道输出流失败");
}  
  }
        }
    }
    class PipedStreamDemo 
    {
public static void main(String[] args) throws IOException
{
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream();
in.connect(out);


Read r=new Read(in);
Write w=new Write(out);
new Thread(r).start();
        new Thread(w).start();
}
    }


四:RandomAccessFile类
   此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型        byte 数组。存在指向该隐含数组的光标或索引,称为文件指针;输入操作从文件指针开始读取字节,并随     着对字节的读取而前移此文件指针。如果随机访问文件以读取/写入模式创建,则输出操作也可用;输出操     作从文件指针开始写入字节,并随着对字节的写入而前移此文件指针。写入隐含数组的当前末尾之后的输出    操作导致该数组扩展。该文件指针可以通过 getFilePointer 方法读取,并通过 seek 方法设置。
 1.该类不算是io体系中的子类,而是直接继承自Object。但是它是io包中成员,因为它具备读和写功能。
 2.它的内部封装了一个数组,而且通过指针对数组的元素进行操作可以通过getFilePointer获取指针的位置
   同时可以通过seek改变指针的位置。
 3.他能完成读写的原理就是内部封装了字节输入流和输出流
 4.通过构造函数可以看出,该类只能操作文件,而且操作文件还有模式:只读r、读写rw、rws、rwd四种
   如果模式为只读r,不会创建文件,会去读取一个已存在的文件,如果该文件不存在
   则出现异常。如果模式为:rw ,操作文件不存在,操作文件不存在,会自动创建,如果存在则不会覆盖。
   eg:System.out.println(Integer.toBinaryString(258));
//实现将258转化为二进制,并打印出来
       RandomAccessFile raf=new RandomAccessFile("ran.txt","r");
        //调整对象中的指针
          raf.seek(8);


//跳过指定的字节数
          raf.skipBytes(8); 
 
        //skipBytes()不能往前跳,只能往后跳,而seek()既可以往前跳,又可以往后跳,任意指
        raf.write("王五".getBytes());
raf.writeInt(99);
//注意:Write只写最后8位,会丢失数字位。要想保持原样性,即写入的是数字。输出也为                          数字,则需将258所对应的四个字节读写出去。
raf.close();
        //RandomAccessFile类的对象也需调用close()方法


五:操作基本数据类型:DataInputStream与DataOutputStream 
 1.DataInputStream与DataOutputStream可以用于操作基本数据类型的数据的流对象
 2.如果对方用的是writeUTF()方法写的,那么只能用readUTF()方法读出来 
 3.writeUTF(String str): 以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。
 eg:
    DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(234);
dos.writeBoolean(true);
dos.writeDouble(9887.543);
dos.close();
    DataInputStream dis=new DataInputStream(new FileInputStream("data.txt"));
        int num=dis.readInt();
boolean b=dis.readBoolean();
double d=dis.readDouble();
System.out.println("num="+num);
        System.out.println("b="+b);
        System.out.println("d="+d);
dis.close();


六:操作字节数组:ByteArryInputStream与ByteArrayOutputStream
 1.ByteArrayInputStream:包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪 read 方     法要提供的下一个字节。关闭 ByteArrayInputStream无效。此类中的方法在关闭此流后仍可被调用,而不     会产生任何 IOException。
 2.ByteArrayOutputStream:此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的   不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。关闭 ByteArrayOutputStream无效    此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。 
 3.ByteArrayInputStream:在构造的时候,需要接收数据源,而且数据源是一个字节数组
 4.ByteArrayOutputStream:在构造的时候。不用定义数据目的地,因为该对象中已经内部封装了可变长度的字    节数组。这就是数据目的地。因为这两个流对象都操作的是数组,并没有使用系统资源,所以,不用进行     close关闭
 5.用流的读写思想来操作数据、
   eg:public static void main(String[] args) throws IOException
{
//数据源
ByteArrayInputStream bis=new ByteArrayInputStream("ABCDEFG".getBytes());
//数据目的
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int by=0;
while((by=bis.read())!=-1)
{
bos.write(by);
}
System.out.println(bos.size());
System.out.println(bos.toString());
//bos.writeTo(new FileOutputStream("a.txt"));
//该类中只有此方法会发生异常
}


七:操作字符数组:CharArrayReader与CharArrayWriter
八:操作字符串:StringReader与StringWriter

-----------------------  android培训 java培训 、java学习型技术博客、期待与您交流! ----------------------
       

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值