Stets to use a properties files in Java.
1. At first create a properties files and save it in a UTF-8 format.
2. Then Make an object of Properties class.
3. After that load the properties file into the program.
4. Finally get the text using the key of the properties file.
An example of using the properties file and changing the caption French language is given below. To run the given example please save the properties file into the c directory of your computer.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyWindows extends JFrame {
public MyWindows() {
}
public static void main(String args[]) {
MyWindows myWindows = new MyWindows();
final Properties myresources = new Properties();
try {
FileInputStream in = new FileInputStream(
"C:\\myResources.properties");
try {
myresources.load(in);
System.out.println("Properties File Loaded");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
final JFrame myFrame = new JFrame("My Windows");
myFrame.setSize(600, 400);
myFrame.setLayout(new FlowLayout());
final JButton testButton = new JButton("Change Laguage");
final JLabel testLabel = new JLabel("Hello Friends");
myFrame.add(testLabel);
myFrame.add(testButton);
myFrame.setVisible(true);
testButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
myFrame.setTitle(myresources.getProperty("label.windows"));
testLabel.setText(myresources.getProperty("label.text"));
testButton.setText(myresources.getProperty("button.text"));
}
});
}
}
|
|
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java Properties File Example
Post your Comment