how to change hebrew path into english in dropbox api(Java) - java

I'm working with the dropbox api to java and i have trying to read from dropbox.
When I use the path of the api it looks like this -
טיול פסח path: /Public/טיול פסח
(Ignore the hebrew words it's like -
someFolderName paht: /Public/someFolderName)
and it's looks as usual, but when I try to go to this path it's give me this exception -
Exception in thread "main" java.lang.NullPointerException
at node.Main.listFolders(Main.java:102)
at node.Main.main(Main.java:112)
When I use the toString() method of the api that give some of the metaData including the path of the folder it's shows me this -
טיול פסח toString(): Folder("/Public/\u05d8\u05d9\u05d5\u05dc \u05e4\u05e1\u05d7"...)
When the string within the quotes should be the path.
When I tried to enter this as the path it's give me this exception -
Exception in thread "main" java.lang.NullPointerException
at node.Main.listFolders(Main.java:102)
at node.Main.main(Main.java:112)
The code example:
The path is "/Public" and it's the argument s.
client is the access to my dropbox.
listing is list of children of the root folder.
public static void listFolders(String s) throws DbxException, IOException {
//list of children.
DbxEntry.WithChildren listing = client.getMetadataWithChildren(s);
for (DbxEntry child : listing.children) {
System.out.println(" " + child.name + " toString(): " + child.toString());
System.out.println(" " + child.name + " path: " + child.path);
}
}
The output of this with "/Public" as argument is:
טיול פסח toString(): Folder("/Public/\u05d8\u05d9\u05d5\u05dc \u05e4\u05e1\u05d7", iconName="folder", mightHaveThumbnail=false)
טיול פסח path: /Public/
Any thoughts?

Related

Eclipse not seeing java file properties

I'm doing internationalisation in Java.
I've created two files: MessageBundle_en_US.properties and MessageBundle_en_IN.properties.
MessageBundle_en_US.properties:
greeting=Hello, how are you?
MessageBundle_en_IN.properties
greeting=Halo, apa
And finally, here's the main class:
import java.util.*;
public class InternationalizationDemo {
public static void main(String[] args) {
ResourceBundle bundle =
ResourceBundle.getBundle("MessageBundle", Locale.US);
System.out.println(
"Message in "
+ Locale.US
+ ": "
+ bundle.getString("greeting")
);
//changing the default locale to indonasian
Locale.setDefault(new Locale("in", "ID"));
bundle = ResourceBundle.getBundle("MessageBundle");
System.out.println(
"Message in "
+ Locale.getDefault()
+ ": "
+ bundle.getString("greeting")
);
}
}
And I receive the following error stacktrace:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name MessageBundle, locale en_US
at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1556)
at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:932)
at provame.InternationalizationDemo.main(InternationalizationDemo.java:8)
UPDATE
I've found the reason it was unable to see the file, first of all you need to put *properties file in the same directory as the main class, as the kind people suggested in the comments, and you should adjust the call to the file like that:
projectName/propertiesFileName
This was all.
Place your *.properties files in the same folder as InternationalizationDemo.java
Rename: MessageBundle_en_IN.properties to: MessageBundle_in_ID.properties

Java - how to create a file under unknown/variable path?

I have a directory: <dir>\Report\<env>\Log_XXX\Logs
where XXX is randomly created at run time, so I have to create a file inside Logs folder.
Following is what I tried to generate the Logs folder:
new File(System.getProperty("user.dir") + "/Report/" + System.getProperty("env") + "/" + Pattern.compile("^Log_") + "/Logs").mkdirs();
Based on your comments, it appears you are trying to locate the one and only subdirectory whose base name starts with Log_. You can accomplish this with Files.list:
Path logParent = Paths.get(
System.getProperty("user.dir"),
"Report",
System.getProperty("env"));
Path logDir;
try (Stream<Path> listing = Files.list(logParent)) {
Optional<Path> match = listing.filter(p -> Files.isDirectory(p) &&
p.getFilename().toString().startsWith("Log_")).findFirst();
logDir = match.orElseThrow(() -> new RuntimeException(
"No log directory found in " + logParent));
}

Getting the Absolute Path of the current class in eclipse (Resource Location of a class)

I've been trying to get the absolute path of the class I am working on.
I have tried doing this:
Class currentClass = new Object() {
}.getClass().getEnclosingClass();
System.out.println(currentClass);
System.out.println(" PATH IS: " + currentClass.getProtectionDomain().getCodeSource().getLocation());
System.out.println("Present Project Directory : " + System.getProperty("user.dir"));
BUT I am getting this kind of output:
PATH IS: file:/C:/Users/jean/eclipse-workspace/AutoCompile/bin/
Present Project Directory : C:\Users\jean\eclipse-workspace\MyProject
I want to get a result like this:
C:\Users\jean\eclipse-workspace\MyProject\src\com\school\demo\Locator.java

Java absolute path adds user.home property when adding quotes - Linux

If I try to use a path that contains spaces in Linux I get FileNotFoundException, obviously. But if I try to add double/single quotes in the path as workaround, I get the same exception.
I was trying to check the reason and I found out that the generated absolute path when using quotes became: the user.home system property + specified path.
For example:
If I use this path:
/home/db2inst1/Desktop/testing - Development Environmet/64_dev/testing/logs
This is the absolute path I get when trying to use quotes:
/home/db2inst1/"/home/db2inst1/Desktop/testing - Development Environmet/64_dev/testing/logs"
I also tried to replace the spaces with "\ " instead of adding quotes, but it did not work.
I tried a lot of API's and it happens every time, made this code just for testing:
System.out.println("- regular path: ");
System.out.println(new File(path).getPath());
System.out.println(new File(path).getAbsolutePath());
System.out.println("- quoted path: ");
System.out.println(new File(quotedPath).getPath());
System.out.println(new File(quotedPath).getAbsolutePath());
And this is the output:
- regular path:
/home/db2inst1/Desktop/testing - Development Environmet/64_dev/testing/logs/testing.log
/home/db2inst1/Desktop/testing - Development Environmet/64_dev/testing/logs/testing.log
- absolute path:
"/home/db2inst1/Desktop/testing - Development Environmet/64_dev/testing/logs/testing.log"
/home/db2inst1/"/home/db2inst1/Desktop/testing - Development Environmet/64_dev/testing/logs/testing.log"
Does anyone know why this is happening and how to make it work?
From your description it seems that you are calling the File(java.lang.String pathname) constructor.
If so, the String used to represent your path should not use quotes.
Quotes are not considered as special characters in the abstract pathname definition, as described in the java.io.File documentation.
An abstract pathname has two components:
An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX >root directory, or "\\" for a Microsoft Windows UNC pathname, and
A sequence of zero or more string names.
Since quotes are not special characters, they are considered part of a name.
Example:
public static void main(String[] args) {
File quotes = new File("\"C:\\myFolder\"");
File noQuotes = new File("C:\\myFolder");
System.out.println("Absolute path with quotes:" + quotes.getAbsolutePath());
System.out.println("Absolute path without quotes:" + noQuotes.getAbsolutePath());
System.out.println("Equal: " + quotes.equals(noQuotes));
File empty = new File("");
File emptyQuotes = new File("\"\"");
System.out.println("Empty path with quotes:" + empty.getAbsolutePath());
System.out.println("Empty path without quotes:"
+ emptyQuotes.getAbsolutePath());
System.out.println("Equal: " + empty.equals(emptyQuotes));
}
will produce the following output when run in C:\temp on Windows:
Absolute path with quotes:C:\temp\"C:\myFolder"
Absolute path without quotes:C:\myFolder
Equal: false
Empty path with quotes:C:\temp
Empty path without quotes:C:\temp\""
Equal: false
In windows a file name without blanks and the same quoted should refer to the same file (or folder). For instance if we have a folder called c:\uni2 both command lines
dir c:\uni2
dir "c:\uni2"
should give the same result. But in java
String rename;
boolean ya;
File f1 = new File ("C:/UNI2"); // given that exists and it is a directory
ya = f1.exists(); // true
ya = f1.isFile(); // false
ya = f1.isDirectory(); // true
rename = f1.getAbsolutePath(); // "C:\\UNI2"
f1 = new File ("\"C:/UNI2\""); // in windows this should be the same directory!!
ya = f1.exists(); // false
ya = f1.isFile(); // false
ya = f1.isDirectory(); // false
rename = f1.getAbsolutePath(); // "C:\tmp\"C:\UNI2""
which is not the expected behavior (even if it is documented).

How to access network paths from Java on OSX?

I try to access a network folder / UNC path from Java on Mac OSX. On Windows, the following test program works fine (at least one of the tested paths):
public class PathTest {
public static void main(String[] args) {
for (String path : Arrays.asList(
"\\\\myserver\\transfer", "//myserver/transfer", "file://myserver/transfer", "smb://myserver/transfer")) {
File f = new File(path);
System.out.println(path + ": " + f.getAbsolutePath() + ", " + f.exists());
Path p = Paths.get(path);
System.out.println(path + ": " + p.toAbsolutePath() + ", " + Files.exists(p));
}
}
}
on Mac OS it fails to reach the folders:
\\myserver\transfer: /Users/tim/IdeaProjects/PathTest/\\myserver\transfer, false
//myserver/transfer: /myserver/transfer, false
file://myserver/transfer: /Users/tim/IdeaProjects/PathTest/file://myserver/transfer, false
smb://myserver/transfer: /Users/tim/IdeaProjects/PathTest/smb://myserver/transfer, false
When I use Finder, I can access the Folder (using the Guest user), by using "smb://myserver/transfer". What's wrong?
EDIT added NIO.2 test
Either mount the partition and access it as any local directory or use a specialized library such as JCIFS or Apache Commons VFS.

Categories