开发者

Trying to Open a Text File from Non Default Folder in Android

I'm saving a file to a non-default spot in Android and am trying to be able to open it to load information from it but cannot find a way to specify the path to the file...

Here is how I am saving the file...

File directory = new File(path + "/Android/data/com.etechtour/save_data/");

    directory.mkdirs();

    extStorageDirectory = path.toString() + "/Android/data/com.etechtour/save_data/";

    File file = new File(extStorageDirectory, "pumpItUpGas.txt");
    try
    {
        outStream = new FileOutputStream(file);
        OutputStreamWriter out = new OutputStreamWriter(outStream);
        out.write("Record#: " + record + ", Nickname: " + nickname + ", Year: " + year + ", Make: " + make + ", Model: " + model);
        out.flush();
        out.close();
        outStream.flush();
        outStream.close();
    }

Now I've successfully saved the file to this location, but when trying to access it I can't figure out ANYTHING that works to set the path to load the file. I've looked around everywhere online and can't seem to find anything that works except using the default save position. Here's how I'm trying to load bu开发者_StackOverflow中文版t am either forceclosing or getting null pointer exceptions

try 
{
        String filename = Environment.getExternalStorageDirectory().toString() +  "/Android/data/com.etechtour/save_data/pumpItUpGas.txt";
        InputStream in = openFileInput("pumpItUpGas.txt");
        //FileReader fileReader = new FileReader(file);
        //FileInputStream fileInput = new FileInputStream(path);
        if (in != null)
        //if (fileReader != null)
        {
            InputStreamReader temp = new InputStreamReader(in);
            //InputStreamReader tmp = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(temp);   
            StringBuffer buf = new StringBuffer();

            while ((str = reader.readLine()) != null) 
            {
                buf.append(str);
            }
            //fileInput.close();
            in.close();

            return str;

        }
    }

Any help would be greatly appreciated. Thanks


Use FileInputStream or FileReader with your File object, just like normal Java I/O.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜