File.Separator in Properties [duplicate] - java

This question already has answers here:
Java properties file specs [duplicate]
(2 answers)
Closed 7 years ago.
I am using java.util.Properties. However, it cannot read File.separator inside the config file.
As an example, I add this line to the config file. source.dir = D:/workspace/Temp1\Temp2 (Note that File.separator is used to separate Temp1 and Temp2)
The below line is used to load propertis:
Properties properties = new Properties ();
properties.load(new FileInputStream("configFileAddress"));
The result is: source.dir = D:/workspace/Temp1Temp2 (File.Separator is removed).
Any one knows, how can I fix that?

Replace:
source.dir = D:/workspace/Temp1\Temp2
To:
source.dir = D:\\workspace\\Temp1\\Temp2
This field is initialized to contain the first character of the value
of the system property file.separator. On UNIX systems the value of
this field is '/'; on Microsoft Windows systems it is '\'.

Related

Class use txt file in the same jar NPE [duplicate]

This question already has answers here:
How to read a file from jar in Java?
(6 answers)
How to read a text file inside a JAR? [duplicate]
(4 answers)
Closed 3 years ago.
I have class files and a text file wrapped up in a jar. This problem has been solved on the internet before, but when I try it I get a null pointer exception, or File f.exists() returns false. It should be noted that my code is not in a package. It should be noted that when help.txt is dropped in the same folder as the jar, then it works.
`MyClass z = new MyClass();
String helpPath = z.getClass().getClassLoader().getResource("help.txt").toString();
File f = new File(helpPath);
if (f.exists()){
Desktop d = Desktop.getDesktop();
d.open(f);`
It should also be noted that I have code written to open powershell and then java my class file, with no specified classpath.
` Runtime.getRuntime().exec(new String[]{"cmd","/c","start","powershell","-noexit","/c","java -jar \"" + filename + "\""});`

Read backslashes from propertyfile [duplicate]

This question already has answers here:
Java Properties backslash
(9 answers)
Closed 4 years ago.
I have a property file: project.properties in which I store some property, such as JAVA_PATH=D:\Application\Java8.
I am trying to read it by Java using FileInputStream.
InputStream input = null;
input = new FileInputStream("D:\\Application\\project.properties");
Properties props = new Properties();
props.load(input);
String javaPath = props.getProperty("JAVA_PATH");
System.out.println("JAVA_PATH : "+javaPath);
The output I am getting is:
JAVA_PATH : D:ApplicationJava8
I understand that Java treats this as an escape character and removes it. But is there any way I can read the property as it is by any kind of massaging?
I want the output as JAVA_PATH : D:\Application\Java8.
The \ needs to be escaped with an additional \ in the .properties file, like so:
JAVA_PATH=D:\\Application\\Java8

Program adds '\' before ':' when writing .properties file in Java [duplicate]

This question already has answers here:
How do you escape colon (:) in Properties file?
(8 answers)
Properties file backslash and semicolon
(4 answers)
Closed 5 years ago.
I'm trying to update my database.properties file in my program. However,
it seems Java is adding additional character on my properties file.
Here's a snippet of my code.
Properties props = new Properties();
String propsFileName = "src/resources/properties/" + "database.properties";
String[] property = new String[4];
property[0] = "database.properties";
property[1] = url.getText();
property[2] = username.getText();
property[3] = password.getText();
try {
FileInputStream configStream = new FileInputStream(propsFileName);
props.load(configStream);
configStream.close();
props.setProperty("jdbc.driverClassName", Commons.driverClassName);
props.setProperty("jdbc.url", property[1]);
props.setProperty("jdbc.username", property[2]);
props.setProperty("jdbc.password", property[3]);
props.setProperty("jdbc.comment", comments.getText());
FileOutputStream output = new FileOutputStream(propsFileName);
props.store(output, null);
output.close();
} catch (IOException ex) {
ex.printStackTrace();
}
I have this output on my console which is to check if I get the string that I wanted,
Properties = org.postgresql.Driver,
jdbc:postgresql://192.168.1.1:1234/db, username,
password, null Program Ran on Fri Jun 23 16:00:37 PHT 2017 by
user=xngapp234
However, in my database.properties file, I'm getting this output.
jdbc.url=jdbc\:postgresql\://192.168.1.1\:1234/db
jdbc.username=username
jdbc.password=password
jdbc.comment=
jdbc.driverClassName=org.postgresql.Driver
It adds '\' before the ':' which always gives me an error. Any help is appreciated.
Thanks!
If you're trying to store and load properties from a Java program, everything is working as supposed! Your code shows that the url is read without the backslashes - just as you stored it.
As Berger writes, this is the normal escaping in java.properties files. This is documented in the Javadoc of Properties.store():
[...] The key and element characters #, !, =, and : are written with a
preceding backslash to ensure that they are properly loaded. [...]
If you try to use the Properties class to write a file to be consumed by a non-Java program, you're out of luck as that's not what this class is intended for. You'll need to use another library for that.
The javadoc states
The Properties class represents a persistent set of properties.
The ".properties"-format is just one representation of a set of properties (with an XML format being another).

How to Hadoop Path to Java File [duplicate]

This question already has answers here:
How to convert a Hadoop Path object into a Java File object
(3 answers)
Closed 5 years ago.
I am working on code which processes File objects. I would like to store the actual files in HDFS, but to retrieve them as File objects.
I found the method pathToFile (Hadoop LocalFileSystem), which supposedly does exactly what I want, but it doesn't seem to work.
There is another question with almost the same topic (How to convert a Hadoop Path object into a Java File object) but it isn't solved.
Is there anyone who actually used the method and converted a Hadoop Path to a Java File?
fyi: I searched all over the Internet and I couldn't find not even a chunk of code where the pathToFile is used and works efficiently.
In my code:
String uri = args[0]; //give the hdsf path as argument
Configuration conf = new Configuration(); //create a Configuration obj
Path pathOfFile = new Path(uri); //create a Hadoop Path obj
LocalFileSystem myFS = FileSystem.getLocal(conf); //LocalFileSystem creation
File theFile = myFS.pathToFile(pathOfFile); // using pathToFile
Perhaps this piece of code could help replace /etc/hadoop/conf with your local config
Configuration configuration = new Configuration();
configuration.addResource("/etc/hadoop/conf/core-site.xml");
configuration.addResource("/etc/hadoop/conf/core-site.xml");
configuration.addResource("/etc/hadoop/conf/hdfs-site.xml");
FileSystem hdfsFileSystem = FileSystem.get(configuration);
Path pathOfFile = new Path("/user/giorgos/test.txt"));

Switch the name of the file [duplicate]

This question already has answers here:
Rename a file using Java
(15 answers)
Closed 9 years ago.
I have file names like 323423233.
I want to add the last 2 digits of the file name and add it to the front and
make it 33/323423233 and add extension to it(like .doc).
What's a simple statement that I can use to achieve this?
This is 2013. This is Java 7. This is the time for Files and Path.
Base directory:
final Path baseDir = Paths.get("/path/to/baseDir");
Determine subdirectory for a file:
final String s = name.substring(name.length() - 2, name.length());
Create that directory:
final Path subDir = baseDir.resolve(s);
// Will not do anything if directory already exists...
// But will throw exception if unable to create
Files.createDirectories(subDir);
Write to the file:
final Path dst = subDir.resolve(name + ".doc");
Files.copy(src, dst);
Remove original:
Files.delete(src);
Or in one operation:
Files.move(src, dst);

Categories