【问题标题】:android text file import where do i save text file what folder?android文本文件导入我在哪里保存文本文件什么文件夹?
【发布时间】:2012-03-29 09:45:23
【问题描述】:

所以这是一个非常尴尬的问题,但是我有一个文本文件,java 会读取其中的所有单词并将其添加到数组中,我不知道将文本文件放在哪里,比如什么文件夹所以comp可以去得到它吗?有人可以告诉我。我的代码在常规的 java 应用程序中工作,所以它应该在 android 上工作。

【问题讨论】:

    标签: android save directory text-files


    【解决方案1】:

    你可以使用

    <your-context>.getAssets();
    

    返回一个 AssetsManager 对象。

    AssetsManager assets = context.getAssets();
    

    然后您可以使用 open() 方法打开一个输入流。

    InputStream inputStream = assets.open("filename");
    

    InputStream 对象是来自 IO 包的标准 Java 对象。您可以使用所需的对象装饰器(Reader、BufferedReader 等)来装饰此流。

    如果您希望将此文件从 APK(未膨胀)移到手机中,您只需使用输出流从输入流中复制文件的字节即可。请注意,您必须在您的写入目录中拥有权限(如果您的手机已获得 root 权限并且您已经创建了一个 shell 接口来通过 JNI 运行本机 shell 命令,则可以这样做)。

    更新

    try {
        InputStream inputStream = this.getAssets().open("test.txt");
        BufferedReader buffer = new BufferedReader(new Reader(inputStream));
    
        String line;
        while((line = buffer.readLine()) != null) {
            tots.add(line);
        }
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    

    尚未测试,但我认为这是您想要的。

    【讨论】:

    • 你能解释一下文件名是test.txt
    • 您需要一个上下文对象来从中提取资产,IE getAssets() 方法是一个上下文方法。因此,如果您在 Activity 中或从 ContextWrapper 继承的任何内容中,您只需说“this.getAssets()”。否则,您需要将上下文引用传递给试图获取资产管理器的函数/方法。
    • try { FileInputStream textfl = (FileInputStream) getAssets().open("test.txt"); DataInputStream is = new DataInputStream(textfl); BufferedReader r = new BufferedReader(new InputStreamReader(is));字符串strLine; while ((strLine = r.readLine()) != null) { tots.add(strLine); //tots 是数组列表 } } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace();我现在有它,但它不起作用....你知道一个快速解决方案吗?
    • 尝试使用 InputStream 而不是 FileInputStream。删除 DataInputStream,因为您没有在其装饰中使用任何方法,而是使用 Reader。
    【解决方案2】:

    您可以将文件放到 assets 文件夹并使用

    InputStream stream = getAssets().open(filename); 
    

    获取输入流

    【讨论】:

    【解决方案3】:

    我在 res 文件夹中创建了新的 raw 文件夹并将 chapter0.txt 放在这里。

    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.induction);
    
        wordss = new Vector<String>();
    
        TextViewEx helloTxt = (TextViewEx) findViewById(R.id.test);
        helloTxt.setText(readTxt());
    }
    
    private String readTxt() {
    
        InputStream inputStream = getResources().openRawResource(R.raw.chapter0);
        // getResources().openRawResource(R.raw.internals);
        System.out.println(inputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    
        int i;
        try {
            i = inputStream.read();
            while (i != -1) {
                byteArrayOutputStream.write(i);
                i = inputStream.read();
            }
            inputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return byteArrayOutputStream.toString();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 2023-02-25
      相关资源
      最近更新 更多