2012-05-28 14 views
11

पढ़ें मैं एंड्रॉइड सिस्टम पर एक साधारण टेक्स्ट फ़ाइल को लिखने की कोशिश कर रहा हूं।एंड्रॉइड - केवल फाइल सिस्टम IOException

public void writeClassName() throws IOException{ 
    String FILENAME = "classNames"; 
    EditText editText = (EditText) findViewById(R.id.className); 
    String className = editText.getText().toString(); 

    File logFile = new File("classNames.txt"); 
     if (!logFile.exists()) 
     { 
      try 
      { 
      logFile.createNewFile(); 
      } 
      catch (IOException e) 
      { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 
     } 
     try 
     { 
      //BufferedWriter for performance, true to set append to file flag 
      BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
      buf.append(className); 
      buf.newLine(); 
      buf.close(); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

हालांकि, इस कोड एक निर्माण करता है: यह मेरा कोड है "java.io.IOException: खोलने में विफल रहा है: EROFS (केवल पढ़ने के लिए फाइल सिस्टम)" त्रुटि।

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="hellolistview.com" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".ClassView" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".AddNewClassView" 
     /> 

</application> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

किसी को भी किसी भी विचार है कि समस्या क्या है: मैं कोई सफलता के साथ मेरी प्रकट फ़ाइल की अनुमतियां जोड़ने के रूप में इस की कोशिश की?

उत्तर

57

क्योंकि आप फ़ाइल को रूट में लिखने की कोशिश कर रहे हैं, तो आपको फ़ाइल फ़ाइल को अपनी फ़ाइल निर्देशिका में पास करने की आवश्यकता है।

उदाहरण

String filePath = context.getFilesDir().getPath().toString() + "/fileName.txt"; 
File f = new File(filePath); 
+0

यह मेरे लिए काम किया। धन्यवाद। –

2

कोशिश डेवलपर मार्गदर्शिका में इस article से दृष्टिकोण का उपयोग करने के लिए:

String FILENAME = "hello_file"; 
String string = "hello world!"; 

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
fos.write(string.getBytes()); 
fos.close(); 
+0

यह मेरे लिए काम करता है, लेकिन मैं तारों से निपट रहा हूं। क्या बाइट्स की बजाय स्ट्रिंग लिखने के लिए FileOutputStream का उपयोग करने के लिए वैसे भी है? –

+1

वह जो भी डालता है वह एक स्ट्रिंग लिख रहा है, केवल स्ट्रिंग के बाइट्स प्रतिनिधित्व। जब आप किसी टेक्स्ट एडिटर में फ़ाइल देखते हैं, या इसे वापस (स्ट्रिंग के रूप में) में पढ़ते हैं तो आपको लिखा गया w/e का स्ट्रिंग प्रस्तुति मिलेगी। – Jug6ernaut

संबंधित मुद्दे