常见异常

异常一:

java.lang.IllegalArgumentException: File /mnt/sdcard/leichi.txt contains a path separatorFile

出错的代码:

        String path = Environment.getExternalStorageDirectory().toString();
        String fileNamePath = path +File.separatorChar+"srt"+File.separatorChar+fileName;
  
         FileOutputStream fileOutputStream = context.openFileOutput(fileNamePath, Context.MODE_APPEND);
         fileOutputStream.write(msg.getBytes());
        fileOutputStream.close();

正常的代码:

        String path = Environment.getExternalStorageDirectory().toString();
        String fileNamePath = path +File.separatorChar+"srt"+File.separatorChar+fileName;
        File file = new File(fileNamePath);

    FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(msg.getBytes());
            fileOutputStream.close();


原因:

1.FileInputStream  与openFileInput 

补充知识:

public abstract FileOutputStream openFileOutput (String name,int mode)
Since:  APILevel 1

Open a private file associated with this Context's applicationpackage for writing. Creates the file if it doesn't alreadyexist.

Parameters
name The name of the file to open; can not contain pathseparators.
mode Operating mode. Use 0 or MODE_PRIVATE forthe default operation, MODE_APPEND toappend to an existing file, MODE_WORLD_READABLE andMODE_WORLD_WRITEABLE tocontrol permissions.
Returns
  • FileOutputStream Resulting output stream.

如果要打开存放在/data/data/<packagename>/files目录应用私有的文件,可以使用Activity提供openFileInput()方法。
也就是说openFileInput创建的文件必须放到/data/data/<packagename>/files目录下,不能随便制定目录。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值