Java基础--IO流--中文乱码问题

FileOutputStream向文件输出中文

因为write方法中参数没有String,所以向文件中输出中,得把字符串转换成byte[]数组。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamTest01 {
    public static void main(String[] args) {
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("test",true);
           byte[] b= {97,98,99,100};
            fos.write(97);
            fos.write(b);
            //输出流向文件中输出中文的方式
            fos.write("我喜欢你".getBytes());
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

FileInputStream读取中文文件

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStringTest01 {
    public static void main(String[] args) {
        FileInputStream fis=null;
        try {
            fis=new FileInputStream("C:\\test\\copy.txt");
            byte[] b=new byte[50];
            int readcount;
            while(( readcount=fis.read(b))!=-1){

            System.out.print(new String(b,0,readcount));}
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}

在这里插入图片描述
在这里插入图片描述

字符流读取中文

在网上查了资料,发现idea的控制台用的是GBK编码。

import java.io.*;

public class FileInputStringTest01 {
    public static void main(String[] args) {
        //FileReader reader=null;
        FileInputStream fi=null;
        InputStreamReader reader=null;
        try {
             fi=new FileInputStream("C:\\test\\copy.txt");
             reader=new InputStreamReader(fi,"GBK");
           // reader=new FileReader("C:\\test\\copy.txt","UTF-8");
            char[] c=new char[10];
            int readcount;
            while(( readcount=reader.read(c))!=-1){

            System.out.print(new String(c,0,readcount));}
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值