1

我有一个文件

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”只在 sdcard 中(应该在 Myfolder 中)。

并且文件的名称是 MyfolderMEMS 而不是 MEMS。

我想不出我的错误。

4

3 回答 3

1

改变

 OutputStream myOutput = new FileOutputStream(directory.getPath()+
                         filename);

File file = new File(directory, filename); 
OutputStream myOutput = new FileOutputStream(file);
于 2013-06-07T08:52:38.377 回答
1

我希望问题是缺少文件分隔符,试试这个,

OutputStream myOutput = new FileOutputStream(directory.getPath()+File.separator+
                         filename);

或者试试这个,

OutputStream myOutput = new FileOutputStream(directory.getPath()+"/"+
                         filename);
于 2013-06-07T08:56:49.483 回答
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);
于 2013-06-07T09:02:54.953 回答