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 应用程序中写入数据,即在应用程序内部的文件中

4

2 回答 2

0

你得到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" />
于 2013-04-01T07:27:08.913 回答
0

写入前请先创建文件。

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

并在 android manifest 文件中授予写权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-04-01T07:29:24.830 回答