java 如果目录中存在文件,我如何覆盖它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39198789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:10:58  来源:igfitidea点击:

how can i overwrite a file if it exists in directory

javaandroid

提问by remy boys

i'm saving image in my directory with same name (for a purpose) but when file is already exists there it won't get overwritten , how do i know that ? because when i'm saving this file the already existing file is not changing but when i use a different name for my file it works. so what i want is to replace the existing file with a new one. so far this is what i'm trying :

我正在以相同的名称(出于某种目的)将图像保存在我的目录中,但是当文件已经存在时,它不会被覆盖,我怎么知道?因为当我保存这个文件时,已经存在的文件没有改变,但是当我为我的文件使用不同的名称时,它可以工作。所以我想要的是用新文件替换现有文件。到目前为止,这就是我正在尝试的:

 content.setDrawingCacheEnabled(true);
                Bitmap bitmap = content.getDrawingCache(); // an bitmap file for saving


                File file = new File(context.getApplicationContext().getFilesDir() + "/img.jpg"); //here's the path where i'm saving my file

                if (file.exists()){

                    file.delete(); // here i'm checking if file exists and if yes then i'm deleting it but its not working 
                }


                String path = file.getPath();

                try {
                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 60, ostream);


                    savedFile = new File(path);


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

                }

i'm doing another check right after deleting existing file :

删除现有文件后,我正在做另一项检查:

                if (file.exists()){

                    Toast.makeText(context , "Still EXISTS",Toast.LENGTH_SHORT).show();
                }

and this time file is not here cause the toast is not appearing.

而这次文件不在这里,因为 toast 没有出现。

回答by sai

Add this line to Your Code

将此行添加到您的代码

FileWriter fw = new FileWriter(myDir + "/score.jpg", false);

回答by Jame

First, Let insert the permission in your AndroidManifest.xml file (less than Android 6.0 version)

首先,让我们在你的 AndroidManifest.xml 文件中插入权限(低于 Android 6.0 版本)

android:name="android.permission.WRITE_INTERNAL_STORAGE" />

Then, use the below code to write the bitmap to file

然后,使用下面的代码将位图写入文件

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "FitnessGirl"+".jpg"); // the File to save to
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = content.getDrawingCache();// Your bitmap
//Bitmap pictureBitmap = getImageBitmap(myurl); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close(); // do not forget to close the stream

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

回答by Prokash Sarkar

Try this,

试试这个,

boolean result = Files.deleteIfExists(file.toPath()); 

Also make sure you have write permission enabled!

还要确保您已启用写入权限!

PS:From Android Lollipop you need the runtime permissionand the permission already set in manifest file won't work.

PS:在 Android Lollipop 中,您需要运行时权限,并且清单文件中已设置的权限将不起作用。