2013-04-01 165 views
0

我試圖將數據寫入Android中的文件,但我無法這樣做。請檢查我的代碼並引導我一樣。無法將數據寫入文件

public static final File SCANNED = new File(Environment.getExternalStorageDirectory() + File.separator + "/scanned.txt"); 
private void WriteIntoScannedFile(String tmp) { 


     try{ 
     FileWriter writer = new FileWriter(Constants.SCANNED,true); 
     LineNumberReader lnr = new LineNumberReader(new FileReader(Constants.pincode_records)); 

     //writer.append("App has been activated successfully"); 
     writer.append(tmp); 
writer.append(#); 
     writer.close(); 

     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

登錄貓

04-01 12:40:24.260: W/System.err(2300): java.io.FileNotFoundException: /mnt/sdcard/scanned.txt (Is a directory) 
04-01 12:40:24.260: W/System.err(2300):  at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 
04-01 12:40:24.260: W/System.err(2300):  at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 
04-01 12:40:24.260: W/System.err(2300):  at java.io.FileOutputStream.<init>(FileOutputStream.java:94) 
04-01 12:40:24.260: W/System.err(2300):  at java.io.FileWriter.<init>(FileWriter.java:58) 
04-01 12:40:24.260: W/System.err(2300):  at com.ecomm.android.ScanShipments.WriteIntoScannedFile(ScanShipments.java:334) 
04-01 12:40:24.260: W/System.err(2300):  at com.ecomm.android.ScanShipments.MatchedShipment(ScanShipments.java:238) 
04-01 12:40:24.260: W/System.err(2300):  at com.ecomm.android.ScanShipments.AddRecord(ScanShipments.java:208) 
04-01 12:40:24.260: W/System.err(2300):  at com.ecomm.android.ScanShipments.access$5(ScanShipments.java:202) 
04-01 12:40:24.260: W/System.err(2300):  at com.ecomm.android.ScanShipments$2.onKey(ScanShipments.java:110) 
04-01 12:40:24.260: W/System.err(2300):  at android.view.View.dispatchKeyEvent(View.java:3850) 
04-01 12:40:24.260: W/System.err(2300):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
04-01 12:40:24.260: W/System.err(2300):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
04-01 12:40:24.260: W/System.err(2300):  at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:386) 
04-01 12:40:24.260: W/System.err(2300):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
04-01 12:40:24.260: W/System.err(2300):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
04-01 12:40:24.260: W/System.err(2300):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789) 
04-01 12:40:24.260: W/System.err(2300):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1746) 
04-01 12:40:24.260: W/System.err(2300):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1130) 
04-01 12:40:24.260: W/System.err(2300):  at android.app.Activity.dispatchKeyEvent(Activity.java:2073) 
04-01 12:40:24.260: W/System.err(2300):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1722) 
04-01 12:40:24.260: W/System.err(2300):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2627) 
04-01 12:40:24.260: W/System.err(2300):  at android.view.ViewRoot.handleMessage(ViewRoot.java:2030) 
04-01 12:40:24.260: W/System.err(2300):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-01 12:40:24.260: W/System.err(2300):  at android.os.Looper.loop(Looper.java:130) 
04-01 12:40:24.260: W/System.err(2300):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
04-01 12:40:24.260: W/System.err(2300):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-01 12:40:24.260: W/System.err(2300):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-01 12:40:24.260: W/System.err(2300):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
04-01 12:40:24.260: W/System.err(2300):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
04-01 12:40:24.260: W/System.err(2300):  at dalvik.system.NativeStart.main(Native Method) 
04-01 12:40:24.260: I/System.out(2300): returning from verified 
04-01 12:40:24.310: W/KeyCharacterMap(2300): No keyboard for id 0 
04-01 12:40:24.310: W/KeyCharacterMap(2300): Using default keymap 
04-01 12:41:13.800: W/IInputConnectionWrapper(2300): showStatusIcon on inactive InputConnection 

請告訴我怎麼寫的Android應用程序,即內部的數據的文件,它是應用

+0

我認爲在您提到的SD卡路徑中不存在scanned.txt文件。在寫入文件之前先創建文件,然後對其執行寫入操作。 – ManishL

回答

0

裏面你得到java.io.FileNotFoundException: /mnt/sdcard/scanned.txt (Is a directory)因爲你有不是在SD卡中的文件,但你正在寫這個文件。

你必須這樣做:

File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 
File file = new File(sdcard,"scanned.txt"); 

//Read text from file 
StringBuilder text = new StringBuilder(); 

try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
     text.append(line); 
     text.append('\n'); 
    } 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
} 

你必須在androidmanifest文件中添加此權限;

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

我已經有權限 – onkar

+0

,但你沒有在SD卡中存檔...這是你的問題..我爲你的分辨率添加權限.. –

0

請在創建之前先創建文件。

File SCANNED = new File(Environment.getExternalStorageDirectory() + File.separator + "scanned.txt"); 
SCANNED.createNewFile(); 

並賦予android清單文件的寫權限。

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