According to the Documentation,
java.io.FileDescriptor works for opening a file having a specific name. If there is any content present in that file it will first erase
all that content and put “Beginning of Process” as the first line.
My FILE.txt file contains content as my name 'Jaimin Modi' only.
Now, below is one sample for the use of FileDescriptor class in java:
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// Initializing a FileDescriptor
FileDescriptor geek_descriptor = null;
FileOutputStream geek_out = null;
// HERE I'm writing "GEEKS" in my file
byte[] buffer = {71,69,69,75,83};
try{
geek_out = new FileOutputStream("FILE.txt");
// This getFD() method is called before closing the output stream
geek_descriptor = geek_out.getFD();
// writes byte to file output stream
geek_out.write(buffer);
// USe of sync() : to sync data to the source file
geek_descriptor.sync();
System.out.print("\nUse of Sync Successful ");
}
catch(Exception excpt)
{
// if in case IO error occurs
excpt.printStackTrace();
}
finally
{
// releases system resources
if(geek_out!=null)
geek_out.close();
}
}
}
Am getting below output/content in FILE.txt as 'GEEKS'.
So, What I have done next is just commented the lines for the use of FileDescriptor.
Commented below lines :
FileDescriptor geek_descriptor = null;
geek_descriptor = geek_out.getFD();
geek_descriptor.sync();
and again changed content in FILE.txt as 'Jaimin Modi'.
then just executed. What am getting in FILE.txt :
'GEEKS'..!!
Am getting the same result with and without the use of FileDescriptor.
So, What is the main use of FileDescriptor here. Confused a little. Please guide.
Hi I am trying to put < sign in java properties file, so that it will show message to user as ' < symbol is not accepted ' but I am not able to put it in the file.
I tried /</, \<\, and \\<... but it did not work.
The Properties text format has no problem with either <s or >s. The exact specification of the file format is laid out in Properties.load() and makes no mention of either character. This is also very easy to verify:
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.put("<key>", "<value>");
System.out.println("Properties Contents: " + prop);
StringWriter writer = new StringWriter();
prop.store(writer, "<comment>");
System.out.println("\nProperties File Format:\n" + writer);
prop = new Properties();
prop.load(new StringReader(writer.toString()));
System.out.println("Properties Contents: " + prop);
}
First we construct a Properties object and add a <key>:<value> pair. Then we write the object to a Writer and print the serialized contents, then we load those contents back into a new Properties object and can see there was no data loss. This program outputs:
Properties Contents: {<key>=<value>}
Properties File Format:
#<comment>
#Sat Jul 23 23:50:50 EDT 2016
<key>=<value>
Properties Contents: {<key>=<value>}
You should be able to load a properties file with such symbols without issue.
If you're using the XML file format (via loadFromXML() and storeToXML()) you need to escape < and > just like you would in any XML document. If you are trying to read/write XML it would have been very helpful to mention that in your question.
I would like to use a function to read from a properties file and than use the object of that file at various places.here is what I have tried so far with no luck.
public class all_the_functions
{
public FileInputStream loadPropertiesFile(FileInputStream obj2) throws IOException
{
//reading from the properties file
Properties obj = new Properties();
FileInputStream fileobj = new FileInputStream("//Users//macuser//Desktop//selenium//project_Mat//input_properties.properties");
obj.load(fileobj);
return fileobj;
}
}
And than in my main function I am using the following code
public class searchdynamic();
{
FileInputStream Obj;
all_the_functions func = new all_the_functions();
func.loadPropertiesFile(Obj);
WebDriver driver = new FirefoxDriver();
driver.navigate().to(Obj.getproperty("valid URL");
}
The end goal is to read from. The input file by using the function and stroung the properties file so that I can always call the same function when I need to read from the file. Can someone please point me to what I am doing wrong here.
Thanks.
Can u try this
public class searchdynamic();
{
FileInputStream Obj;
all_the_functions func = new all_the_functions();
Obj=func.loadPropertiesFile(Obj);
WebDriver driver = new FirefoxDriver();
driver.navigate().to(Obj.getproperty("valid URL");
}
What is the string that your property is returning? "http://www..."
In theory, this should work (although I'm used to webdriver being navigate().GoToUrl(string url). Give that a shot...
Also, friendly warning about this being not oop or compiling. Might be a good idea to tie your properties to values somehow. I understand this is a keyword driven framework, but it is better practice to have a user input "Google" and have your code digest that into something like
driver.Navigate().GoToUrl("http://google.com"); // (c#)
This way, you can count on correct input and handle incorrect input appropriately.
Regardless, try .Navigate().GoToUrl()
# Compulsory Dimension to create port
xOffset=-3
yOffset=50
How to get these xOffset and YOffset in java file.I tried with inputstream but not getting.These variable should get loaded in java file.
You can use Properties class from Java library
Properties prop = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(inputStream);
The you can get the values as
prop.getProperty("propertyname");
Try the following code:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("your_config.properties");
prop.load(input);
System.out.println(prop.getProperty("xOffset"));
System.out.println(prop.getProperty("yOffset"));
} catch (IOException e) {
// ...
}
As above explained Create a Function to read the property File During or Before Selenium Driver Constructor . So you can use them in test ( Help in Desire Capability Impl).
Store the values in Public Static final ( If you don not want to change them in Selenium and use as Default Property input)
As the values are read by Java Propertie file or .config file before selenium Driver so you can use them in Driver constructor or if you don't want you can use those properties stored as Static anywhere in the project. These values act as GLOBAL param.
I have a config file, named config.txt, look like this.
IP=192.168.1.145
PORT=10022
URL=http://www.stackoverflow.com
I wanna change some value of the config file in Java, say the port to 10045. How can I achieve easily?
IP=192.168.1.145
PORT=10045
URL=http://www.stackoverflow.com
In my trial, i need to write lots of code to read every line, to find the PORT, delete the original 10022, and then rewrite 10045. my code is dummy and hard to read. Is there any convenient way in java?
Thanks a lot !
If you want something short you can use this.
public static void changeProperty(String filename, String key, String value) throws IOException {
Properties prop =new Properties();
prop.load(new FileInputStream(filename));
prop.setProperty(key, value);
prop.store(new FileOutputStream(filename),null);
}
Unfortunately it doesn't preserve the order or fields or any comments.
If you want to preserve order, reading a line at a time isn't so bad.
This untested code would keep comments, blank lines and order. It won't handle multi-line values.
public static void changeProperty(String filename, String key, String value) throws IOException {
final File tmpFile = new File(filename + ".tmp");
final File file = new File(filename);
PrintWriter pw = new PrintWriter(tmpFile);
BufferedReader br = new BufferedReader(new FileReader(file));
boolean found = false;
final String toAdd = key + '=' + value;
for (String line; (line = br.readLine()) != null; ) {
if (line.startsWith(key + '=')) {
line = toAdd;
found = true;
}
pw.println(line);
}
if (!found)
pw.println(toAdd);
br.close();
pw.close();
tmpFile.renameTo(file);
}
My suggestion would be to read the entire config file into memory (maybe into a list of (attribute:value) pair objects), do whatever processing you need to do (and consequently make any changes), then overwrite the original file with all the changes you have made.
For example, you could read the config file you have provided by line, use String.split("=") to separate the attribute:value pairs - making sure to name each pair read accordingly. Then make whatever changes you need, iterate over the pairs you have read in (and possibly modified), writing them back out to the file.
Of course, this approach would work best if you had a relatively small number of lines in your config file, that you can definitely know the format for.
this code work for me.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
public void setProperties( String key, String value) throws IOException {
Properties prop = new Properties();
FileInputStream ip;
try {
ip = new FileInputStream("config.txt");
prop.load(ip);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
prop.setProperty(key, value);
PrintWriter pw = new PrintWriter("config.txt");
prop.store(pw, null);
}
Use the Properties class to load/save configuration. Then simply set the value and save it again.
Properties p = new Properties();
p.load(...);
p.put("key", "value");
p.save(...)
It's easy and straightforward.
As a side, if your application is a single application that does not need to scale to run on multiple computers, do not bother to use a database to save config. It is utter overkill. However, if you application needs real time config changes and needs to scale, Redis works pretty well to distribute config and handle the synchronization for you. I have used it for this purpose with great success.
Consider using java.util.Properties and it's load() and store() methods.
But remember that this would not preserve comments and extra line breaks in the file.
Also certain chars need to be escaped.
If you are open to use third party libraries, explore http://commons.apache.org/configuration/. It supports configurations in multiple format. Comments will be preserved as well. (Except for a minor bug -- apache-commons-config PropertiesConfiguration: comments after last property is lost)