簡體   English   中英

在Android中從SD卡讀取文本文件時出錯

[英]Error in Read text file from sdcard in android

我正在嘗試從sdcard逐行讀取文本文件。 雖然我在PC上獨立編譯的java中的代碼可以工作,但是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.
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
}

您正在嘗試訪問未創建的文件。
使用此代碼。或將文本文件手動放入/ 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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM