我研究了下面的链接,但它没有回答我的问题。 http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
如果标题中问题的答案是肯定的。有人可以提供一个简单的示例来创建子文件夹并将文件添加到该文件夹吗?也许展示如何从子文件夹中读取文件?
或者也许告诉我为什么下面的例子惨败
更改file.mkdirs() 后现在可以使用;到file.getParentFile().mkdirs();
以下答案中的Se解释
public static void Test(String path, String fileName, String fileStr, Context ctx)
{
SaveFile(path, fileName, fileStr, ctx);
String returnFileStr = ReadFile(path, fileName, ctx);
}
public static Boolean pathExists(String path, Context ctx)
{
Boolean result = false;
String[] pathSeqments = path.split("/");
String pathStr = "";
for(int i = 0;i<pathSeqments.length;i++ )
{
pathStr += pathSeqments[i];
if(!new File(ctx.getFilesDir() +"/" + pathStr).exists())
{
result = false;
break;
}
pathStr += "/";
result = true;
}
return result;
}
public static void SaveFile(String path, String fileName, String fileStr, Context ctx) {
try {
File file = new File(ctx.getFilesDir() +"/" + path, fileName); //new File(ctx.getFilesDir() +"/" + path + "/" + fileName);
file.getParentFile().mkdirs();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(fileStr);
osw.flush();
osw.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static String ReadFile(String path, String fileName, Context ctx) {
String fileStr = null;
try {
if(pathExists(path, ctx))
{
File file = new File(ctx.getFilesDir() +"/" + path, fileName);
FileInputStream fIn = new FileInputStream(file);
StringWriter writer = new StringWriter();
IOUtils.copy(fIn, writer, "UTF-8");
fileStr = writer.toString();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return fileStr;
}