http://blog.csdn.net/alex_zhuang/article/details/7340901

对以下错误:

Java.lang.RuntimeException: java.lang.IllegalArgumentException: File /data/data/com.alex.datasave/files/user.txt contains a path separator

原先代码:

fis = this.context.openFileInput("/data/data/com.alex.datasave/files/user.txt");

正确代码:

File file = new File("/data/data/com.alex.datasave/files/user.txt");
fis = new FileInputStream(file);

注意: 
1.FileInputStream  与 openFileInput

补充知识:

public abstract FileOutputStream openFileOutput (String name, int mode)

Since: API Level 1

Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

Parameters
name The name of the file to open; can not contain path separators.
mode Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_APPEND to append to an existing file, MODE_WORLD_READABLE andMODE_WORLD_WRITEABLE to control permissions.
Returns
  • FileOutputStream Resulting output stream.

如果要打开存放在/data/data/<package name>/files目录应用私有的文件,可以使用Activity提供openFileInput()方法。
FileInputStream inStream = this.getContext().openFileInput("user.txt");
Log.i("FileTest", readInStream(inStream));
readInStream()的方法请看本页下面备注。

或者直接使用文件的绝对路径:
File file = new File("/data/data/com.alex.datasave/files/user.txt");
FileInputStream inStream = new FileInputStream(file);
Log.i("FileTest", readInStream(inStream));
注意:上面文件路径中的“com.alex.datasave”为应用所在包,当你在编写代码时应替换为你自己应用使用的包。
对于私有文件只能被创建该文件的应用访问,如果希望文件能被其他应用读和写,可以在创建文件时,指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限

[zhuan]Android 异常处理:java.lang.IllegalArgumentException(...contains a path separator)的更多相关文章

  1. android DownloadManager: java.lang.IllegalArgumentException: Not a file URI: content://

    DownloadManager 使用Uri指定下载路径Bug 使用DownloadManager 下载文件 通常写法: DownloadManager dm = (DownloadManager) g ...

  2. 异常处理 | java.lang.IllegalArgumentException: Request header is too large

    背景 今天在自己使用Springboot开发博客系统的时候,在浏览器点击提交markdown文件一直出现以下异常: java.lang.IllegalArgumentException: Reques ...

  3. Android 多点触控错误处理(java.lang.IllegalArgumentException: pointerIndex out of range)

    最近做View的多点触控时,每次第一次触控事件完美运行,第二次就直接崩了,错误信息如下: 01-03 00:05:44.220 4377-4410/system_process E/AndroidRu ...

  4. java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@450b2f48 异常处理

    晕死的错误,改了半天也没想到是这样的原因,基础正要呀... 先看一下警告信息: 07-07 08:32:19.540: WARN/WindowManager(74): Failed looking u ...

  5. [Android] View.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)

    转自: http://blog.csdn.net/brokge/article/details/8536906 setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息 ...

  6. 关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析

    在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log: // CRASH: com.meizu.media.painter (pid 12491) // Short Msg: java. ...

  7. android 如何分析java.lang.IllegalArgumentException: Cannot draw recycled bitmaps异常

    这类问题的分析,通常你需要找到bitmap对象已经在那个位置recyle,然后检查代码. 如何定位的位置,其中代码具有对bitmap 目的recyle.能够 Bitmap.java的recycle方法 ...

  8. [Android]Caused by: java.lang.IllegalArgumentException: Service not registered.md

    Caused by: java.lang.IllegalArgumentException: Service not registered: org.diql.aidldemo.MainActivit ...

  9. Android java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inn

    AsyncTaskLoader: http://developer.Android.com/intl/zh-CN/reference/android/content/AsyncTaskLoader.h ...

随机推荐

  1. 第八模块:算法&设计模式、企业应用 第2章 企业应用工具学习

    第八模块:算法&设计模式.企业应用 第2章 企业应用工具学习

  2. es6笔记4^_^function

    一.function默认参数 现在可以在定义函数的时候指定参数的默认值了,而不用像以前那样通过逻辑或操作符来达到目的了. es5 function sayHello(name){ //传统的指定默认参 ...

  3. leetcode-单词探索

    单词搜索     给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母 ...

  4. 春招实习汇总(7个offer)

    转载出处 刚从北京到家,总算也可以歇歇了,最近一段时间真是忙于奔命的感觉,也确实体会到了找工作的艰辛,总而言之,求职之路,如人饮水,冷暖自知. 我想把这段时间找工作的体验和经历分享出来告诉大家,让大避 ...

  5. SPOJ 8073 The area of the union of circles(计算几何の圆并)(CIRU)

    Description You are given N circles and expected to calculate the area of the union of the circles ! ...

  6. POJ 2187 Beauty Contest(凸包+旋转卡壳)

    Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...

  7. vue.js学习之 如何在better-scroll加载完成后,自动滚动到最底部

    首先我们需要使用scrollTo这个方法: scrollTo(x, y, time, easing) 参数: {Number} x 横轴坐标(单位 px) {Number} y 纵轴坐标(单位 px) ...

  8. php分页类的实现与调用 (自我摘记)

    page.class.php <?php namespace Component; class Page { private $total; //数据表中总记录数 private $listRo ...

  9. Python中的import语句

    Python中的import语句是导入一个文件,这条语句主要做三件事: 1 通过一定的方式,搜寻要导入的文件: 2 如果需要,就编译这个文件: 3 运行这个文件 但是,需要注意的是,所有这三个步骤,都 ...

  10. Java常用类之File类

    File 类: 1. java.io.File 类代表系统文件名(路径名.文件名); 2. File 类常见的构造方法: 2.1. File(String pathname):通过将给定路径名字符串转 ...