Android从SD卡上读取文件

时间:2015-08-13 14:36:32

标签: java android file

我正在尝试从我在SD卡上创建的文本文件中读取一些数据。但是,当我读完它时,stringbuilder仍然是空白的。我不知道它是否正在阅读该文件。我查看了android设备监视器,其中包含数据。它是在我的SD卡里面的一个" Notes"目录。这是代码。

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    welcomeText = (TextView) findViewById(R.id.welcomeText);
    userName = (EditText) findViewById(R.id.userName);
    submitButton = (Button) findViewById(R.id.submitButton);
    password = (EditText) findViewById(R.id.passInput);
    viewAll = (Button) findViewById(R.id.viewAll);
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            generateNoteOnSD("info", userName.getText().toString() + "," +password.getText().toString() );
        }
    });

    viewAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File sdCard = Environment.getExternalStorageDirectory();
            File file = new File(sdCard, "info");
            String line;

            StringBuilder text = new StringBuilder();

            try {
                BufferedReader br = new BufferedReader(new FileReader(file));


                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }

                br.close();
            }
            catch (IOException e) {

            }

            welcomeText.setText("Welcome, " + text);

        }
    });
}

public void generateNoteOnSD(String sFileName, String sBody){
    try
    {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

尝试改变如下

<h:selectOneRadio value="#{faPrincipiaCntrprtyList.activeFlag}">
    <f:selectItem itemLabel="Active" itemValue="1"/>
    <f:selectItem itemLabel="InActive" itemValue="0" />
    <f:selectItem itemLabel="All" itemValue="????"/> 
</h:selectOneRadio>