[Zhuan] Android Exception Handling: Java.lang.IllegalArgumentException (... contains a path separator)

Source: Internet
Author: User

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

For the following error:

Java.lang.runtimeexception:java.lang.illegalargumentexception:file/data/data/com.alex.datasave/files/user.txt Contains a path separator

Original code:

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

Correct code:

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

Attention:
1.FileInputStream and Openfileinput

Additional knowledge:

Public abstract FileOutputStream openfileoutput (String name, int mode) SINCE:API Level 1

Open a private file associated with the 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, to MODE_APPEND append to a existing file, and to MODE_WORLD_READABLE MODE_WORLD_WRITEABLE control permissions.
Returns
    • FileOutputStream resulting output stream.

If you want to open a file stored in the/data/data/<package name>/files directory app, you can use the activity to provide the Openfileinput () method.
FileInputStream instream = This.getcontext (). Openfileinput ("User.txt");
LOG.I ("Filetest", Readinstream (instream));
Readinstream () Please see the following remarks on this page.


or use the absolute path of the file directly:
File File = new file ("/data/data/com.alex.datasave/files/user.txt");
FileInputStream instream = new FileInputStream (file);
LOG.I ("Filetest", Readinstream (instream));
Note: "Com.alex.datasave" in the file path above is the package that the app is in, and when you write the code you should replace it with the package used by your own app.
For private files can only be accessed by the app that created the file, if you want the file to be read and written by another app, you can specify context.mode_world_readable and Context.mode_world_writeable permissions when you create the file

[Zhuan] Android Exception Handling: Java.lang.IllegalArgumentException (... contains a path separator)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.