我有一个文件
String filename="MEMS.backup";
我用它来保存:
InputStream myInput;
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "Myfolder");
try {
myInput = new FileInputStream(MyApplication.getAppContext().getFilesDir().getParentFile().getPath()+"/databases/MEMS");
// Create the folder if it doesn't exist:
if (!directory.exists())
{
directory.mkdirs();
}
OutputStream myOutput = new FileOutputStream(directory.getPath()+
filename);
这将加载:
OutputStream myOutput;
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "Myfolder");
try {
myOutput= new FileOutputStream(MyApplication.getAppContext().getFilesDir().getParentFile().getPath()+"/databases/MEMS");
InputStream myInputs = new FileInputStream(directory.getPath()+filename);
问题是它在sdcard中创建了“Myfolder”,但它是空的。
文件“MEMS”只是在SD卡中(应该在Myfolder中)。
该文件的名称是MyfolderMEMS而不是MEMS。
我无法弄清楚我的错误。
答案 0 :(得分:1)
更改
OutputStream myOutput = new FileOutputStream(directory.getPath()+
filename);
与
File file = new File(directory, filename);
OutputStream myOutput = new FileOutputStream(file);
答案 1 :(得分:1)
我希望问题是文件分隔符丢失,试试这个,
OutputStream myOutput = new FileOutputStream(directory.getPath()+File.separator+
filename);
或试试这个,
OutputStream myOutput = new FileOutputStream(directory.getPath()+"/"+
filename);
答案 2 :(得分:1)
请参阅以下代码,在myfolder中创建新文件
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "Myfolder");
if( !directory.exists() )
directory.mkdirs();
File f = new File( directory, "my_file.jpg" );
OutputStream myOutput = new FileOutputStream(f);