021.15 IO流 其他流

 

IO包中的其他类
操作基本数据类型:DataInputStream与DataOutputStream
操作字节数组:ByteArrayInputStream与ByteArrayOutputStream
操作字符数组:CharArrayReader与CharArrayWriter
操作字符串:StringReader与StringWriter

 

####DataInputStream与DataOutputStream
public static void main(String[] args) throws IOException
{
    writeFile();
    readFile();
}


private static void writeFile() throws IOException
{
    FileOutputStream fos = new FileOutputStream("myfile\\data.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    
    dos.writeBoolean(true);
    
    dos.close();
}

private static void readFile() throws IOException
{
    FileInputStream fis = new FileInputStream("myfile\\data.txt");
    DataInputStream dis = new DataInputStream(fis);
    
    boolean b;
    b = dis.readBoolean();
    System.out.println(b);
    
    dis.close();
}


#####ByteArrayInputStream与ByteArrayOutputStream
public static void main(String[] args)
{
    ByteArrayInputStream bis = new ByteArrayInputStream("abcdef".getBytes());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
    int b;
    while((b = bis.read())!=-1){
        bos.write(b);
    }
    System.out.println(bos.toString());
}

 

posted @ 2018-09-15 15:44 _nbloser 阅读(...) 评论(...) 编辑 收藏