0

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.
Madiya
  • 23
  • 5
  • did u added permissions? it is not able to find path properly – KOTIOS Nov 25 '13 at 07:24
  • Permission is already added for read and write both. It also finds the sample1.txt file but instead of loopping thru thru the file and read file text, it gives some garbage. – Madiya Nov 25 '13 at 07:36
  • Pl note that the garbage starts only after code enters the while loop as seen in logcat. Is java scanner for reading text file available in android also? May be something wrong with java scanner. – Madiya Nov 25 '13 at 07:41
  • why are you terminating Readtxtfile() with semi-colon – Suhail Mehta Nov 25 '13 at 07:42
  • see this once http://zeeshanakhter.com/2013/06/04/reading-and-writing-a-file-to-sd-card-sample-program-in-android/ – KOTIOS Nov 25 '13 at 07:45
  • and this http://stackoverflow.com/questions/18012557/cant-get-files-with-externalstoragedirectory – KOTIOS Nov 25 '13 at 07:45
  • Suhail Mehta - If I do not terminate the Readtxtfile() with semi-colon, it errors out with "Syntax error, insert ";" to complete BlockStatements" – Madiya Nov 25 '13 at 07:52
  • Monica - Checked both the links but none address the issue of reading text file line by line. – Madiya Nov 25 '13 at 07:52

2 Answers2

0
you can use this to read file line by line

File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {

 Log.i(line,line);
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here
}
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
0

You are trying to access a file which is not created.
use this code.or put manually text file into /mnt/sdcard/kk

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" );  



 try {
         if (!fileexists.exists()) {
             Log.d("Ketan check", "asdasdasdasdsadsd");
        fileexists.createNewFile();
         }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
navneet sharma
  • 680
  • 4
  • 7
  • File already exists as the file exixts call returns true. If file do not exists, Readtxtfile() will not be executed at all. I have manually copied the file at that location. – Madiya Nov 25 '13 at 08:41
  • have you added these permission – navneet sharma Nov 25 '13 at 10:00
  • can you post some text of text file – navneet sharma Nov 25 '13 at 10:12
  • Text is actually tab delimited texts. How do I post it here with proper format? Sorry, I am new and this comment box takes all in one line only. – Madiya Nov 25 '13 at 10:23
  • Hi Navneet, I pulled the txt file on pc and opened but it didn't opened. So I created a new tab delimited file and pused again to emulator. This is now working fine. Thanks to point out to recheck the text file. Issue resolved. – Madiya Nov 25 '13 at 10:33