无法在“Lenovo IdeaTab”中创建用于写入的文件

时间:2015-01-16 05:09:33

标签: android file fwrite

该应用程序是每秒在文件上写入一些数据。这适用于其他标签&手机,但在" 联想IdeaTab" (4.1)能够创建目录但不能创建要写入的文件。

下面是代码,我用来写文件:

public void writeDataInFile(String dataString)
{       File logFile = null;
try {

    File folder = new File(Environment.getExternalStorageDirectory() + "/file_name_xyz");
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
    if (success) {
        // Do something on success
        String file_path = folder+"/"+currentFileName+".txt";

        logFile = new File(file_path);
        if (!logFile.exists())
        {
            try
            {
                logFile.createNewFile();

                //BufferedWriter for performance, true to set append to file flag
                BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
                buf.append("\nxyz: \n");
                buf.newLine();
                buf.close();

            } 
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {
        // Do something else on failure 
        Toast.makeText(getApplicationContext(), "Failed to write", Toast.LENGTH_SHORT).show();
    }


    try
    {
        //BufferedWriter for performance, true to set append to file flag
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
        buf.append(dataString);
        buf.newLine();
        buf.close();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

} catch (Exception e) {
    e.printStackTrace();
}

//Write internal storage -- data/data/xyzzz
try {
    FileOutputStream fos = openFileOutput(currentFileName, Context.MODE_APPEND);
    fos.write(dataString.getBytes());
    fos.close();

} catch (Exception e) {
    Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}
}

1 个答案:

答案 0 :(得分:0)

验证平板电脑上安装了哪个版本的Android,然后在该Android版本的Android文档中查找文件IO API。我非常确定他们在访问API的某些更高版本中的文件时会改变一些内容。