如何读取SD卡文件中的文本? [英] How read text from sdcard file?

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

问题描述

我有问题。我创建一个名为test.txt的话,我得从cat命令system文件,本文把我的test.txt文字我的code文本文件,但我不知道如何读取该文件中的文本。我需要从这个文件中读取文本,然后将其保存到我的共享preferences。
这里是code:

  {尝试
            FOS的FileOutputStream =新的FileOutputStream(/ SD卡/的test.txt);
            DataOutputStream类DOS =新的DataOutputStream类(FOS);
            dos.flush();
            dos.close();
        }赶上(FileNotFoundException异常E1){
            // TODO自动生成catch块
            e1.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }处理;
        尝试{
            A =调用Runtime.getRuntime()EXEC(苏);            DataOutputStream类AAA =新的DataOutputStream类(a.getOutputStream());
            aaa.writeBytes(猫的/ proc / SYS /伤心/ asdsad> /sdcard/test.txt\
);
            aaa.writeBytes(退出\\ n);
            aaa.flush();
            尝试{
                a.waitFor();
                如果(a.exitValue()!= 255){
                    // TODO code到成功运行
                    toastMessage(根);
                    }
                其他{
                    // TODO code在运行不成功
                   toastMessage(没有根);
                   }
            }赶上(InterruptedException的E){
                // TODO code在中断异常运行
                toastMessage(没有根);
                }
        }赶上(IOException异常五){
            // TODO code在输入/输出异常运行
            toastMessage(没有根);
            }


解决方案

您并不需要复制的文件,以便阅读SD卡。

和不管怎么说,使用猫进行复印是不是你的应用程序想要的东西。当你失去了操作所有的控制;错误检测和处理变得更加困难。

只要使用的FileReader 的BufferedReader 。一个例子可以发现这里。这里是一个副本:

 文件fil​​e =新的文件(的test.txt);
StringBuffer的内容=新的StringBuffer();
读者的BufferedReader = NULL;尝试{
    读者=新的BufferedReader(新的FileReader(文件));
    字符串文本= NULL;    //重复,直到所有行被读取
    而((文= reader.readLine())!= NULL){
        contents.append(文本)
            .append(System.getProperty(
                line.separator));
    }
}赶上(FileNotFoundException异常五){
    e.printStackTrace();
}赶上(IOException异常五){
    e.printStackTrace();
} {最后
    尝试{
        如果(读者!= NULL){
            reader.close();
        }
    }赶上(IOException异常五){
        e.printStackTrace();
    }
}Log.e(TEXT,contents.toString());

这一切都是非常基本的东西。你应该考虑读一些Java相关的书籍或几篇文章。

I have problem. I create with my code text file called test.txt then I get text from system file with cat command and this text put to my test.txt, but I don't know how to read the text from this file. I need read text from this file and then save it to my SharedPreferences. Here is code:

try {
            FileOutputStream fos = new FileOutputStream("/sdcard/test.txt");
            DataOutputStream dos = new DataOutputStream(fos);
            dos.flush();
            dos.close(); 
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Process a;
        try {
            a = Runtime.getRuntime().exec("su"); 

            DataOutputStream aaa = new DataOutputStream(a.getOutputStream());
            aaa.writeBytes("cat /proc/sys/sad/asdsad > /sdcard/test.txt\n");
            aaa.writeBytes("exit\n"); 
            aaa.flush();
            try {
                a.waitFor();
                if (a.exitValue() != 255) {
                    // TODO Code to run on success
                    toastMessage("root");
                    }
                else {
                    // TODO Code to run on unsuccessful             
                   toastMessage("not root");
                   }
            } catch (InterruptedException e) {
                // TODO Code to run in interrupted exception    
                toastMessage("not root");
                }
        } catch (IOException e) {
            // TODO Code to run in input/output exception
            toastMessage("not root");
            }

解决方案

You don't need to "copy" file to sdcard in order to read it.

And anyway, using "cat" for copying is not what you want in application. As you loose all control over the operation; error detection and handling becomes much harder.

Just use FileReader and BufferedReader. An example can be found here. Here is a copy:

File file = new File("test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;

try {
    reader = new BufferedReader(new FileReader(file));
    String text = null;

    // repeat until all lines is read
    while ((text = reader.readLine()) != null) {
        contents.append(text)
            .append(System.getProperty(
                "line.separator"));
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}           

Log.e("TEXT", contents.toString());

All this is very basic stuff. You should consider reading some Java related book or a few articles.

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

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