无法将数据写入文件 [英] Unable to write data into a file
本文介绍了无法将数据写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图到Android中文件写入数据,但我不能这样做。请检查我的code和引导我的是一样的。
I am trying to write data on to a file in android, but I am unable to do so. Please check my code and guide me for the same.
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(是一个目录)
因为你的SD卡没有这个文件,但你特林写文件。
you are getting java.io.FileNotFoundException: /mnt/sdcard/scanned.txt (Is a directory)
because you have not that file in sdcard but you are tring to write that file.
您必须这样做:
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文件中添加此权限;
you have to add this permission in androidmanifest file;
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这篇关于无法将数据写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文