在阅读文本文件的错误在Android的SD卡 [英] Error in Read text file from sdcard in android

查看:159
本文介绍了在阅读文本文件的错误在Android的SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取SD卡一行行的文本文件。
而在Java中我的code在PC上的作品汇编独立,在这个日食code给出错误。
code保持循环直通东西是不是在所有我的文本文件。

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.layout_ardtxt);
// *********************
    Toast.makeText(getApplicationContext(),开始,Toast.LENGTH_LONG).show();
    Log.d(CHK,应用程序启动);    文件目录=新的文件(Environment.getExternalStorageDirectory()+文件分割符+KK);
    directory.mkdirs();    Log.d(mytxt应用程序应用程序,Environment.getExternalStorageDirectory()+文件分割符+KK);
     FILEEXISTS =新的文件(Environment.getExternalStorageDirectory()+文件分割符+KK+文件分割符,Sample1.txt);
       如果(fileexists.exists()){
         //做动作
           Toast.makeText(getApplicationContext(),主题文件存在,Toast.LENGTH_LONG).show();
           的System.out.println(文件存在这样可以通过我们使用);
       Log.d(科坦检查,Sample1.txt存在);
       尝试{
           Readtxtfile();
       }赶上(FileNotFoundException异常五){
            // TODO自动生成catch块
        Log.d(GG,异常:找不到文件);
           e.printStackTrace();
            Toast.makeText(getApplicationContext(),科坦找不到文件,Toast.LENGTH_LONG).show();
        }       }其他{
           Toast.makeText(getApplicationContext(),抱歉,没有文件或路径,Toast.LENGTH_LONG).show();
           Log.d(应用程序文件检查,应用程序找不到文件);
       }}公共无效Readtxtfile()抛出FileNotFoundException异常{
   Log.i(GG,子readtextfile启动);    Toast.makeText(getApplicationContext()开始读取文件,Toast.LENGTH_SHORT).show();
    FIS的FileInputStream =新的FileInputStream(FILEEXISTS);
    扫描仪扫描=新的扫描仪(FIS);
    Log.i(GG,扫描仪开始);
    的System.out.println(一行在Java中使用扫描仪读取文件行);    而(scanner.hasNextLine()){
串TT = scanner.nextLine();
Log.d(应用程序,内部扫描仪while循环);
Log.i(TT,TT);    }    scanner.close();
    };
}

我已经添加权限,读取和写入SD卡。
logcat的输出如下。

  11月11日至25日:08:46.626:E /跟踪(805):错误打开跟踪文件:没有这样的文件或目录(2)
11月11日至25日:08:47.456:D / CHK(805):应用程序启动
11月11日至25日:08:47.456:D / mytxt应用应用程序(805):到/ mnt / SD卡/ KK
11月11日至25日:08:47.525:我/的System.out(805):文件存在这样可以通过我们使用
11月11日至25日:08:47.525:D /科坦检查(805):Sample1.txt存在
11月11日至25日:08:47.536:我/我的应用程序(805):分readtextfile开始
11月11日至25日:08:47.628:我/我的应用程序(805):扫描仪开始
11月11日至25日:08:47.628:我/的System.out(805):按行用Java读取文件中的行扫描仪使用
11月11日至25日:08:47.656:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.656:我/在早期的init(805):在早期的init
11月11日至25日:08:47.722:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.722:I /导出EXTERNAL_STORAG​​E到/ mnt / SD卡(805):出口EXTERNAL_STORAG​​E到/ mnt / SD卡
11月11日至25日:08:47.726:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.726:I / MKDIR到/ mnt / SD卡0000系统系统(805)上:mkdir到/ mnt / SD卡0000系统的系统
11月11日至25日:08:47.726:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.726:I /#向后兼容(805):#向后兼容性
11月11日至25日:08:47.735:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.735:I /符号链接到/ mnt / SD卡/ SD卡(805):符号链接到/ mnt / SD卡/ SD卡
11月11日至25日:08:47.735:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.748:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.748:I /开机(805):在引导
11月11日至25日:08:47.748:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.748:I / setprop哎呀哎呀(805):setprop哎呀哎呀
11月11日至25日:08:47.760:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.760:I / setprop net.eth0.gw 10.0.2.2(805):setprop net.eth0.gw 10.0.2.2
11月11日至25日:08:47.766:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.766:I / setprop net.eth0.dns1 10.0.2.3(805):setprop net.eth0.dns1 10.0.2.3
11月11日至25日:08:47.786:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.786:I / setprop net.gprs.local-IP 10.0.2.15(805):setprop net.gprs.local-IP 10.0.2.15
11月11日至25日:08:47.896:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.906:I / setprop ro.radio.use-PPP没有(805):setprop ro.radio.use-PPP没有
11月11日至25日:08:47.906:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.906:I / setprop ro.build.product通用(805):setprop ro.build.product通用
11月11日至25日:08:47.928:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.947:I / setprop ro.product.device通用(805):setprop ro.product.device通用
11月11日至25日:08:47.947:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.976:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:47.976:I /#假的一些电池状态(805):#假冒一些电池状态
11月11日至25日:08:48.030:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:48.030:I / setprop status.battery.state慢(805):setprop status.battery.state慢
11月11日至25日:08:48.046:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:48.046:I / setprop status.battery.level 5(805):setprop status.battery.level 5
11月11日至25日:08:48.127:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:48.127:I / setprop status.battery.level_raw 50(805):setprop status.battery.level_raw 50
11月11日至25日:08:48.127:D /应用程序(805):扫描仪内循环时
11月11日至25日:08:48.127:I / setprop status.battery.level_scale 9(805):setprop status.battery.level_scale 9
.......像这样继续下去。


解决方案

 ,你可以用它来读取文件行线文件SD卡= Environment.getExternalStorageDirectory();//获取文本文件
档案文件=新的文件(SD卡,file.txt的);//从文件中读取文本
StringBuilder的文本=新的StringBuilder();尝试{
    BR的BufferedReader =新的BufferedReader(新的FileReader(文件));
    串线;    而((行= br.readLine())!= NULL){ Log.i(线,线);
    }
}
赶上(IOException异常五){
    //你需要在这里添加正确的错误处理
}

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.

解决方案

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
}

这篇关于在阅读文本文件的错误在Android的SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆