Java IO流(一)

主要从以下来说:

    编码问题

    File类的使用

    RandomAccessFile

    字节流

    字符流

    对象的序列化和反序列化

1:字节编码

public class EncodDemo {
    public static void main(String[] args) throws Exception{
        String s="javaIO流";
        //转换成字节序列,是工具自带的编码
        byte[] bytes=s.getBytes();
        for(byte b:bytes){
            //把字节转换成了int,以16进制的方式显示
            System.out.print(Integer.toHexString(b &0xff)+" ");
        }
        System.out.println();


        byte[] bytes1=s.getBytes("gbk");
        //在gbk中,中文占两个字节,英文占用一个字节
        for(byte b:bytes1){
            //把字节转换成了int,以16进制的方式显示
            System.out.print(Integer.toHexString(b &0xff)+" ");
        }
        System.out.println();

        //在utf-8中,中文占用三个字节,英文占用一个字节
        byte[] bytes2=s.getBytes("utf-8");
        for(byte b:bytes2){
            //把字节转换成了int,以16进制的方式显示
            System.out.print(Integer.toHexString(b &0xff)+" ");
        }
        System.out.println();


        //utf-16be中文占用2个字节,英文占用2个字节
        byte[] bytes3=s.getBytes("utf-16be");
        for(byte b:bytes3){
            //把字节转换成了int,以16进制的方式显示
            System.out.print(Integer.toHexString(b &0xff)+" ");
        }
        System.out.println();

        /**
         * 当你的字节序列是某种编码时,这个时候想把字节序列变成字符串,也需要这种编码方式,否则会出现乱码。
         */
        String str1=new String(bytes);
        System.out.println(str1);

        String str2=new String(bytes1,"gbk");
        System.out.println(str2);

        String str3=new String(bytes2,"utf-8");
        System.out.println(str3);

        String str4=new String(bytes3,"utf-16be");
        System.out.println(str4);
    }
}

  当你的字节序列是某种编码时,这个时候想把字节序列变成字符串,也需要这种编码方式,否则会出现乱码。

2:File类

    java.io.File类用于表示文件(目录),File类只用于表示文件(目录)的信息(名称,大小),不能用于文件内容的访问

    File常用API

API 作用
exists() 判断目录或者文件是否存在
mkdir() 创建一个目录
对象.isDirectory() 判断是不是一个目录
对象,isFile() 判断是不是一个文件
对象.getAbsolutePath() 获取对象的路径
对象.getName() 获取对象的名字
对象.getParent() 获取对象的上级目录
 
public class FileDemo {
    public static void main(String[] args){
        //了解构造函数的情况

        File file=new File("E:\\IOtest\\javaio.txt");
        //判断文件存在不存在
        System.out.println(file.exists());
        /*
        if(!file.exists()){
            file.createNewFile(); //创建文件
        }
        else
            file.delete();//删除文件
        */
        System.out.println(file.isDirectory());//判断是不是一个目录,如果是目录,则返回true,如果不是目录或者不存在,则返回false
        System.out.println(file.isFile());//判断是不是一个文件
        
        //常用的File对象的API
        System.out.println(file);
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getName());
        System.out.println(file.getParent());
    }
}

3:遍历File

public class FileUtils {

    /**
     * 遍历目录(包括子目录下的)
     * @param dir
     */
    public static void listDirectory(File dir)throws IOException{
        if(!dir.exists()){
            throw new  IllegalArgumentException("目录"+dir+"不存在");
        }
        if(!dir.isDirectory()){
            throw new IllegalArgumentException(dir+"不是目录");
        }
        /*
        String[] filename=dir.list();//list()方法用于列出当前目录下的子目录和文件,不包含子目录下的内容
        for(String str:filename){
            System.out.println(str);
        }
        */

        //如果要遍历子目录下的内容,就需要构造成File对象做递归操作
        File[] files=dir.listFiles();//返回File对象
        if(files!=null&&files.length>0){
            for(File file:files){
                if(file.isDirectory()){
                    //递归
                    listDirectory(file);
                }
                else {
                    System.out.println(file);
                }
            }
        }
    }
    public static void main(String[] args) throws IOException{
        File file=new File("E:\\structs");
        FileUtils.listDirectory(file);
    }
}

4:RandomAccessFile

    1): RandomAccessFile 是java提供的对文件内容的访问,即可以读文件还可以写文件。并且支持随机访问文件,可以访问文件的任意位置

    2):java文件模型

    在硬盘上的文件是byte byte byte存储的,是数据的集合    

    3):java中的文件打开有两种方式:rw(读写),r(只读),并且有文件指针,打开文件时指针在开头pointer=0;

    4):文件读写之后一定要关闭

    常用API:

API 说明
write() 只写一个字节,同时指针指向下一个位置,准备再次读写
read() 只读一个字节
close() 关闭文件
public class RafDemo {
    public static void main(String[] args) throws IOException{
        //创建目录和文件
        File file=new File("javaio");
        if(!file.exists()){
            file.mkdir();
        }
        File file1=new File(file,"a.txt");
        if(!file1.exists()){
            file1.createNewFile();
        }

        //设置文件权限
        RandomAccessFile raf=new RandomAccessFile(file1,"rw");
        //查看指针的位置
        System.out.println(raf.getFilePointer());

        //对文件进行写入,一次性只能读进一个字节
        raf.write('A');
        raf.write('B');
        System.out.println(raf.getFilePointer());//2

        int i=0x7ffffff;
        //用write()方法只能写一个字节,下面的代码与raf.writeInt(i);等价
        /*
        raf.write(i>>>24);//高8位
        raf.write(i>>>16);
        raf.write(i>>>8);
        raf.write(i);
        */
        raf.writeInt(i);
        String s="文件";
        byte[] bytes=s.getBytes("gbk");
        raf.write(bytes);
        System.out.println(raf.length());   //10

        //读文件,必须要先把文件指针移到头部
        raf.seek(0);
        //一次性读取,把文件中的内容都读到数组当中去
        byte[] bytes1=new byte[(int) raf.length()]; //length()返回的是long型
        raf.read(bytes1);
        System.out.println(Arrays.toString(bytes1));
        //以十六进制的方式输出
        for(byte bt:bytes){
            System.out.print(Integer.toHexString(bt &0xff)+",");
        }
        //关闭文件
        raf.close();
    }
}

   

    

    

猜你喜欢

转载自blog.csdn.net/phoenix_tgd/article/details/79835604
今日推荐