SD card Read and write FileNotFoundException:/storage/emulated/0object.txt:open failed:enoent (No such file or dir

Source: Internet
Author: User

Read and write the files in the SD card as follows: 1 call Environment's Getexternalstoragestate () method to determine if the SD card is plugged into the phone and the application has the ability to read and write the SD card

If the phone is already plugged into an SD card and has the ability to read and write the SD card, the following statement will return True

Environment.getexternalstoragestate (). Equals (envronment.media_mounted)

2) Call Environment's getExternalStorageDirectory () method to get the external memory, which is the directory of the SD card

3) Use FileInputStream, FileOutputStream FileReader or FileWriter to read and write files on SD card

Registration permissions

<--! Create and delete files in SD card permissions--

<uses-permission android:name= "Android.permission.MOUNT_FORMAT_FILESYSTEMS"/>
<--! permission to write data to an SD card--->
<uses-permission android:name= "Androd.permission.WRITE_EXTERNAL_STORAGE"/>
<--! permission to read data--->
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

Instance code:

There are two EditText and two button components, Text_write is used to write data, and the Button (write) component is used to read the data in Text_write and write to the/storage/emulated/0/object.txt file;

The Button (read) component is used to read data from the/storage/emulated/0/object.txt of the SD card and is displayed in the Text_read component.


<span style= "FONT-SIZE:18PX;" ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_                Vertical_margin "tools:context=" com.example.sdcard03.mainactivity$placeholderfragment "> <EditText Android:id= "@+id/text_write" android:layout_width= "match_parent" android:layout_height= "45DP"/&    Gt <edittext android:id= "@+id/text_read" android:layout_width= "Match_parent" android:layout_height= "4 5DP "/> <button android:id=" @+id/read "android:layout_height=" Wrap_content "Android : Layout_width= "Match_parent" android:text= "read"/> <button android:id= "@+id/write" Android:layout_heigh t= "Wrap_content" android:layout_width= "match_parent" android:text= "write"/> </linearlayout></ Span>
Main program:
<span style= "FONT-SIZE:18PX;" >package com.example.sdcard03;import java.io.bufferedreader;import Java.io.file;import java.io.FileInputStream; Import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstreamreader;import Android.app.activity;import Android.os.bundle;import Android.os.environment;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Activity {private EditText read_text;private EditText write_text ;p rivate button read;private button write;private String fileName = <span style= "color: #FF6666;" > "/object.txt";</span> @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.fragment_main); read = (Button) Findviewbyid (r.id.read); write = (button) Findviewbyid (r.id.write); read_text = (EditText) Findviewbyid (r.id.text_read); write_text = (EditText) findViewbyid (R.id.text_write); Read.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {// TODO auto-generated method Stubstring s = Readfromsdcard (); Read_text.settext (s);}}); /------------------------Write.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {// TODO auto-generated method Stubstring s2 = Write_text.gettext (). toString (); Writetosdcard (S2);});} Public String Readfromsdcard () {if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {try { File Sdpath = Environment<span style= "color: #FF0000;" >.getexternalstoragedirectory () </span>;<span style= "color: #6633FF;" >system.out.println (Sdpath.tostring ()); </span>fileinputstream fis = new FileInputStream (sdPath.<span style= "COLOR: #FF0000;" >getcanonicalfile () </span>.tostring () + fileName) <span style= "color: #3366FF;" >system.out.println (Sdpath.getcanonicalfile (). toString ()), </span>bufferedreader br = new BufferedReader (New InputStreamReader (FIS)); StringBuilder sb = new StringBuilder (""); String line = null;while (line = Br.readline ())! = null) {sb.append (line);} Fis.close (); Br.close (); return sb.tostring ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} return null;} -------------------------------------------------------public void Writetosdcard (String s) {if ( Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {File Sdcarddir = Environment.getexternalstoragedirectory (); try {File file = new file (Sdcarddir.getcanonicalpath () + fileName); FileOutputStream fos = new FileOutputStream (file); Fos.write (S.getbytes ()); Fos.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} else {toast.maketext (mainactivity.this, "SD card Exception", Toast.length_long). Show ();}} </span>

Note: 1 Do not forget to register permission

2,

<span style= "FONT-SIZE:18PX;" >file Sdpath = Environment<span style= "color: #FF0000;" What is returned by the >.getexternalstoragedirectory () </span>; getexternalstoragedirectory () method? </span><pre name= "code" class= "java" ><span style= "font-size:18px;" >sdpath.<span style= "color: #FF0000;" What is returned by the >getcanonicalfile () </span>.tostring () method? The above two lines of blue output statements can be seen in Logcat return is/storage/emulated/0 Note that there is no "/" backslash after 0, so we </span><pre name= when we initially declare the string constant Filname "Code" class= "java" ><span style= "font-size:18px;" >private String fileName = <span style= "color: #FF6666;" > "/object.txt"; object has a backslash, which means the path </span></span><pre name= "code" class= "java" ><pre name= "Code" class= "java" >/storage/emulated/0<span style= "color: #FF6666;" >/object.txt</span>. If we initially declared filename without a backslash, it becomes/storage/emulated/0<span style= "color: #FF6666;" >object.txt</span&gt, this path is illegal and there is no program error 03-15 15:07:39.440:w/system.err (26730): Java.io.FileNotFoundException:/STorage/emulated/0object.txt:open failed:enoent (No such file or directory) <pre name= "code" class= "Java" ><pre N Ame= "Code" class= "java" ><pre name= "code" class= "Java" >







SD card Read and write FileNotFoundException:/storage/emulated/0object.txt:open failed:enoent (No such file or dir

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.