IO

来源:互联网 发布:恶意软件分析实战 编辑:程序博客网 时间:2024/04/29 00:24

IO

编码

public class EncodeDemo {    public static void main(String[] args) {        String s = "慕课ABC";        // 转换成字节序列用的是项目默认的编码        //byte[] bytes1 = s.getBytes();         byte[] bytes1;        try {            // 以指定编码方式转换            bytes1 = s.getBytes("gbk");            for (byte b : bytes1) {                // 把字节(转换成了int)以16进制的方式显示                System.out.print(Integer.toHexString(b&0xff)+" ");            }            System.out.println();        } catch (Exception e) {        }         /**         * gbk 编码中文占用2个字节,英文占用1个字节         * utf-8  编码中文占用3个字节,英文占用1个字节         * java是双字节编码utf-16be         */        byte[] bytes4 = null ;        try {            bytes4= s.getBytes("utf-16be");            for (byte b : bytes4) {                System.out.print(Integer.toHexString(b&0xff)+" ");            }            System.out.println();        } catch (Exception e) {        }        /**         * 当你的字节序列是某种编码时,这时候想把字节序列转换成         * 字符串,也需要使用这种编码方式,否则会出现乱码         */        String str1 = new String(bytes4); // 用项目默认的编码        System.out.println(str1);        try {            String str2 = new String(bytes4,"utf-16be");            System.out.println(str2);        } catch (Exception e) {        }        /**         * 文本文件 就是字节序列         * 可以是任意编码的字节序列         * 如果我们在中文机器上直接创建文本文件         * 那么该文件只识别ANSI编码         */    }}

这里写图片描述

File基本API

public class FileDemo1 {    public static void main(String[] args) {        /**         * java.io.File类用于表示文件(目录)         * File类只用于表示文件(目录)的信息         * 不能用于文件内容的访问         */        String path = "D:\\javaio";        File file = new File(path);        System.out.println("文件(目录)是否存在: "+file.exists());        if(!file.exists()){            // 创建多级目录            file.mkdirs();        }        System.out.println("是否是一个目录: "+file.isDirectory());        System.out.println("是否是文件: "+ file.isFile());        File file2 = new File("D:\\javaio\\日记1.txt");        if(!file2.exists()){            try {                // 不存在则创建                file2.createNewFile();            } catch (IOException e) {            }        } else {            System.out.println("file2文件已存在");        }        File file3 = new File("D:\\javaio","日记2.txt");        if(!file3.exists()){            try {                // 不存在则创建                file3.createNewFile();            } catch (IOException e) {            }        } else {            System.out.println("file3文件已存在");        }        System.out.println(file3); // D:\javaio\日记2.txt        System.out.println(file3.getAbsolutePath()); // D:\javaio\日记2.txt        System.out.println(file3.getName());  // 日记2.txt         System.out.println(file3.getParent()); // D:\javaio        System.out.println(file3.getParentFile()); // D:\javaio        System.out.println(file3.getParentFile().getAbsolutePath()); // D:\javaio    }}

这里写图片描述

遍历所有的文件和目录

    /**     * 列出指定目录下(包括其子目录)的所有文件     * @param dir     */    public static void listDirectory(File dir){        if(!dir.exists()){            System.out.println("目录不存在");            return;        }        if(!dir.isDirectory()){            System.out.println("不是目录");            return;        }        // 返回的是字符串数据 不包含子目录下的内容//      String[] filenames = dir.list();//      for (String string : filenames) {//          System.out.println(dir+"\\"+string);//      }        // 如果要遍历子目录下的内容就需要构造称File对象做递归        File[] files = dir.listFiles(); // 直接返回的是直接子目录的抽象        if(files!=null&&files.length>0)        for (File file : files) {            // System.out.println(file);            if(file.isDirectory()){                // 递归                listDirectory(file);            } else {                System.out.println(file);            }        }    }

这里写图片描述

RandomAccessFile

public class FileDemo2 {    public static void main(String[] args) {        /**         * RandomAccessFile java提供的对文件内容的访问         * 既可以读文件,也可以写文件         * 支持随机访问文件,可以访问文件的任意位置         *          * java文件模型         *   在硬盘上的文件是byte存储的,是数据的集合         * 打开文件         *   有两种模式 rw(读写) r(只读)         * 文件指针         *   代开文件时指针在开头 pointer = 0;         * 写方法         *    write 只写一个字节(后8位),同时指针指向下一个位置         *    准备再次写入         * 读方法         *   read 读一个字节         * 文件读写完成以后一定要关闭         *          */        try {            String mode = "rw";            File file = new File("d:\\javaio\\test","日记1.txt");            File parentFile = file.getParentFile();            if(!parentFile.exists()){                parentFile.mkdirs();            }             if(!file.exists()){                file.createNewFile();            }            RandomAccessFile ra = new RandomAccessFile(file, mode);            ra.write("abcd".getBytes("utf-8"));            long filePointer = ra.getFilePointer(); // 获取指针位置0开始            System.out.println("当前位置:"+filePointer);            ra.seek(0); // 把指针移到开始i位置            // 一次性读取,把文件中的内容都读到字节数组中            byte[] buf = new byte[(int)ra.length()];            // 讲内容读到字节数组中            ra.read(buf);            System.out.println("读取到的字符: "+new String(buf)); //abcd            ra.close();        } catch (Exception e) {        }    }}

这里写图片描述

0 0
原创粉丝点击