Read File Content

Source: Internet
Author: User
To open a file that is private to the application in the/data/<package name>/Files directory, you can use the openfileinput () method provided by activity. Fileinputstream instream = This. getcontext (). openfileinput ("itcast.txt"); log. I ("filetest", readinstream (instream); The readinstream () method can be found below.
public static String readInStream(FileInputStream inStream){try {ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length = -1;while((length = inStream.read(buffer)) != -1 ){outStream.write(buffer, 0, length);}outStream.close();inStream.close();return outStream.toString();//byte[] data=outStream.toByteArray();
   //String result=new String(data);
} catch (IOException e) {Log.i("FileTest", e.getMessage());}return null;}

 

Or directly use the absolute path of the file: file = new file ("/data/CN. itcast. action/files/itcast.txt "); fileinputstream instream = new fileinputstream (File); log. I ("filetest", readinstream (instream); note: the "CN. itcast. action is the package where the application is located. When writing code, you should replace it with the package used by your own application. Only applications that create a private file can access the file. If you want the file to be read and written by other applications, you can specify the context. mode_world_readable and context. mode_world_writeable permissions when creating the file. Activity also provides the getcachedir () and getfilesdir () Methods: getcachedir () methods for obtaining/data/<package name>/cache directory getfilesdir () to obtain the/data/<package name>/Files directory.

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.