When i am trying to move file from fileDirectory1 to fileDirectory2 .. Is there any way to move or copy that file from one path to another in one FTP server.Please anyone can help me.
Here is my sample code:
String existingfile = file.getFilename();
String newfile =file.getFilename();
String fileDirectory1 = clients.getFtpFolder() + "/" + "unprocessed" + "/";
String fileDirectory2 = clients.getFtpFolder() + "/" + "processed" + "/";
sftpChannel.cd(fileDirectory1);
if (sftpChannel.get(newfile) != null){
sftpChannel.rename(fileDirectory1 + newfile ,
fileDirectory2 + newfile );
sftpChannel.cd(fileDirectory2);
sftpChannel.rm(existingfile );
}
Console:
Caused by:2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
I have tried How to move file from directory A to directory B in remote server?
Move a directory in remote server to another location in the same remote server using jsch
You can try ChannelSftp#rename:
sftpChannel.rename(oldPath, newPath);
Note that oldPath and newPath can be a relative path to your current remote working directory or a absolute remote path.
Edited:
Seems you use sftpChannel.cd(fileDirectory1) to change your working directory to fileDirectory1 and suppose newfile is inside that directory, you can have a try with something like
sftpChannel.rename(filename, destFilePath);
Note that filename is a file name and destFilePath is the remote file path of the destination. I have used it before and it works fine.
You code will be something like:
sftpChannel.rename(newfile, fileDirectory2 + newfile);
in your code. Hope this can help
Related
I am developing an android app. I want to download files from Firebase Storage to the internal storage and be able to play them afterwards (audio files). I use getFilesDir() to achieve that.
Everything works fine on the emulator. My audio directory is located at
/data/user/0/com.package.name/files/audio/
The problem occurs on some Huawei devices (phone and tablet) with Android API 28.
I get the error, that my audio folder can't be found at the location mentioned above and with the device file explorer I can clearly see that there is no data/user/ folder but instead the system still uses the old /data/data/com.package.name/ directories.
So my question is:
does someone have an idea why getFilesDir() is not giving me the right path and how I could fix this without hardcoding the path.
Here is some code, I have also tried to call exists() on the root folder, with no success.
String filename = document.getId() + ".m4a";
String rootPath = getFilesDir().getAbsolutePath();
File root = new File(rootPath);
//not working
if(!root.exists()){
rootPath = "/data/data/" + getPackageName() + File.separator + "files"
+ File.separator + "voice" + File.separator;
}
//
String folderPath = rootPath + File.separator + "voice" + File.separator;
File subFolder = new File(folderPath);
if(!subFolder.exists()){
subFolder.mkdir();
}
final File localFile = new File(subFolder, filename);
final StorageReference voiceRef = uploadsRef.child("voice").child(filename);
voiceRef.getFile(localFile);
getFilesDir() can and does return different absolute paths based on what device you're running on or even what user (for devices that support multiple users) is running your app (that's the user/0 you're seeing).
As long as you're operating on the directory returned by getFilesDir() or subdirectories of it (such as your new File(getFilesDir(), "voice") directory), everything will work on every device.
I was confused why getFilesDir() creates file in correct directory but not found them when need to delete, getFilesDir().getPath() worked for me.
The getFilesDir() method returns an abstract file path that will change based on the storage system.
You can get the /data/data/com.package.name/files pathway by using getCanonicalFile(). In Kotlin it would look something like this:
val filePath = context.filesDir.canonicalFile
This gives you the path name specific to the device.
I try to read files from the resources folder. The problem is, that the File.separator turns into a "%" on Windows.
String inputFilesFolder = "input_files" + File.separator;
File file = new File(classLoader.getResource(inputFilesFolder + "filename").getFile());
The inputFilesFolder is still fine (input_files/), but after creating the file file.getPath() becomes D:\blabla\input_files%filename.
Then I try to read the file, but I get a FileNotFoundException (big surprise).
What's wrong here?
File.separator is a file system thing. When you're using classLoader.getResource() always use forward slash as the name of a resource is a '/'-separated path name.
See Javadoc for getResource()
Try this:
File file = new File(classLoader.getResource(inputFilesFolder + filename).toURI());
How about
String inputFilesFolder = "input_files" + File.separator;
File file = new File(classLoader.getResource(inputFilesFolder + filename).toString());
First of all i request people do not consider it as a duplicate question, please look into query.
I am copying the xml files from one folder to other folder, in the source folder, i have some files which have some content like "backingFile="$IDP_ROOT/metadata/iPAU-SP-metadata.xml" but while writing to the destination folder.
i am replacing the "$IDP_ROOT" with my current working directory. The entire copying of files is for deploying into tomcat
server. The copying is done only when server starts for the first time.
Problem: If i change the folder name from my root path in my machine after i run the server,
the entire process will be stopped because the destination folder files already contains the content which is with
existed files names or folder names.
So i want to change it to relative path instead absolute path. What is the best way to do it?
Please look at code below:
// Getting the current working directory
String currentdir = new File(".").getAbsoluteFile().getParent() + File.separator;
if(currentdir.indexOf("ControlPanel")!=-1){
rootPath=currentdir.substring(0, currentdir.indexOf("ControlPanel"));
}else{
rootPath=currentdir;
}
rootPath = rootPath.replace("\\", "/");
// target file in source folder is having "backingFile="$IDP_ROOT/metadata/iPAU-SP-metadata.xml"
String content = FileReaderUtil.readFile(targetFile,
rootPath + "Idp/conf");
content = updatePath(content, Install.rootPath
+ "IdP/IdPserver/metadata","$IDP_ROOT");
FileWriterUtil.writeToFile(Install.rootPath
+ "IdP/IdPserver/idp/conf", content,
targetFile);
// update method
public String updatePath(String content, String replaceString,String replaceKey) {
replaceKey = replaceKey!=null ? replaceKey : "$IDP_SERVER_ROOT";
replaceString= replaceString.replace("\\","/");
String updateContent = content.replace(replaceKey,
replaceString);
return updateContent;
}
After reading that is it possible to create a relative filepath name using "../" I tried it out.
I have a relative path for a file set like this:
String dir = ".." + File.separator + "web" + File.separator + "main";
But when I try setting the file with the code below, I get a FileNotFoundException.
File nFile= new File(dir + File.separator + "new.txt");
Why is this?
nFile prints: "C:\dev\app\build\..\web\main"
and
("") file prints "C:\dev\app\build"
According to your outputs, after you enter build you go up 1 time with .. back to app and expect web to be there (in the same level as build). Make sure that the directory C:\dev\app\web\main exists.
You could use exists() to check whether the directory dir exist, if not create it using mkdirs()
Sample code:
File parent = new File(dir);
if(! parent.exists()) {
parents.mkdirs();
}
File nFile = new File(parent, "new.txt");
Note that it is possible that the file denoted by parent may already exist but is not a directory, in witch case it would not be possible to use it a s parent. The above code does not handle this case.
Why wont you take the Env-Varable "user.dir"?
It returns you the path, in which the application was started from.
System.getProperty(user.dir)+File.separator+"main"+File.separator+[and so on]
I want to create a folder in ex. my desktop in Mac OS X
I try to use this code, instead of Mymac is my name of course:)
String path="/Users/Mymac/Desktop";
String house = "My_home";
File file=new File(path);
if(!file.exists())
file.mkdirs(); // or file.mkdir()
file=new File(path + "/" + house);
try {
if(file.createNewFile())
{
}
} catch (IOException ex) {
ex.printStackTrace();
}
Do you know how I could create a new folder?
And another thing is when I want to create a folder in the directory where my code is, do you know how I could write that? I have tried
String path="./";
String path="::MyVolume";
String path=".";
A platform-independent way:
File rootDir = File.listRoots()[0];
File dir = new File(new File(new File(rootDir, "Users"), "Mymac"), "Desktop");
if (!dir.exists()){
dir.mkdirs();
}
Your code is ok and will work. Perhaps you have a typo in your username in your file-path ("Mymac"), so you don't see the changes, since they go to another folder.
Running this code on my machine works fine and gives the expected result.
To make your code platform-independant, you can build your file-path with the following trick:
File path = new File(File.listRoots()[0], "Users" + System.getProperty("file.separator") + "Mymac" + System.getProperty("file.separator") + "Desktop"));
If "My_home" should be a folder and not a file, you have to change the file.createNewFile() - command. More detailed information you'll find in the answer of Thomas.
To find the folder/directory where one of your classes is (assuming they are not in a Jar), and then to create a subfolder there:
String resource = MyClass.class.getName().replace(".", File.separator) + ".class";
URL fileURL = ClassLoader.getSystemClassLoader().getResource(resource);
String path = new File(fileURL.toURI()).getParent();
String mySubFolder = "subFolder";
File newDir = new File(path + File.separator + mySubFolder);
boolean success = newDir.mkdir();
(The code above could be made more compact, I listed it more verbosely to demonstrate all the steps.) Of course, you need to be concerned about permission issues. Make sure that the user which is running java has permission to create a new folder.
Use file.mkdir() or file.mkdirs() instead of file.createNewFile() and it should work, if you have permission to create new folders and files.
mkdirs() will create the subfolders if they don't exist, mkdir() won't.
To create a directory in your base directory (the one you start your application from), just provide a relative path name: new File("mydir").mkdir();
Edit: to make file handling easier, I'd suggest you also have a look at Apache Commons' FilenameUtils and FileUtils.