Let's say we have:
String path = "D:\aaa\bbb\ccc"
I wonder if there is a function to modify quickly path to:
D:\aaa\bbb\ or D:\aaa\
I tried to use Paths with no luck:
path = "D:\\aaa\\bbb\\ccc";
pathNew = java.nio.file.Paths.get(path).subpath(0,2).toString();
println "${pathNew}"
Gives the next console result:
aaa\bbb
You can do:
String path = "D:\\aaa\\bbb\\ccc";
File parent = new File(path).getParentFile();
System.out.println(parent);
String parentStr = new File(path).getParent();
System.out.println(parentStr);
Prints:
D:\aaa\bbb
D:\aaa\bbb
You can do like this:
Path p1 = Paths.get("D:\\aaa\\bbb\\ccc");
Path p2 = p1.getParent();
....
try this
Path path = Paths.get("your path");
Path parentPath = path.getParent();
File path = new File("D:\aaa\bbb\ccc");
path.getParentFile(); // Returns "D:\aaa\bbb\"
path.getParentFile().getParentFile(); // Returns D:\aaa\"
File parent = new File("D:\\aaa\\bbb\\ccc").getParent();
System.out.println(parent);
Related
I have a File
/user/guest/work/test/src/main/java/Test.java
And a File-Object:
File f = new File("/user/guest/work/test/src/main/java/Test.java");
I need this outputs
System.out.println(f); -> src/main/java/Test.java
System.out.println(f.getAbsolutePath()); -> /user/guest/work/test/src/main/java/Test.java
I tried:
File relativeTo = new File("/user/guest/work/test");
new File(relativeTo.toURI().relativize(f.toURI()));
but it is throwing a
java.lang.IllegalArgumentException: URI is not absolute
at java.io.File.<init>(File.java:416)
at Test.<init>(Test.java:43)
How to get the required output?
relativize returns a URI.
a new File(URI uri) takes...
uri - An absolute, hierarchical URI
You can instead try using the String constructor.
new File(relativeTo.toURI().relativize(f.toURI()).toString());
You have access to that file other ways, however
For example, you can try going through the java.nio.file.Path API instead of java.io.File
Like
Path path = Paths.get("/", "user", "guest", "workspace",
"test", "src", "main", "java", "Test.java");
Path other = ...
Path relPath = other.relativize(path);
// relPath.toString(); // print it
// relPath.toFile(); // get a file
You can use path resolve to relativize file paths
File f = new File("/user/guest/workspace/test/src/main/java/Test.java");
File relativeTo = new File("/user/guest/workspace/test");
System.out.println(new File(relativeTo.toPath().resolve(f.toPath()).toUri()));
I'm creating FileDialog and trying to get a FilePath for FileDialog object.
FileDialog fd = new FileDialog(this, "Open", FileDialog.LOAD);
fd.setVisible(true);
String path = ?;
File f = new File(path);
In this codes, I need to get a absolute FilePath for using with File object.
How can I get filepath in this situation?
You can combine FileDialog.getDirectory() with FileDialog.getFile() to get a full path.
String path = fd.getDirectory() + fd.getFile();
File f = new File(path);
I needed to use the above instead of a call to File.getAbsolutePath() since getAbsolutePath() was returning the path of the current working directory and not the path of the file chosen in the FileDialog.
Check out File.getAbsolutePath():
String path = new File(fd.getFile()).getAbsolutePath();
I want to add .jar dynamically. So I make a demo. But I don't know how to new the DexClassLoader. I don't know how to add the first params.
final File optimizedDexOutputPath = new File("" + File.pathSeparator + "test.jar");
//PackageManager pm = getPackageManager();
String dexOutputDir = getApplicationInfo().dataDir;
DexClassLoader dexClassLoader = new DexClassLoader("", dexOutputDir, null, getClassLoader());
I have made the dynamical .jar as test.jar(become dex) as well as a new folder 'text' in projects and put the text.jar in it.
Can you help me see what I have done wrong?
Try this code:
// dexPath is the absolute path of your **DEX** file
ClassLoader loader = context.getClassLoader();
dexLoader = new dalvik.system.DexClassLoader(**dexPath**, dexOutputDir, null, loader);
The code to dynamically load a jar should look something like this:
//get the path to your .jar as a String
String jarPath = this.getApplicationContext().getFilesDir().getAbsolutePath();
jarPath += File.pathSeparator + "test" + File.pathSeparator + "test.jar";
//get a path to the directory you want to store odexs in as a String
String optimizedDir = this.getApplicationContext().getDir("odex", MODE_PRIVATE).getAbsolutePath();
//finally, call DexClassLoader
DexClassLoader dcl = new DexClassLoader( jarPath, optimizedDir, null, getClassLoader() );
The above assumes you've created a directory named "test" within your app's private files area and placed test.jar within that directory. You could create this directory and possibly copy test.jar from your app's assets area into this directory when your app first starts up.
I need check at least 4 directories if they exists and get the correct path in a variable for finnish my code.
But I don't know the correct way to do that.
Thanks for your help.
Here my code for check a single directory
final String uploadFilePath = "/mnt/sdcard/folder1/";
File f = new File(uploadFilePath);
if(f.exists() && f.isDirectory()){
Log.v("FILES", "EXIST");
}else{
Log.v("FILES", "DONT EXIST");
}
This way you can go on
String[] myDirectories = {"","",""......}; // your list of directories
for (String directory : myDirectories ) {
File file = new File(directory);
if(file.exists() && file.isDirectory())
// Do something you have found your directory
}
Is this enough?
Path uploadPath = Paths.get(uploadFilePath);
Path path = Files.exists(uploadPath) ? uploadPath : Files.createDirectory(uploadPath);
System.out.println(path);
Then to check for the N directories, put it in a loop maybe.
final String[] paths = { "C:/aa/", "C:/bb/", "C:/cc/", "C:/dd/"};
for (String path : paths) {
Path uploadPath = Paths.get(path);
if(Files.exists(uploadPath))
Files.createDirectory(uploadPath);
System.out.println(uploadPath);
}
Imagine I have a 'base' path object, denoting a directory, and a 'relative' path object denoting some file within the base.
I would expect that code to look somewhat like
AbsolutePath base = new AbsolutePath("/tmp/adirectory");
RelativePath relativeFilePath = new RelativePath("filex.txt");
AbsolutePath absoluteFile = base.append( relativeFilePath );
But in the Java API (which I don't yet know very well) I find only File, with which I can do nothing better than
File base = new File("/tmp/adirectory");
File relativeFilePath = new File("filex.txt");
File absoluteFile = base.toString()
+ File.separator
+ relativeFilePath.toString();
Is there a better way?
The closest you can get with java.io.File is the File(File, String) constructor:
File base = ...;
File relative = ...;
File combined = new File(base, relative.toString());
If you can use the Path class introduced in Java 7, then you can use the resolve() method, which does exactly what you want:
Path base = ...;
Path relative = ...;
Path combined = base.resolve(relative);
Please note that if base is not an absolute path, then combined won't be absolute either! If you need an absolute path, then for a File you'd use getAbsoluteFile() and for a Path you'd use toAbsoutePath().
Yes. new File(base, "filex.txt") will create a file names "filex.txt" in the directory base.
There is no need to create a relativeFilePath File instance with just the relative name if what you want to do is make it relative to another directory than the current one.
how about:
File base = new File("/tmp/adirectory");
File absoluteFile = new File(base, "filex.txt");
EDIT: Too late #JB Nizet pipped me at the post...
The File class has some constructors which may be of interest to you:
File base = new File("/tmp/adirectory");
File absolute = new File(base, "filex.txt");
File absolute2 = new File("/tmp/adirectory", "filex.txt");