Android 如何使用输入流读取器从内部内存加载文件

Android 如何使用输入流读取器从内部内存加载文件,android,inputstream,Android,Inputstream,我的应用程序要求将文本保存(用户选择)到内存中,然后我想要一个类似收藏夹的功能,用户点击按钮,收藏夹活动开始加载保存在内存中的文件(内部)。在我的程序中有多个文本,我使用随机生成器将文件保存为“fav1”、“fav2”等。。其中整数部分是随机生成的。问题是,现在我不知道如何给出我的文件名,以便在文本视图中检索和显示文件 public void load(String name){ try { BufferedReader inputReader=新的BufferedReader(新

我的应用程序要求将文本保存(用户选择)到内存中,然后我想要一个类似收藏夹的功能,用户点击按钮,收藏夹活动开始加载保存在内存中的文件(内部)。在我的程序中有多个文本,我使用随机生成器将文件保存为“fav1”、“fav2”等。。其中整数部分是随机生成的。问题是,现在我不知道如何给出我的文件名,以便在文本视图中检索和显示文件

public void load(String name){


    try {
BufferedReader inputReader=新的BufferedReader(新的InputStreamReader(openFileInput(filename))

        String inputString;

        StringBuffer stringBuffer = new StringBuffer();                

        while ((inputString = inputReader.readLine()) != null) {

            stringBuffer.append(inputString + "\n");

        }

        show.setText(stringBuffer.toString());

        } catch (IOException e) {

        e.printStackTrace();

        }

那么,您建议我如何检索这些文件,这让任何人都感到沮丧。您可以通过以下代码轻松地进行检索:; 正如您所说,它从内部存储器读取文件

private String readFromFile(String fname) {

    String ret = "";

    try {
        InputStream inputStream = openFileInput(fname+".txt");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);
            }

            show.setText(stringBuilder.toString());                

            inputStream.close();

        }
    }
编辑 从包含路径分隔符的路径读取

File myFile = new File(fname+".txt"); // path contains full path to the file including file name
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(
                    new InputStreamReader(fIn));
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = myReader.readLine()) != null) {
                stringBuilder.append(receiveString);
            }
            show.setText(stringBuilder.toString());    
            myReader.close();
编辑2

列出特定目录中的文件并按其名称选择所需文件

File directory = new File(Environment.getExternalStorageDirectory()+"/sample");

 // check the existance of the parent directory
 if (directory.exists()) { 

 // get the list of files from the directory and keep it in an array of type File.
                    File[] fileList = directory.listFiles(); 

                for (File file : fileList) {

  //compares with filename: you can change this to your required file!
                        if (file.getName().equals("sam2.txt")) { 

  // method to read and show the text in text view
                            loadFile(file); 



                        }

                    }
最后是loadFile()方法的定义:


我已经在logcat中列出了所有文件名和路径,其他一切都是正确的,除了调用load()时得到一个npe。edit2部分工作得很好,我仍然面临加载文件部分NPE中的错误,关于什么?你现在面临的错误是什么?你能把logcat放在你得到NPE的地方吗?当你打印文件名和路径时,你在logcat中得到了什么?当我打印路径时,我得到的是/data/data/mypackagename/files/fave2 3,4 ie。剩下的都是一样的,只是文件名改变了,现在我没有得到NPE,但是我用日志消息注意到了一件事,这个load()没有被调用,编译器对if(file等于“myname.txt”)进行if检查,然后它只返回no,函数不被调用,我得到一个空白的textViewfile.equals(“mytext.txt”)或file.getName().equals(“mytext.txt”),你试过哪一个?我实际上并不喜欢getName,因为我不知道文件名是什么,程序会动态地将它分配给它们,这样我就可以在使用文件时尝试类似load()的操作了=null,而file是上面定义的数组u
private void loadFile(File file) {
                // TODO Auto-generated method stub
                   FileInputStream fIn;
                    try {
                        fIn = new FileInputStream(file);
                        BufferedReader myReader = new BufferedReader(
                                new InputStreamReader(fIn));
                        String receiveString = "";
                        StringBuilder stringBuilder = new StringBuilder();

                        while ((receiveString = myReader.readLine()) != null) {
                            stringBuilder.append(receiveString);
                        }
                        show.setText(stringBuilder.toString());    
                        myReader.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }