Android输出csv文件乱码

问题:Android输出csv文件乱码

解决方案:
output.write(new byte[]{(byte) 0xEF, (byte) 0xBB,(byte) 0xBF}); //代码中的这个是为了解决乱码用的

//往SD卡写入文件的方法
    public static String savaFileToSD(String fileName, ArrayList<String> list,Context context) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        Log.d(TAG,"come in saveFileToSD");
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//            fileName = context.getExternalFilesDir(null).getAbsolutePath() + "/" + fileName; //内部存储
            fileName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + fileName;
            Log.d(TAG,"savaFileToSD fileName:"+fileName);
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
            File file = new File(fileName);
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream output = null;
            OutputStreamWriter oStreamWriter = null;
            try {
                output = new FileOutputStream(fileName);
                output.write(new byte[]{(byte) 0xEF, (byte) 0xBB,(byte) 0xBF});
                oStreamWriter = new OutputStreamWriter(output, "utf-8");

                for (String sms :list){
                    oStreamWriter.append(sms);
                    oStreamWriter.append("\r\n");
                }
                //将String字符串以字节流的形式写入到输出流中
            } catch (Exception e) {
                Log.e(TAG, "saveFileTOSD error: " + e.getMessage(), e);
            } finally {
                try {
                    oStreamWriter.close();
                    output.close();
                    //关闭输出流
                } catch (Exception e) {
                }
            }

            return fileName;
        } else Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
        return "";
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值