Java is putting my file path twice - java

Hi i have made a 2 files that is suppose to copy file, but it makes the file url twice e.g. Users/Name/Users/Name/Desktop/jar.jar
Its adding the location of the runnable jar i opened to start then my path i want.
Code:
String path1 = System.getProperty("user.dir") + File.separator + "Desktop" + File.separator + "Coding" + File.separator + "Temp";
File file = new File(path1);
String path2 = System.getProperty("user.dir") + File.separator + "Library" + File.separator + "LaunchAgents" + File.separator + "program.jar";
File file2 = new File(path2);
if(file2.exists()) {
logger.warning("File 3 def");
return;
}
File file4 = new File(file.getAbsolutePath() + File.separator + "copied.jar");
if(!file4.exists()) {
logger.warning("cp " + file4.getAbsolutePath() + " : " + file2.getAbsolutePath());
logger.warning("File 4 def");
return;
}
Log:
WARNING: cp /Users/myuser/Desktop/Coding/Temp/Desktop/Coding/Temp/program.jar : /Users/myuser/Desktop/Coding/Temp/Library/LaunchAgents/copied.jar
WARNING: File 4 def

System.getProperty("user.dir") gets the current working directory. See documentation.
Perhaps you meant System.getProperty("user.home"), which gets your home directory.

Related

mobile-ffmpeg: No such file or directory

I'm using mobile-ffmpeg to convert a .mp4 file to .mjpeg file so that it can be processed by opencv, the problem is it cannot find the file
this is my code:
File video = new File(GlobalValue.uri.getPath());
File dfanFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"Dfan");
if(!dfanFolder.exists()){
dfanFolder.mkdir();
}
String videoPath = video.getAbsolutePath();
String mjpegPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"Dfan"+File.separator+"text.mjpeg";
int rc = FFmpeg.execute("-i '" + videoPath + "' -vcodec mjpeg '" +mjpegPath+"'");
VideoCapture videoCapture = new VideoCapture();
videoCapture.open(mjpegPath);
and the error is
E/mobile-ffmpeg: /raw/storage/emulated/0/DCIM/Camera/VID_20210715_102314.mp4: No such file or directory

how to include path in string array with the file in android

am working with Bitmap.decodeFile(pathname,bOptions), I wanted to include the file detected by the phone in the mean time am using this one
String path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Temp Images";
this is only single one of string and I can't include in my array, I want to do is to pass the parameter to my method which is accepting String[] files which includes the pathFile + filename
ex: sd0/pictures/temp file/img1.jpeg
Supossing you have a variable where the file name is stored:
String path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Temp Images" + File.separator + fileName;
public String getPath(String folderName, String fileName){
return Environment.getExternalStorageDirectory().toString() + folderName + File.separator + fileName;
}
using the method:
String path = getPath("/Pictures/Temp Images", "img1.jpeg");
or
String path = getPath("/Pictures/Temp Images", fileName);

mkdir() & mkdirs() returns false

I'm creating and deleting same folder continuously as a requirement. mkdir() creating some times correctly but some times fails and mkdir() returns false.
I have searched and i got solution like change directory name before deleting. But I'm not deleting directory through android code. Deletion is done by windows side.
So, please any help.
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "eTestifyData" + File.separator + orgId + File.separator +
providerId + File.separator + datewise + File.separator + encounterId);
if (file.exists()) {
write(file, file.getAbsolutePath(), jsonData);
} else {
if (file.mkdirs()) {
write(file, file.getAbsolutePath(), jsonData);
}
}
From the documentation https://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs()
Returns: true if and only if the directory was created, along with all
necessary parent directories; false otherwise
So, if the directory already exists it returns false.

Rename the file while preserving file extension in java

How to rename a file by preserving file extension?
In my case I want to rename a file while uploading it. I am using Apache commons fileupload library.
Below is my code snippet.
File uploadedFile = new File(path + "/" + fileName);
item.write(uploadedFile);
//renaming uploaded file with unique value.
String id = UUID.randomUUID().toString();
File newName = new File(path + "/" + id);
if(uploadedFile.renameTo(newName)) {
} else {
System.out.println("Error");
}
The above code is changing the file extension too. How can I preserve it?
Is there any good way with apache commons file upload library?
Try to split and take only the extension's split:
String[] fileNameSplits = fileName.split("\\.");
// extension is assumed to be the last part
int extensionIndex = fileNameSplits.length - 1;
// add extension to id
File newName = new File(path + "/" + id + "." + fileNameSplits[extensionIndex]);
An example:
public static void main(String[] args){
String fileName = "filename.extension";
System.out.println("Old: " + fileName);
String id = "thisIsAnID";
String[] fileNameSplits = fileName.split("\\.");
// extension is assumed to be the last part
int extensionIndex = fileNameSplits.length - 1;
// add extension to id
System.out.println("New: " + id + "." + fileNameSplits[extensionIndex]);
}
BONUS - CLICK ME

Generate filename for a copied file

I am looking to get similar behaviour to what you get in Windows when you copy and paste a file in the same directory.
For e.g, if you've copy/paste a file called foo.txt, it will create foo Copy.txt and if you paste it once more, it creates foo Copy(2).txt and if you copy/paste foo Copy.txt, foo Copy Copy.txt is created.
Is there a Java utility function that does this? I've looked at File.createTempFile but the filename it generates is too long and contains a UID-like substring.
By using the FileChooser in combination with the "showSaveDialog"-method you will get the result you want, because java is then using the OS behaviour for existing files.
Sometimes, you just have to do the work first, it will give you an appreciation for the API. Then you can write your own utility methods
File original = new File("build.xml");
String path = original.getAbsoluteFile().getParent();
String name = original.getName();
String ext = name.substring(name.indexOf("."));
name = name.substring(0, name.indexOf("."));
name = path + File.separator + name;
int index = 1;
File copy = new File(name + " (" + index + ")" + ext);
while (copy.exists()) {
index++;
copy = new File(name + " (" + index + ")" + ext);
}
System.out.println(copy);

Categories

Resources