我試圖從SD卡中逐行讀取文本文件。 雖然我在java編譯獨立的代碼工作,在eclipse中的這個代碼給出了錯誤。 代碼保持循環通過一些不是我的文本文件。在Android中讀取SD卡中的文本文件時出錯
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_ardtxt);
//*********************
Toast.makeText(getApplicationContext(), "start" , Toast.LENGTH_LONG).show();
Log.d("chk","app start");
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"kk");
directory.mkdirs();
Log.d("mytxt app App", Environment.getExternalStorageDirectory()+File.separator+"kk");
fileexists = new File(Environment.getExternalStorageDirectory()+File.separator+"kk"+File.separator, "Sample1.txt");
if (fileexists.exists()) {
//Do action
Toast.makeText(getApplicationContext(), "subject file exists" , Toast.LENGTH_LONG).show();
System.out.println("file exists so can be used by us");
Log.d("Ketan check", "Sample1.txt exists");
try {
Readtxtfile();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.d(gg,"Exception : file not found");
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ketan file not found" , Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), "sorry no file or path" , Toast.LENGTH_LONG).show();
Log.d("App file check", "App File not found");
}
}
public void Readtxtfile() throws FileNotFoundException {
Log.i(gg, "sub readtextfile started");
Toast.makeText(getApplicationContext(), "start reading file" , Toast.LENGTH_SHORT).show();
FileInputStream fis = new FileInputStream(fileexists);
Scanner scanner = new Scanner(fis);
Log.i(gg,"Scanner started");
System.out.println("Reading file line by line in Java using Scanner");
while(scanner.hasNextLine()){
String tt = scanner.nextLine();
Log.d("app", "inside scanner while loop");
Log.i(tt,tt);
}
scanner.close();
} ;
}
我已經添加了讀寫SD卡的權限。 Logcat輸出如下。
11-25 11:08:46.626: E/Trace(805): error opening trace file: No such file or directory (2)
11-25 11:08:47.456: D/chk(805): app start
11-25 11:08:47.456: D/mytxt app App(805): /mnt/sdcard/kk
11-25 11:08:47.525: I/System.out(805): file exists so can be used by us
11-25 11:08:47.525: D/Ketan check(805): Sample1.txt exists
11-25 11:08:47.536: I/my app(805): sub readtextfile started
11-25 11:08:47.628: I/my app(805): Scanner started
11-25 11:08:47.628: I/System.out(805): Reading file line by line in Java using Scanner
11-25 11:08:47.656: D/app(805): inside scanner while loop
11-25 11:08:47.656: I/on early-init(805): on early-init
11-25 11:08:47.722: D/app(805): inside scanner while loop
11-25 11:08:47.722: I/export EXTERNAL_STORAGE /mnt/sdcard(805): export EXTERNAL_STORAGE /mnt/sdcard
11-25 11:08:47.726: D/app(805): inside scanner while loop
11-25 11:08:47.726: I/mkdir /mnt/sdcard 0000 system system(805): mkdir /mnt/sdcard 0000 system system
11-25 11:08:47.726: D/app(805): inside scanner while loop
11-25 11:08:47.726: I/# for backwards compatibility(805): # for backwards compatibility
11-25 11:08:47.735: D/app(805): inside scanner while loop
11-25 11:08:47.735: I/symlink /mnt/sdcard /sdcard(805): symlink /mnt/sdcard /sdcard
11-25 11:08:47.735: D/app(805): inside scanner while loop
11-25 11:08:47.748: D/app(805): inside scanner while loop
11-25 11:08:47.748: I/on boot(805): on boot
11-25 11:08:47.748: D/app(805): inside scanner while loop
11-25 11:08:47.748: I/setprop ARGH ARGH(805): setprop ARGH ARGH
11-25 11:08:47.760: D/app(805): inside scanner while loop
11-25 11:08:47.760: I/setprop net.eth0.gw 10.0.2.2(805): setprop net.eth0.gw 10.0.2.2
11-25 11:08:47.766: D/app(805): inside scanner while loop
11-25 11:08:47.766: I/setprop net.eth0.dns1 10.0.2.3(805): setprop net.eth0.dns1 10.0.2.3
11-25 11:08:47.786: D/app(805): inside scanner while loop
11-25 11:08:47.786: I/setprop net.gprs.local-ip 10.0.2.15(805): setprop net.gprs.local-ip 10.0.2.15
11-25 11:08:47.896: D/app(805): inside scanner while loop
11-25 11:08:47.906: I/setprop ro.radio.use-ppp no(805): setprop ro.radio.use-ppp no
11-25 11:08:47.906: D/app(805): inside scanner while loop
11-25 11:08:47.906: I/setprop ro.build.product generic(805): setprop ro.build.product generic
11-25 11:08:47.928: D/app(805): inside scanner while loop
11-25 11:08:47.947: I/setprop ro.product.device generic(805): setprop ro.product.device generic
11-25 11:08:47.947: D/app(805): inside scanner while loop
11-25 11:08:47.976: D/app(805): inside scanner while loop
11-25 11:08:47.976: I/# fake some battery state(805): # fake some battery state
11-25 11:08:48.030: D/app(805): inside scanner while loop
11-25 11:08:48.030: I/setprop status.battery.state Slow(805): setprop status.battery.state Slow
11-25 11:08:48.046: D/app(805): inside scanner while loop
11-25 11:08:48.046: I/setprop status.battery.level 5(805): setprop status.battery.level 5
11-25 11:08:48.127: D/app(805): inside scanner while loop
11-25 11:08:48.127: I/setprop status.battery.level_raw 50(805): setprop status.battery.level_raw 50
11-25 11:08:48.127: D/app(805): inside scanner while loop
11-25 11:08:48.127: I/setprop status.battery.level_scale 9(805): setprop status.battery.level_scale 9
....... and like this goes on.
沒有ü添加的權限?它無法正確找到路徑 – KOTIOS
已經爲讀取和寫入兩者添加了權限。它也可以找到sample1.txt文件,但不是通過文件循環來讀取文件文本,而是給出一些垃圾。 – Madiya
注意,垃圾只有在代碼進入while循環後纔會啓動,如logcat所示。是用於閱讀Android中的文本文件的java掃描儀嗎?可能是java掃描儀錯了。 – Madiya