I have a code snippet that get path of felix home like this:
String path = System.getProperty('felix.home')
Felix home place at:
D:\LongFolderName\Implementations\brannches\MoreLongFolerName\
Path will be a string like this:
D:\LONG-F~1\IMPLEM~1\branches\MORE~1\
I want it must be full path without '~' character like:
D:\LongFolderName\Implementations\brannches\MoreLongFolerName\
What i have to do?
Try:
// Groovy
String path = new File( System.properties.'felix.home' ).canonicalPath
or
// Java
String path = new File( System.getProperty('felix.home') ).getCanonicalPath()
Related
I am trying to get the file path of a file from a file picker.
My problem is, that when I output the file path in text form, I get the following output:
file:/storage/emulated/~rest_of_path~
I assume, to access the file, the path should begin something like this:
file:///storage/emulated/~rest_of_path~
or at least file://
Why do I only get file:/ i.e a single slash.
The code in charge for getting the file path:
...
File fileToImport = new File(actualfilepath);
String tempPath = uri.getPath();
if (tempPath.contains("//")){
tempPath = tempPath.substring(tempPath.indexOf("//")+1);
}
if ( actualfilepath.equals("") || actualfilepath.equals(" ")) {
fileToImport = new File(tempPath);
}else {
fileToImport = new File("file://"+actualfilepath);
}
// TODO: 2019-06-04
functionforfile(fileToImport)
filePath.setText(fileToImport.toString());
The text view for the code filePath.setText(fileToImport.toString());
shows me the file path for debug purpose only
if fileToImport = new File("file://"+actualfilepath); has File("file://"actualfilepath) instead, it outputs the file path without file:// which I understand, but why do the two // get removed when I add "file://" + actualfilepath
How could I add the correct file:///
Thanks in advance
I want to get "to" from the below string which is the path of a file.
String path="/Path/to/Text.txt";
String path="/The/Path/to/Text.txt";
How do i get the subdirectory name "to"?
Java has a library class to work with files. It is called File (surprisingly...):
import java.io.File;
//...
File file= new File("/Path/to/Text.txt");
File parentDir = file.getParent();
System.out.println(parentDir.getName());
You can use Path class:
Path p = Paths.get("/The/Path/to/Text.txt");
System.out.println(p.getParent()); // /The/Path/to
System.out.println(p.getParent().getFileName()); // to
System.out.println(p.getName(2)); // to
If your path is a String:
String[] directories = path.split("/");
System.out.println(directories[directories.length-2]);
But remember to check your path length to avoid indexOutOfBounds
Im giving the path of the image to properties file and getting the path to Image class in my pdfGenerating class. like:
properties.load(DownloadPdf.class.getResourceAsStream("/resource.properties"));
System.out.println("--properties----"+properties);
System.out.println("-path-"+properties.getProperty("logoPath"));
//String path = properties.getProperty("logoPath");
//URL uri = Paths.get(path).toUri().toURL();
System.out.println("---path-"+properties.getProperty("logoPath"));
Image image = Image.getInstance(properties.getProperty("logoPath"));
Here my logopath is:
path----------------C:/Users/Home/Documents/workspace-gofindo/.metadata/.plugins/org.eclipse.wst.server.core/.....
But i'm getting the path in
Image.getInstance(properties.getProperty("logopath"));
like
C:\Users\Home\Documents\workspace-gofindo\.metadata\.plugins\org.
I have tried with replace and replaceAll() methods to convert '\' to '/'
again the image class converting into '\'.
How to get my absolute path which i have specified in properties file exactly into Image.getInstance() method
I have remodified the way i can read the path of the file like below.
// Load path of the Image from Properties file
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream(
"/resource.properties"));
// To get the Path of the Context
String contextPath = request.getContextPath();
String split_path[] = contextPath.split("/");
contextPath = request.getRealPath(split_path[0]);
Image image = Image.getInstance(contextPath
+ properties.getProperty("logoPath"));
image.scaleAbsolute(120f, 60f);// image width,height
I need to extract up to the directory a file is in, in the folder path. To do so, I created a simple regex. Here is an example path \\myvdi\Prod\2014\10\LTCG\LTCG_v2306_03_07_2014_1226.pfd
The regex below will find exactly what I need, but my problem is storing it into a variable. This is what I have below. It fails at the string array
String[] temp = targetFile.split("\\.*\\");
folder = temp[0];
Suggestions?
Thanks!
Edit
The exception being thrown is: java.util.regex.PatternSyntaxException: Unexpected internal error near index 4
If your path is valid within your file system, I would recommend not using regex and using a File object instead:
String path = "\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
File file = new File(path);
System.out.println(file.getParent());
Output
\\myvdi\\Prod\\2014\\10\\LTCG\\
Simply, you need:
String path = "\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
String dir = path.substring(0, path.lastIndexOf("\\") + 1);
You should use Pattern & Matchers which are much more powerful; from your description I'm not sure if you want to get the whole folder path, but if it is, here is a solution:
String s = "\\\\myvdi\\Prod\\2014\\10\\LTCG\\LTCG_v2306_03_07_2014_1226.pfd";
Pattern p = Pattern.compile("^(\\\\+[a-zA-Z0-9_-]+\\\\+([a-zA-Z0-9_-]+\\\\+)+).+$");
Matcher m = p.matcher(s);
if(m.matches()){
System.out.println(m.group(m.groupCount()-1));
}
I have DirectoryPath:
data/data/in.com.jotSmart/app_custom/folderName/FileName
which is stored as a String in ArrayList
Like
ArrayList<String> a;
a.add("data/data/in.com.jotSmart/app_custom/page01/Note01.png");
Now from this path I want to get page01 as a separate string and Note01 as a separate string and stored it into two string variables. I tried a lot, but I am not able to get the result. If anyone knows help me to solve this out.
f.getParent()
Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
For example
File f = new File("/home/jigar/Desktop/1.txt");
System.out.println(f.getParent());// /home/jigar/Desktop
System.out.println(f.getName()); //1.txt
Update: (based on update in question)
if data/data/in.com.jotSmart/app_custom/page01/Note01.png is valid representation of file in your file system then
for(String fileNameStr: filesList){
File file = new File(fileNameStr);
String dir = file.getParent().substring(file.getParent().lastIndexOf(File.separator) + 1);//page01
String fileName = f.getName();
if(fileName.indexOf(".")!=-1){
fileName = fileName.substring(0,fileName.lastIndexOf("."));
}
}
For folder name: file.getParentFile().getName().
For file name: file.getName().
create a file with this path...
then use these two methods to get directory name and file name.
file.getParent(); // dir name from starting till end like data/data....../page01
file.getName(); // file name like note01.png
if you need directory name as page01, you can get a substring of path u got from getparent.
How about using the .split ?
answer = str.split(delimiter);