繁体   English   中英

如何在Android Assets中读取文件

[英]How to read files in Android Assets

我对Android有问题。 我想打开文件,检查它,然后返回一个简单的值。

public void setMessageCode(String code){
        try {
            messageCode = setMsg(code);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String setMsg(String messageCode) throws IOException{
        String FILE = "src/dialogs.txt", msg;
        BufferedReader in = new BufferedReader(new FileReader(FILE)); //IO Exception here
        msg = in.readLine();
        while(msg!=null){
            message = msg.split(",");
            for(int i=0; i<message.length; i++)
                if(message[i].equals(messageCode)){
                    in.close();
                    return message[i++];
                }
            msg = in.readLine();
        }
        in.close();
        return "Nothing to get";
    }

因此Eclipse看不到问题,无法完美工作。 ADT失败。 Dialogs.txt位于src / ...中,我检查了这百万次。 该怎么办?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            String first_line = read_file("dialogs.txt");
            System.out.print(first_line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public String read_file(String filename) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                getAssets().open(filename)));

        int i=0;
        String line = reader.readLine();
        while (line != null) {
            if(++i==1)
                return line;//only read the first line
            line = reader.readLine();
        }

        reader.close();
        return "Nothing to get";
    }

}

您可以在运行项目时打开“文件系统”窗体“窗口”>“视图”,然后导航到安装路径以查看是否有文件。

如果您想从中读取文件而无需更改文件,请使用资产。 您想要将其写入文件,然后需要将其保存在设备上,这还要求您具有在SD卡手机上读写的适当权限。

使用文件之前,还要检查文件是否存在于设备中。

好的。 我解决了这个问题。 我使用getAssets(“ fileName”)通过活动传递了文件的本地化信息,然后在InputStreamReader的类中使用了它。 现在看起来像这样。

public void setMessageCode(InputStream location, String code){
    try {
        messagee = setMsg(location, code);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public String setMsg(InputStream location, String messageCode) throws IOException{
    String FILE = "assets/a.txt", msg;
    BufferedReader in = new BufferedReader(new InputStreamReader(location));
    msg = in.readLine();
    while(msg!=null){
        message = msg.split(",");
        for(int i=0; i<message.length; i++)
            if(message[i].equals(messageCode)){
                in.close();
                return message[i+1];
            }
        msg = in.readLine();
    }
    in.close();
    return "Nothing to get";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM