簡體   English   中英

無法將數據寫入文件

[英]Unable to write data into a file

我正在嘗試將數據寫入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應用程序內寫入數據,即在應用程序內的文件中

您正在獲取java.io.FileNotFoundException: /mnt/sdcard/scanned.txt (Is a directory)因為您在sdcard中沒有該文件,但是您正在嘗試編寫該文件。

您必須這樣做:

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" />

請先創建文件,然后再寫入。

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

並在android清單文件中提供寫權限。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM