In my spring project, one of my service classes has this method to save a file named database.properties in disk:
public void create_properties(String maquina, String usuario, String senha) {
System.out.println("create_properties");
Properties props = new Properties();
props.setProperty("jdbc.Classname", "org.postgresql.Driver");
props.setProperty("jdbc.url", "jdbc:postgresql://"+maquina+"/horario" );
props.setProperty("jdbc.user", usuario );
props.setProperty("jdbc.pass", senha );
props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
props.setProperty("hibernate.show_sql", "false");
props.setProperty("hibernate.hbm2ddl.auto", "validate");
FileOutputStream fos;
try {
fos = new FileOutputStream( "database.properties" );
props.store( fos, "propriedades" );
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
My problem is that the property jdbc:url should be something like that:
jdbc:postgresql://localhost:5432/horario
But what is being saved is this:
jdbc\:postgresql\://localhost\:5432/horario
Anyone can tell me how to avoid this backslashes to be included?
It's doing exactly the right thing - you're saving a properties file, which escapes things like colons using backslashes. From the documentation for Properties.store:
Then every entry in this Properties table is written out, one per line. For each entry the key string is written, then an ASCII =, then the associated element string. For the key, all space characters are written with a preceding \ character. For the element, leading space characters, but not embedded or trailing space characters, are written with a preceding \ character. The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.
If you load the properties file in using Properties.load, you'll get the original string back in the Properties object.
If you don't want to store the value in a properties file, use a Writer and just write the string directly.
Related
I am trying to write data to CSV file.
The string value which starts with - is getting converted to #NAME? automatically when i open csv file after writing. e.g. If i write test it displays correctly but when i write -test the value would be #NAME? when i open csv file. It is not a code issue but csv file automatically changes the value which starts with - to error(#NAME?). How can i correct this programmatically. below is the code,
public class FileWriterTest {
public static void main(String[] args) {
BufferedWriter bufferedWriter = null;
File file = new File("test.csv");
try {
bufferedWriter = new BufferedWriter(new FileWriter(file));
List<String> records = getRecords();
for (String record : records) {
bufferedWriter.write(record);
bufferedWriter.newLine();
}
bufferedWriter.flush();
System.out.println("Completed writing data to a file.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null)
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static List<String> getRecords() {
List<String> al = new ArrayList<String>();
String s1 = "test";
String s2 = "-test";
al.add(s1);
al.add(s2);
return al;
}
}
Could you please assist?
It's a problem with excel. When you open a CSV file in excel it tries to determine cell type automatically which usually fails. The CSV file is alright the editor is not ;)
You can either right click on the field, select Format Cell and there make it a text file (and you might need to remove the automatically inserted '=' sign). Or you can open the CSV file by going into Data - From Text/CSV and in the wizard select the proper column types.
In the formal CSV standard, you can do this by using quotes (") around the field value. They're a text delimiter (as opposed to other kinds of values, like numeric ones).
It sounds like you're using Excel. You may need to enable " as a text delimiter/indicator.
Update: If you double-click the .csv to open it in Excel, even this doesn't work. You have to open a workbook and then import the CSV data into it. (Pathetic, really...)
I got a relatively old version of Excel (2007), and the following works perfectly:
Put the text between double quotes and preceed it with an equal sign.
I.e., -test becomes ="-test".
You file will therefore look like this:
test1,test2,test3
test4,="-test5",test6
UPDATE
Works in Excel-2010 as well.
As Veselin Davidov mentioned, this will break the csv standard but I don't know whether that's a problem.
I am using Selenium to test an e-commerce application. I need to check the items listed on selecting a category on the listing page match with items in the Database. So I am accessing pages using selenium and storing the page source in a text file. I later parse this text file using HTMLCleaner and JSoup to get the field I wish to validate with the DB.
However, I noticed that some products listed on the page use special characters like ™ , ® and so on which are not stored/retrieved correctly and displayed as question marks.
Code I am using to store the page source:
BufferedWriter writer = null;
try
{
writer = new BufferedWriter(new FileWriter(filepath+"/"+filename+".txt"));
writer.write(driver.getPageSource());
}
catch ( IOException e)
{
e.printStackTrace();
}
finally
{
try
{
writer.close( );
}
catch (IOException e)
{
}
}
Retrieving and parsing file
Document htmlFile = Jsoup.parse(fileSavedPreviously,"ISO-8859-1");
TagNode tagNode = new HtmlCleaner().clean(fileSavedPreviously);
try {
org.w3c.dom.Document doc = new DomSerializer(new CleanerProperties())
.createDOM(tagNode);
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//rest of the parsing....
Define the encoding for the stream writer :
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fullpath), "UTF-8"));
And provide the same one to the parser :
Document htmlFile = Jsoup.parse(fileSavedPreviously, "UTF-8");
To get the encoding of the page, execute document.inputEncoding in the browser console.
If you use the constructors of FileWriter it will use the default charset which doesn't cover those special characters obviously if you face such issue so you should instead use the constructors of OutputStreamWriter to define explicitly the character encoding as next:
writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(String.format("%s/%s.txt", filepath, filename)), charsetName
)
);
Since ISO-8859-1 covers ® but not ™, you should use a unicode charset such as UTF-8 and set it in both places where you write your content and where you read it.
Currently, my property file output looks like this
Name=Frank
Email=frank#mail.com
based on the following codes
File prop = new File(".properties");
if (!prop.exists()) {
prop.createNewFile();
}
try {
FileReader reader = new FileReader(prop);
Properties properties = new Properties();
properties.load(reader);
reader.close();
properties.setProperty("Name", name);
properties.setProperty("Email", email);
FileWriter writer = new FileWriter(prop);
properties.store(writer, "Settings");
writer.close();
} catch (IOException ex) {
Logger.getLogger(PropertiesTest.class.getName()).log(Level.SEVERE, null, ex);
}
I want it to be written in one line, with each key value pair separated by a comma. So it will look like this
Name=Frank, Email=frank#mail.com
How can I achieve this? Or is there a way to group each Name and Email key value pairs with a unique identifier?
Basically, I want to have multiple Name and Email entries in one property file and to be able to get each of the entry.
The properties file is not designed to do this. You should instead use CSV, JSON, YAML, XML (but probably XML is too complicated for this use case) or a database.
boolean valid = false;
String user = txtUser.getText();
String pass = txtPass.getText();
try {
PrintWriter writer = new PrintWriter("src/file");
writer.println("The line");
writer.println(user + "#" + pass);
JOptionPane.showMessageDialog(null,"Sign Up"complete",JOptionPane.INFORMATION_MESSAGE);
writer.close();
} catch(Exception e) {
}
I am making a sign up page and I already have made the login page. The # in the code is used to separate the username to the password. Everything works fine but the problem with this is that every time I sign up it replaces the sign up information I gave the previous time. So if I signed up the first time with the username "greg" and the password "877" it works fine but then if I go on program again and sign up another user with a different username and password, it replaces the first username and pass. I need it to go to new line after every time someone signs up.
Wrap your file with a FileWriter first:
PrintWriter writer = new PrintWriter(new FileWriter("src/file", true));
Here's the description for the constructor of FileWriter(String, boolean):
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
Parameters
fileName - String The system-dependent filename.
append - boolean if true, then data will be written to the end of the file rather than the beginning
you are using public PrintWriter(File file) to write to the file
The javadoc says -
parameter specifies the file to use as the destination of this writer. If the file
exists then it will be truncated to zero size; otherwise, a new file will be created.
The output will be written to the file and is buffered.
So in your case you need to append text to the contents of the existing file so as Luiggi said FileWriter is your friend
FileWriter, a character stream to write characters to file. By default, it will
replace all the existing content with new content, however, when you specified a
true (boolean) value as the second argument in FileWriter constructor, it will keep
the existing content and append the new content in the end of the file.
try in this way
PrintWriter outputFile = new PrintWriter(new FileWriter("src/file", true));
I have a Java class to write/append into existing properties file. After appending, it's replacing all single backslash with double backslash and it places single backslash before every semicolon.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out= response.getWriter();
String systemPath=request.getParameter("SYSTEMPATH");
String deployPath = getServletConfig().getServletContext().getRealPath("/WEB-INF/DB.properties");
InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/DB.properties");
Properties prop = new Properties();
prop.load(stream);
prop.setProperty("Workspace", systemPath);
File file = new File(deployPath);
FileOutputStream fileOut = new FileOutputStream(file);
prop.store(fileOut, "sample properties");
fileOut.close();
}
Before appending:
Url=jdbc:oracle:thin:#//192.168.1.22:1521/
Workspace=D:\RACHEL\SW\Antivirus
after appending:
Url=jdbc:oracle:thin:#//192.168.1.22:1521/
Workspace=D:\\RACHEL\\SW\\Antivirus
How to remove these extra backslashes?
The properties file should have the extra backslashes to start with. In particular, without them you could end up with the wrong data, e.g. if you have d:\foo\new that wouldn't mean what you expect it to.
The backslashes escape characters which are sensitive in properties files, basically. The colons are unnecessary (as they're not in the key) but they don't do any harm either. The doubling of backslashes for text is entirely beneficial.
This is documented in the Properties documentation - in particular, look at the store() method that you're calling.
Properties file has their own format. Colon and backslashes are special characters in properties file. So, they have to be escaped. Also take a look at Properties.load() documentation.
If you are using Properties class to write and read the file, there won't be any problem. But, if you write the properties file using Property class, and read using some other method, then you would have to handle the escapes manually.
you can retrieve the key and its value and check and it will not vary but in properties file
It seems to look with extra slashes
I had this same problem, as I was on stackoverflow on another issue. I remember had this code!
I hope it helps, at least it is java, but it does escaping an issue in Properties file backslash and semicolon pitfall.
// load to store prop
#SuppressWarnings ( "resource" )
PrintWriter pw = new PrintWriter( configFile );
pw.println( "#" + LocalDateTime.now() );
pw.println( "hibernate.connection.username=" + prop.getProperty( "hibernate.connection.username" ) );
pw.println( "hibernate.connection.password=" + prop.getProperty( "hibernate.connection.password" ) );
pw.println( "hibernate.connection.url=" + prop.getProperty( "hibernate.connection.url" ) );
pw.close();