Why Can't I Delete File From Path? - java

The code below is not deleting the file using API 33, but it's saying the file does exist though. I am not sure why the file is not deleting from external storage path?
Here is the code that I have...
File file = new File (Environment.getExternalStorageDirectory().toString() + "/Pictures/IMG_20230211_060830.jpg");
if (file.exists()) {
file.delete();
}
The file did not delete from file path, but it seems to work fine with API 31.

Related

How to open an file that was downloaded with the android download manager

I use the Android download manager to download a json file. I set the location of the downloaded file with the following code:
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS+ File.separator , "myJsonFile.json");
It is downloaded. So far, so good. Now I want to read the downloaded file.
Firss I want to check if the file exists.
File f = new File(Environment.DIRECTORY_DOWNLOADS+ File.separator , "myJsonFile.json");
The check:
f.exists()
returns false. How can I access (and finaly read) the downloaded file?

Read file from root folder on Android

My Android application needs to read a CSV file which is copied to the root directory on the device (device is NOT rooted so I guess it's not the real root directory, just the directory you see when opening the device in Windows explorer).
I wonder if this is possible?
When I do this:
File file = new File("/data.csv");
if (file.exists()) {
System.out.println("File exists!");
} else {
System.out.println("File does NOT exist");
}
I get: "File does NOT exist"
Try this:File file = new File(Environment.getExternalStorageDirectory()+ "/data.csv");Make sure you include the proper READ permissions(depending on the API level you are targeting) in your AndroidManifest.xml

Storing files in project directory using Spring

I am trying to work through Spring tutorials on file uploads. What I'm trying to do is have the file be saved to a folder within the project. The folder is called "files" and is separate from the src folder.
|bin
|build
|src
|files
I have this code which accepts a file upload:
public #ResponseBody String handleFileUpload(#RequestParam("name") String name,
#RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
//file.transferTo(); help?
return "You successfully uploaded " + name + "!";
What I want to do is use transferTo()to move the file to the "files" directory. When I try the true path, or try some sort of relative path I get this error which is created in the web window I am uploading files in.
Failed to upload file => "uploadedFileName/directory" does not exist
I am not sure why the file name is being appended to the directory path. Any assistance on this is much appreciated.
When you are trying to save the uploaded file to the file "uploadedFileName/directory", you are using relative file path. It's relative to the current working directory of the java process (java process, running your Tomcat Application Server or whatever appserver you are using). And that current working directory is not your project root. The following code:
System.out.println(new File(name).getAbsolutePath())
will print you the exactly path where you are trying to save your uploaded file.
To fix that issue you have to explicitly specify your project root:
File rootDir = new File("C:/Projects/myTestProject");
File uploadedFile = new File(rootDir, name);
file.transferTo(uploadedFile);
In real project you will want not to hardcode that rootDir path, but to retrieve it from some configuration file.
Don't rely to the current working directory of the application server. It could point to any directory.
PS that's out of scope of this question, but please be careful with saving data to the user-provided file names. Malicious user could post file with name "../../../../../../../../SomeSensitiveDirectory/SomeFile" and will overwrite that file unless you explicitly check that input parameter name for bad characters.
When you upload a file, you have to save it to another file.
You should use File#createTempFile() which takes a directory instead.
<p style="border-style:solid; border-color:#FFFFFF;">
<br>
File file = File.createTempFile("upload-", ".bin", new File("/path/to/your/uploads"));
<br><br>
item.write(file);
<br><br></p>

error while opening pdf files from jar folder

I am making a Software in whihch I have to display the pdf files. I have stored the pdf files in my project folder. The software runs perfectly fine.
But when I cleand and build the project and then i run my jar exe file the pdf files doesnt open.
After some experiments I included my pdf files in SRC folder and then clean and build the project. The difference i found is that now the jar file is bigger in size ( it equals to sum of all the pdf files) , I thought that this time it would work . But it didnt work.
Then After more experiments I included all the files in the Dist folder.
Then The jar files can open the pdf files :) , I was happy but not satisfied, Since I only have to create a jar file and seeing all the pdf files in the project folder with one jar files looks really awkard and senseless, is thier anyway i can open the pdf files using only the jar file without copying the pdf files in the folder where my jar file is stored, . This is the code i used to open a file named "aleemullah resume".
try{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "aleemullah resume.pdf");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error");
}
It looks as if you are reading the PDF file from your current working directory (that is, the folder that your program is launched from). By that logic, you should be able to open a PDF stored anywhere by entering it's full path, rather than just the file name. For instance:
String filePath = "C:\Users\you\Desktop\aleemullah resume.pdf"
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + filePath);
Change the filePath variable to wherever you are storing the PDF file.
You might also consider using a JFileChooser to select the file if you want the user to choose the PDF during runtime. Check out this example of how to open a dialog box from Java to pick a PDF file and retrieve its path.

Jsch renaming file fails

I am uploading a large file by SFTP over Jsch. During the upload process, the old file should be available, so I'm uploading to a temp file and rename it to the new file.
final String tmpName = dest + "_tmp";
channel.put(source, tmpName);
channel.rename(tmpName, dest);
The upload is ok but the renaming fails:
ERROR: Failed to upload files
4: Failure
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2491)
at com.jcraft.jsch.ChannelSftp.rename(ChannelSftp.java:1665)
...
I can't figure out where the problem is. Please help
The target file already exists. Try deleting the existing file before renaming.
I have tried rename and it worked fine for me. there was another file with same and i tried to rename new file to existing one. and it worked.
so no need to check file exist or not if you want to overwrite.

Categories