I am trying to read the text file from sdcard line by line. While my code in java compiled standalone on pc works, this code in eclipse gives error. Code keeps looping thru something which is not at all my text file.
@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();
} ;
}
I have already added permission for read and write sdcard. Logcat output is below.
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.