四、文件传输基础——Java IO流

2019-07-19  本文已影响0人  Lord丶轩莫言弃

1、文件的编码(Java是双字符编码UTF-16be编码)

2、 File 类常用 API 介绍

3、遍历目录

public class FileUtils {

    /**
     * 列出指定目录下(包括其子目录)的所有文件
     * @param dir
     * @throws IOException
     */
    public static void listDirectory(File dir) throws IOException {
        if (!dir.exists()) {
            throw new IllegalArgumentException("目录" + dir + "不存在.");
        }
        if (!dir.isDirectory()) {
            throw new IllegalArgumentException(dir + "不是目录.");
        }

        // 返回的是,直接子目录(文件)的抽象
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // 递归
                    listDirectory(file);
                } else {
                    System.out.println(file);
                }
            }
        }
    }

}

4、RandomAccessFile基本操作

5、字节流之文件输入流FileInputStream

public class IOUtils {

    /**
     * 读取指定文件内容,按照16进制输出到控制台
     * 并且每输出10个byte换行
     * @param fileName
     * @throws IOException 
     */
    public static void printHex(String fileName) throws IOException {
        // 把文件作为字节流进行读操作
        FileInputStream in = new FileInputStream(fileName);
        int b;
        int i = 1;
        while ((b = in.read()) != -1) {
            if (b <= 0xf) {
                // 单位数前面补0
                System.out.println("0");
            }
            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[2 * 1024];

        /**
         * 从in中批量读取字节,放到buf这个字节数组中,
         * 从第0个位置开始放,最多放buf.length个,
         * 返回的是读到的字节个数
         */
        int bytes = 0;// 一次性读完,说明字节数组足够大
        int j = 1;
        while ((bytes = in.read(buf, 0, buf.length)) != -1) {
            for (int i = 0; i < bytes; i++) {
                if (buf[i] <= 0xf) {
                    System.out.println("0");
                }
                System.out.print(Integer.toHexString(buf[i] & 0xff) + "  ");
                if (j++ % 10 == 0) {
                    System.out.println();
                }
            }
        }
        in.close();
    }

}

6、字节流之文件输出流FileOutputStream

public static void copyFile(File srcFile, File destFile) throws IOException {
    if (!srcFile.exists()) {
        throw new IllegalArgumentException("文件" + srcFile + "不存在.");
    }
    if (!destFile.isFile()) {
        throw new IllegalArgumentException(destFile + "不是文件.");
    }

    FileInputStream in = new FileInputStream(srcFile);
    FileOutputStream out = new FileOutputStream(destFile);
    byte[] buf = new byte[8 * 1024];
    int b;
    while ((b = in.read(buf, 0, buf.length)) != -1) {
        out.write(buf, 0, b);
        out.flush();
    }
    in.close();
    out.close();
}

7、 字节流之数据输入输出流

public static void main(String[] args) throws IOException {
    String file = "demo/test.txt";
    DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
    dos.writeInt(10);
    dos.writeInt(-10);
    dos.writeLong(10L);
    dos.writeDouble(10.12D);
    // 采用UTF-8编码写出
    dos.writeUTF("中国");
    // 采用UTF-16be编码写出
    dos.writeChars("Java");
    dos.close();
}

8、字节缓冲流

/**
* 进行文件的拷贝,利用带缓冲的字节流
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByBuffer(File srcFile, File destFile) throws IOException {
    if (!srcFile.exists()) {
        throw new IllegalArgumentException("文件" + srcFile + "不存在.");
    }
    if (!destFile.isFile()) {
        throw new IllegalArgumentException(destFile + "不是文件.");
    }

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

    int c;
    while ((c = bis.read()) != -1) {
        bos.write(c);
        bos.flush();// 刷新缓冲区
    }
    bis.close();
    bos.close();
}

9、字节字符转换流

上一篇 下一篇

猜你喜欢

热点阅读