I created an empty File and store the extracted value/content from a jar. The jar is running on linux.
String filename ="base_script";
File targetFile = new File( filename + ".sh");
String pathStr=null;
//empty file
targetFile.createNewFile();
if(targetFile.exists()) {
InputStream link = (getClass().getResourceAsStream(this.userScriptPath));
Files.copy(link,
targetFile.getAbsoluteFile().toPath(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
pathStr = targetFile.getAbsolutePath();
}
This is the file path ./base_script.sh
And this is file absolute path apps/MyApps/./base_script.sh
My question is why there's an extra ./ on the absolute path?
It's not clear why you have "./" in the name as you define the value without it. Anyway this will resolve the path to the real name without any "./" or ".." in a path:
pathStr = targetFile.toPath().toRealPath().toString()
Related
I'm currently reading a txt file in Java, which is located in a package with the scanner object.
To receive the file location I use a quick and dirty method:
File currentDirectory = new File(new File(".").getAbsolutePath());
String location = currentDirectory.getAbsolutePath().replace(".", "")+"\\corefiles\\src\\filereadingexample\\";
Is there a better way of doing so?
I'd love to improve my code.
Greetings
J
Use a URL
URL resource = getClass().getResource("/path/to/text/file.txt");
As the path, since your text file is inside a package, use the package structure. For example, assume your file(myfile.txt) is inside
com.myproject.files
package, your path should be
"/com/myproject/files/myfile.txt"
(mind the leading slash. It's necessary)
Now you can create a File using the URL
new File(resource.getFile());
The "resource.getFile()" returns the absolute path to the file also.
Hope this helps
Get current diretory path
String currentDirPath = System.getProperty("user.dir");
String ohterPackages = currentDirPath + File.separator + "filereadingexample\\fileName.txt";
User home directory
String homePath = System.getProperty("user.home");
I am trying to set System.setProperty to a file path:
//properties key
String propFile = "propertiesFile";
String pathToFile = "properties/prop.properties";
File file = new file(pathToFile);
//properties value
String path = file.getAbsolutePath();
System.setProperty(propFile, path);
//using properties.....
And I am getting FileNotFoundException.
when printing the file I get the absolutePath - c:\Project...\prop.properties
Is setting System.setProperty should be done another way?
the properties - package inside src.
Your problem is not related to the method System.setProperty as your path is managed as any other String, your problem is more that new File(pathToFile) refers to a non existing file as you provide a relative path and the absolute path is created from the user directory (value of System.getProperty("user.dir")) which is probably not what you expect. If you call new File(pathToFile).exists() it will return false check the resulting path first.
I have my java code like below-
string folderName = "d:\my folder path\ActualFolderName";
File folder = new File( folderName );
folder.mkdirs();
So here as given directory path has space in it. folder created is d:\my, not the one I am expecting.
Is there any special way to handle space in file/folder paths.
You should us \\ for path in java. Try this code
String folderName = "D:\\my folder path\\ActualFolderName";
File folder = new File( folderName );
folder.mkdirs();
Or use front-slashes / so your application will be OS independent.
String folderName = "D:/my folder path1/ActualFolderName";
Unless you are running a really old version of Java, use the Path API from JDK7:
Path p = Paths.get("d:", "my folder path", "ActualFolderName");
File f = p.toFile();
It will take care of file separators and spaces for you automatically, regardless of OS.
Following alternatives should work in Windows:
String folderName = "d:\\my\\ folder\\ path\\ActualFolderName";
String folderName = "\"d:\\my folder path\\ActualFolderName\"";
You need to escape your path (use \\ in your path instead of \) and you also need to use String, with an uppercase S, as the code you posted does not compile. Try this instead, which should work:
String folderName = "D:\\my folder path\\ActualFolderName";
new File(folderName).mkdirs();
If you are getting your folder name from user input (ie.not hardcoded in your code), you don't need to escape, but you should ensure that it is really what you expect it is (print it out in your code before creating the File to verify).
If your are still having problems, you might want to try using the system file separator character, which you can get with System.getProperty(file.separator) or accesing the equivalent field in the File class. Also check this question.
You need to escape path seprator:
String folderName = "D:\\my folder path\\ActualFolderName";
File file = new File(folderName);
if (!file.exists()) {
file.mkdirs();
}
First of all, the String path you have is incorrect anyway as the backslash must be escaped with another backslash, otherwise \m is interpreted as a special character.
How about using a file URI?
String folderName = "d:\\my folder path\\ActualFolderName";
URI folderUri = new URI("file:///" + folderName.replaceAll(" ", "%20"));
File folder = new File(folderUri);
folder.mkdirs();
I'm using camel to create some routes.
I have a file in the resources folder and I only want the file path + name. Not the content.
When I use:
from(URI)
.log("resource:classpath:llave.txt")
I got the content of llave.txt but I need something like
C:\something\llave.txt
Thanks!!
Edit for clarity: I do not need the file information from a File endpoint (also, is easy get that info using file language or the exchange's header).
I need the info of a file located in the resource folder in the project.
Get the file:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("llave.txt").getFile());
Get the path:
//full path ( including the file )
String absolutePath = file.getAbsolutePath();
// path only
String filePath = absolutePath.
substring(0, absolutePath.lastIndexOf(File.separator));
You can access the path using the headers that Camel sets after calling the File component:
from(URI)
.log("$simple{headers.CamelFileAbsolutePath}")
You can read more about the File component here:
http://camel.apache.org/file2.html
With java.nio you can get the full path:
String fullPath = Paths.get(getClass().getResource("llave.txt").toURI()).toString();
And the file name:
String fileName = Paths.get(getClass().getResource("llave.txt").toURI()).getFileName();
How can I get an absolute path of a directory containing a file specified:
// current dir is "/home/me/dev"
File file = new File("./target/test.txt");
assert absolute(file).equals("/home/me/dev/target");
It's Java 6.
You mean the methods in the documentation?
File file = new File("./target/test.txt");
String dirPath = file.getAbsoluteFile().getParentFile().getAbsolutePath()
assert dirPath.equals("/home/me/dev/target");
assert file.getParentFile().getAbsolutePath().equals("/home/me/dev/target");