I'm working on a new program that opens different files. The thing is when I change the computer and the files need to be opened they don't because the computer can't find those files. Is there a way to make the files reachable even on another computer? I mean the files are in a folder inside the project but the path changes when you open it on a new computer.
File myFile = new File("C:\\Users\\Usuario\\Documents\\NetBeansProjects\\Info12\\src\\info12\\Dokumente\\Aufgaben\\Bubblesort.docx");
In a Maven Project, all yours files are in src/main/resources so:
src/main/resources/info12/Dokumente/Aufgaben/Bubblesort.docx
Your java code is:
File myFile = new File("src\\main\\resources\\info12\\Dokumente\\Aufgaben\\Bubblesort.docx")
and more smart:
File myFile = new File("src" + File.separator + "main" + File.separator + "resources" + File.separator + "info12" + File.separator + "Dokumente" + File.separator + "Aufgaben" + File.separator + "Bubblesort.docx")
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.
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
java.io.FileNotFoundException: C:\Emails\ToSend\. (Access is denied)
I'm trying to download a file from FTP and save it to a folder to be processed, But when I setup the OutputStream it throws this error. Here is the code:
File downloadFile1 = new File("C:" + File.separator + "Emails"
+ File.separator + "ToSend" + File.separator
+ f.getName());
OutputStream outputStream1 = new BufferedOutputStream(
new FileOutputStream(downloadFile1));
The FTPFile f is fetched by the FTPClient from the FTP server. I've got full control of the folder Emails and all of its sub folders and I have given these same permissions to all application packages.
I'm sure its just because I'm abit out of my depth when it comes to file permissions.
Any and all help appreciated
Instead of hard-coding the file location, how about using a temporary directory? This is assuming the file will be processed immediately by your application and does not need to be kept.
For how to do this, see the accepted answer to this question: How to create a temporary directory/folder in Java?
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());
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.