Java IO流(二)

主要从以下来说:

    编码问题

    File类的使用

    RandomAccessFile

    字节流

    字符流

    对象的序列化和反序列化

接着上一篇

    5:字节流

        1):有两个父类:InputStream,OutputStream

            InputStream抽象了应用程序读取数据的方法,outputStream抽象了应用程序写出数据的方式

        2):EOF=end 读到-1就读到结尾

        3):输入流基本方法:

基本方法 作用
int b=输入流对象.read() 读取一个字节无符号填充到int低八位,-1是EOF
输入流对象.read(byte[] buf) 读取数据填充到字节数组buf
输入流对象.read(byte[] buf,int start,int size) 读取数据填充到字节数组buf,从buf的start位置开始,存放size长度的数据

        4):输出流基本方法:

基本方法 作用
out.write(int b) 写出一个byte到流,b的低8位
out.write(byte[] buf) 将buf字节数组都写入到流
out.write(byte[] buf,int start,int size) 将buf字节数组写入到流,从buf的start位置开水,写size长度的数据
    5):FileInputStream:具体实现了在文件上读取数据
public class IOutil {
    /**
     * 读取指定文件内容,按照16进制输出到控制台中
     * 并且每输出10个byte换行
     * @param fileName
     */
    public static void printHex(String fileName )throws IOException{
        FileInputStream in=new FileInputStream(fileName);
        int b;
        int i=1;
        while ((b=in.read())!=-1){
            System.out.print(Integer.toHexString(b)+" ");
            if(i++%10==0){
                System.out.println();
            }
        }
        //关闭文件流
        in.close();
    }

    public  static void printHexByByteArray(String fileName) throws IOException{
        FileInputStream in=new FileInputStream(fileName);
        byte[] buf=new byte[20*1024];
        int j=1;
        /**
         * 从in中批量读取字节,放入到buf中,从0位置开始放,最多放buf.length个,返回的是读到的字节的个数
         */
          int b=0;
          while((b=in.read(buf,0,buf.length))!=-1){
              for(int i=0;i<b;i++){
                System.out.print(Integer.toHexString(buf[i] & 0xff)+" ");
              }
              if(j++%10==0){
              System.out.println();
              }
          } in.close();
  }
    public static void main(String[] args)throws IOException{
        IOutil.printHex("E:\\test\\javaio.txt");
        IOutil.printHexByByteArray("E:\\test\\javaio.txt");
    }
}

    5):FileOutputStream输出流:实现了向文件中写出byte数据的方法 

public class FileOutputStreamUtil {
    public static void main(String[] args) throws IOException{

        //如果该文件不存在,则直接创建,如果存在,删除后创建,可以在后面追加:true,表示如果存在,不删除
        FileOutputStream out=new FileOutputStream("javaio:\\out.dat");
        out.write('A');
        out.write('B');
        int a=10;//write只能写8位,则要进行四次写入
        out.write(a>>>24);
        out.write(a>>>18);
        out.write(a>>>8);
        out.write(a);
        //也可以用字节数组来进行写入
        String s="输出流";
        byte[] bytes=s.getBytes("gbk");
        out.write(bytes);
        out.close();
    }
}

     6):数据输出与输入流

    DataOutputStream/DataInputStream:对"流"功能的扩展,可以更加方便读取int,long,字符等类型数据

    

public class DateStream {

    /**
     * 数据流写入
     * @throws IOException
     */
    public static void dataOutputStream() throws IOException{
        String file="javaio/date.dat";
        DataOutputStream dos=new DataOutputStream(new FileOutputStream(file));
        dos.writeInt(10);
        dos.writeInt(-10);
        dos.writeLong(10l);
        dos.writeDouble(10.5);
        //采用utf-8编码写出
        dos.writeUTF("数据流");
        //采用utf-16be编码写出
        dos.writeChars("输出流");
        dos.close();
    }

    /**
     * 数据流读出
     * @throws IOException
     */
    public static void dataInputStream() throws IOException{
        String file="javaio/date.dat";
        DataInputStream dis=new DataInputStream(new FileInputStream(file));
        int i=dis.readInt();
        System.out.println(i);
        i=dis.readInt();
        System.out.println(i);
        long l=dis.readLong();
        System.out.println(l);
        double d=dis.readDouble();
        System.out.println(d);
        String s=dis.readUTF();
        System.out.println(s);
        char a=dis.readChar();
        System.out.println(a);
        dis.close();
    }
    
    public static void main(String[] args) {
        try {
            DateStream.dataOutputStream();
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            DateStream.dataInputStream();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

    7):字节缓冲流

    BufferedInputStream/BufferedOutputStream流:这两个流为IO提供了带缓冲区的操作,一般打开文件进行写入或读取操作时,都会加上缓冲,这种流模式提高了IO的性能

    

猜你喜欢

转载自blog.csdn.net/phoenix_tgd/article/details/79839234