我正在尝试将数据写入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 :(得分: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" />
答案 1 :(得分:0)
请在写作之前先创建文件。
File SCANNED = new File(Environment.getExternalStorageDirectory() + File.separator + "scanned.txt");
SCANNED.createNewFile();
并在android清单文件中提供写入权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />