字节流和字符流的文件读写操作

1.乱码问题

问题:当从本地磁盘里读取.txt文件到控制台时,如果本地文件有中文,则控制台会显示乱码;

原因:本地文件是默认采用ANSI编码来进行编码的,而我们的ide采用的编码默认是UTF-8(可以采用System.getProperties().list(System.out),查看file.encoding属性的值得到自己ide默认编码。编码和解码不是同一个自然会有乱码;

解决:用notepade++将.txt文件采用utf-8重新编码即可。

2.字节流和字符流的文件读取实例

package mybatistest.file_transport;

import org.junit.Test;

import java.io.*;

public class FileImport {

    /**
     * 字节流
     * 一次读取一个字节到控制台
     */
    @Test
    public void test01() {
        File file = new File("e:\\a.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            int read = -1;
            while ((read = fis.read()) != -1) {
                System.out.print((char) read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 字节流
     * 一次读取一个固定数组
     */
    @Test
    public void test02() {
        File file = new File("e:\\a.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int read = -1;
            StringBuilder sb = new StringBuilder();
            while ((read = fis.read(bytes)) != -1) {
                sb.append(new String(bytes, 0, read));
            }

            System.out.println(sb.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节流
     * 一次读取整个文件
     */
    @Test
    public void test03() {
        File file = new File("F:\\a.txt");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);

            System.out.println(new String(bytes));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 字节流
     * 一次写一个字节
     */
    @Test
    public void test04() {
        File target = new File("f:\\a.txt");
        FileOutputStream fos = null;
        try {
            String str = "白日依山尽,黄河入海流。欲穷千里目,更上一层楼";
            byte[] bytes = str.getBytes();
            fos = new FileOutputStream(target);
            for (int i = 0; i < bytes.length; i++) {
                fos.write(bytes[i]);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节流
     * 一次写一个固定数组
     */
    @Test
    public void test05() {
        File file = new File("f:\\a.txt");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            String str = "白日依山尽,黄河入海流。欲穷千里目,更上一层楼";
            byte[] source = str.getBytes();

            for (int i = 0; i <= source.length / bytes.length; i++) {
                if (i == source.length / bytes.length) {
                    fos.write(source, bytes.length * i, source.length);
                } else {
                    fos.write(source, bytes.length * i, bytes.length * (i + 1));
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节流
     * 一次全写入
     */

    @Test
    public void test06() {
        File file = new File("f:\\a.txt");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            String str = "白日依山尽,黄河入海流。欲穷千里目,更上一层楼";
            fos.write(str.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 字符流
     * 一次读取一个字符
     */

    @Test
    public void test07() {
        File file = new File("e:\\a.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            int read = -1;
            while ((read = fr.read()) != -1) {
                System.out.print((char) read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 字符流
     * 一次读取一个固定数组
     */
    @Test
    public void test08() {
        File file = new File("e:\\a.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            char[] chars = new char[1024];
            int read = -1;
            StringBuilder sb = new StringBuilder();
            while ((read = fr.read(chars)) != -1) {
                sb.append(new String(chars, 0, read));
            }
            System.out.println(sb.toString());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符流
     * 一次写一个字符
     */
    @Test
    public void test09() {
        File file = new File("f:\\a.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(file);
            String str = "白日依山尽,黄河入海流。欲穷千里目,更上一层楼";
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                fw.write(chars[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符流
     * 一次写一个字符数组
     */
    @Test
    public void test10() {
        File file = new File("f:\\a.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(file);
            char[] chars = new char[1024];
            String str = "白日依山尽,黄河入海流。欲穷千里目,更上一层楼";
            char[] source = str.toCharArray();
            printCharArray(source);
            for (int i = 0; i <= source.length / chars.length; i++) {
                if (i == source.length / chars.length) {
                    fw.write(source, chars.length * i, source.length);
                } else {
                    fw.write(source, chars.length * i, chars.length * (i + 1));
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符流
     * 一次全部写入
     */
    @Test
    public void test11() {
        File file = new File("f:\\a.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(file);
            String str = "白日依山尽,黄河入海流。欲穷千里目,更上一层楼";
            char[] chars = str.toCharArray();

            fw.write(chars);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节流
     * 将文件e:a.txt复制到f:a.txt
     * 一次读写一个字节
     */
    @Test
    public void test12() {
        File source = new File("e:\\a.txt");
        File target = new File("f:\\a.txt");

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);

            int read = -1;
            while ((read = fis.read()) != -1) {
                fos.write(read);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节流
     * 将文件e:a.txt复制到f:a.txt
     * 一次读写一个字节数组
     */
    @Test
    public void test13() {
        File source = new File("e:\\a.txt");
        File target = new File("f:\\a.txt");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);

            byte[] bytes = new byte[1024];
            int read = -1;
            while ((read = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节流
     * 将文件e:a.txt复制到f:a.txt
     * 一次全部读写
     */
    @Test
    public void test14() {
        File source = new File("e:\\a.txt");
        File target = new File("f:\\a.txt");

        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);

            byte[] bytes = new byte[fis.available()];
            int read = fis.read(bytes);
            fos.write(bytes, 0, read);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符流
     * * 将文件e:a.txt复制到f:a.txt
     * * 一次读写一个字符
     */
    @Test
    public void test15() {
        File source = new File("e:\\a.txt");
        File target = new File("f:\\a.txt");
        FileReader fr = null;
        FileWriter fw = null;

        try {
            fr = new FileReader(source);
            fw = new FileWriter(target);
            int read = -1;
            while ((read = fr.read()) != -1) {
                fw.write(read);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.flush();
                fw.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符流
     * * 将文件e:a.txt复制到f:a.txt
     * * 一次读写一个字符数组
     */
    @Test
    public void test16() {
        File source = new File("e:\\a.txt");
        File target = new File("f:\\a.txt");
        FileReader fr = null;
        FileWriter fw = null;

        try {
            fr = new FileReader(source);
            fw = new FileWriter(target);
            char[] chars = new char[1024];
            int read = -1;
            while ((read = fr.read(chars)) != -1) {
                fw.write(chars);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.flush();
                fw.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void printCharArray(char[] chars) {
        if (chars != null) {
            for (int i = 0; i < chars.length; i++) {
                System.out.print(chars[i]);
            }
        }
    }

    @Test
    public void test(){
        System.getProperties().list(System.out);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值