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
Related
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 + "\""});`
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).
This question already has answers here:
Saving path in String
(4 answers)
Closed 6 years ago.
I'm sure this has been answered, but ten different strategies hasn't worked on this issue.
If I use C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt
as my absolute path for the file, IDEA cannot read or execute from this path. If I take that same path and paste it into windows explorer, it will execute right away. I dont want to focus on a working directory as this file works as the program's configurations file, but replaceing the slashes with backslashes doesnt work, the absolute path still brings me to the file, but IDEA doesnt launch.
I'm at wits end.
public static String generateFileName(String folder){
String filename = "";
List<String> hashtags = new ArrayList<>();
String instructions_file = "C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt";
//does not return true-true, but can launch file on windows explorer..
System.out.println("FILE EXIST AND EXECUTE?" + new File(instructions_file).getAbsoluteFile().canRead() +" "+new File(instructions_file).getAbsoluteFile().canExecute());
System.out.println(new File(instructions_file).getAbsoluteFile());
//C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(new File(instructions_file).getAbsoluteFile()));
EDIT
After replacing backslashes with forward slashes, the reader still cannot properly read or execute the file.
LOG:
The string prints:
C:/Users/Anny/Dropbox/SocialMediaOcto/instructions/Bees/instructions.txt
java.io.FileNotFoundException: C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Bees\instructions.txt (The system cannot find the file specified)
Correct url:
String instructions_file = "C:/Users/Anny/Dropbox/SocialMediaOcto/instructions/Trees/instructions.txt";
Because \ is an escape character in Java. If you want to use \ as a character you have to escape it itself.
Correct Url v2:
String instructions_file = "C:\\Users\\Anny\\Dropbox\\SocialMediaOcto\\instructions\\Trees\\instructions.txt";
What you had:
String instructions_file = "C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt";
is read by java as
"C:{something}sers{something}nny{something}ropbox{something}ocialMediaOcto{something}nstructions\Trees\instructions.txt"
In my oppinion it's much better to use the first approach as it's platform safe.
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 '\'.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to read properties file in java
I have a 'MyProp.properties' file having key-values like:
browser = Firefox
baseURL = http:\\www.google.com
user = student
The above key-value pairs I want to use in my project
How can I achieve that?
I am new to JAVA, please guide me through
prop.load(new FileInputStream("config.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("browser"));
You could have spent some time in google/stackoverflow before throwing questions. .