从android中的sdcard读取文本文件时出错

时间:2013-11-25 07:24:04

标签: java android samsung-mobile

我正在尝试逐行读取sdcard中的文本文件。 虽然我在java上独立编译的代码可以在eclipse上运行,但是eclipse中的这段代码会给出错误。 代码通过不完整的文本文件来循环。

@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.

2 个答案:

答案 0 :(得分:1)

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
}

答案 1 :(得分:0)

您正在尝试访问未创建的文件 使用此代码。或手动将文本文件放入/ 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();
    }