How to read a properties file
hi
I have a properties file.
I am using load method to load the properties file but i have encountered a problem here.
the properties file has space like
FIRST PROP=xyz
SECOND PROP=abc
my question is how to read the file
thank u
Either format it properly--I think a \ before the space will work--or write your own method to use in place of Properties.load.
(Or, I suppose, you could inject your own InputStram that translates the lines on the fly to include the \, but that would probably be more trouble than it's worth.)
Similar Messages
-
How to read the properties file available in Server File structure in webdy
hi all,
I have developed one webdynpro application. In this application i need to access mdm server to continue. For getting the connection i need to pass the IP addresses.
Can i have code how to read the properties file which is residing in the server file. with out included along with the application. keeping some where in the file structure in the server. I want to read that properties file by maintain the iP addresses and users in properties file based on the key i want to read like below.
servername="abcServer"
username="john"
password="test123"
Please send me the code how to read this properties file from the file structure and how to read this values by key in webdynpro application
Regards
VijayHi Vijay,
You can try this piece of code too:
Properties props = new Properties();
//try retrieve data from file
//catch exception in case properties file does not exist
try {
props.load(new FileInputStream("c:\property\Test.properties")); //File location
String serverName = props.getProperty("servername"); //Similarly, you can access the other properties
if(serverName==null)
//....do appropriate handling
} catch(IOException e)
e.printStackTrace();
Regards,
Alka. -
How to read from properties file
Hi,
I am using JSR 168.
while creating a new portlet, a folder gets created with the name as "portlet". Under which is resource package and <PortletName>Bundle.java.
pls tell me how to read from .properties file.
waiting eagerly for some reply
Thanks & Regards,
HP
Edited by: user9003827 on Apr 13, 2010 3:42 AMI think i have mixed it up :)
I have looked at it again and believe you are using regular JSP portlets.
Can you tell what you want to achieve by reading .properties file. Are you meaning the preferences of the portlet or what exactly are you trying to do?
Reading propertie files is easy:
// Read properties file.
Properties properties = new Properties();
try {
properties.load(new FileInputStream("filename.properties"));
String myKey = properties.getProperty("yourKey");
} catch (IOException e) {
}Edited by: Yannick.O on 13-Apr-2010 05:52 -
How to read waveset.properties
Hi group
Please let me know how to read waveset.properties file for finding SOURCES.RESOURCENAME.HOSTS where I ll have different host name to get connected to Authoritative Resource.
Please Help me.
Thanks in advance.
Regards
Gajendra NagapurkarHi,
You can invoke static java method getProperty on class com.waveset.util.WavesetProperties.
Argument is name of your property. -
How do I read a properties file in WEB-INF without hard-coding a path?
Hello,
How do I read a properties file in WEB-INF without hard-coding a path?
I tried:
Properties properties = new Properties();
properties.load(new FileInputStream("db.properties"));
driver = properties.getProperty("driver");
but it cannot find the db.properties file.
Thanks for the help.
FrankDon't use a File to read those properties.
Better to use the servlet context and
getResourceAsStream() method to get the InputStream.
It'll look for any file in the CLASSPATH. If you put
that properties file in the WEB-INF/classes directory
you'll have no problems, even if you deploy with a
WAR file.Completely agree with this approach. Just have to mention the following for completeness
according to the API,
"This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader. "
So using this method, the resource can be anywhere under your web context, not just in the classpath.
Cheers,
evnafets -
Reading a properties file in a servlet and passing its contents to a JSP pa
Hi all,
I'm totally new to Servlet pgmg n JSP. Please can ne1 tell how to read a simple properties file (in a servlet) and pass its contents to a JSP page.Actually the reason is to fill a combo box in a JSP page with the contents of a properties file.If calling a servlet for the same is d best way to do that, plz can ne1 tell me :
1> whether to override the doPost method in the servlet in which to read d file using FileInputStream
2> Putting the contents in an array and then how to pass this array to the JSP page.
Thanks,
deepthy.I'm using a properties file to let my web application know what the name of the database is. I'm using an abstract class GeneralDao which will be extended by all the DAO's (Data Access Objects or java classes containing SQL statements).
Here's the code :
protected Connection getDatabaseConnection()
throws Melding
Connection dbconn = null;
ResourceBundle resBundle;
try
Class.forName("com.mysql.jdbc.Driver").newInstance();
resBundle = ResourceBundle.getBundle("gcoConfig");
String dbConnectie = resBundle.getString("databaseconnection");
gcoLogger.debug("lezen databaseconnection in resourceBundle " );
dbconn = DriverManager.getConnection(dbConnectie);
} catch (InstantiationException exc)The ResourceBundle is used to read the properties file, named gcoConfig.properties.
With the getString() command I read the content of the string named databaseconnection.
The gcoConfig.properties file is placed inside the folder C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\gco\WEB-INF\classes
The GeneralDao is placed in the folder C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\gco\WEB-INF\classes\org\gertcuppens\cluifDao.
So this class is placed some folder below the WEB-INF\classes folder of gcoConfig.properties.
And the content of the properties file is just one line :
databaseconnection=jdbc:mysql://localhost/cluif
If you want to let the JSP know what's inside the properties file, wrap the content in a Javabean, put it in the request or even the session and pass the control to the JSP. -
Is this how to use a properties file?
Hi:
The following is how I intend to create and read a properties file, please verify for me that the idea is correct.
1. create myProp.properties
2. in myProp, the content is like such:
a=myA
b=myB
c=myC
3. In my application,
FileInputStream in = new FileInputStream(getClass().getResourceAsStream("/myProp.properties"));
Properties p = new Properties();
p.load(in);
String a = p.getProperty("a");
String b = p.getProperty("b");
String c = p.getProperty("c");Thanx!It depends on what youare doing but if you have a set of properties that you are going to always use from one running of the application to the next, they should generally be defined as static finals.
For example, lets say you want a configuration file for an application that holds the user's preferences. You might want something like this:
public class Preferences {
private static final String ALWAYS_ON_TOP = "always on top";
private Properities preferences = new Properties();
static {
//load the file
public boolean setAlwaysOnTop(boolean alwaysOnTop) {
preferences.put(ALWAYS_ON_TOP, String.valueOf(alwaysOnTop));
public boolean getAlwaysOnTop() {
return Boolean.valueOf(preferences.get(ALWAYS_ON_TOP));
public void save() {
/save file
} -
How to read several text files at a time
Dear all
Read and write one text file is not a problem, but what confusies me is how to read several text files at one time, in the meanwhile,
is it possible to display the name of the text file?
For example, assuming I want to load file" cha 1, cha 2 , cha 3, " at one time and show their names, how to hadle with it
I have reviewed some files and it is not helpfulEither with a 'for' loop like in the lib you have attached, or like this attached VI
that's it
Message Edited by devchander on 05-30-2006 05:11 AM
Attachments:
MULTIPLE READ.vi 44 KB -
How to read multiple dat files.
Hello Everyone!
I am working on a project that requires one file that creates a JFrame with a current file title, JTextFields that accesses two .dat files and a JButton.
The user clicks a JButton to cycle through the first sequential .dat file. When the end of the first file is reached the file is closed, the JFrame title is changed to the second file title as the user continues to click the JButton to continue on viewing the second file?s records, until the second reaches the end of file.
The project requires using Try-Catches to catch the EOF exceptions and IOExceptions and Thread or Runnable.
I have been able to get one file to read and display its records; however, researching back through my text on how to read/write to files I haven?t been able to determine how to get to the end of the file and proceed to the next file without triggering the EOFException. I have even tried multiple Try-Catches (one for each file within the actionPerformed method) and that ends up ignoring the first file records and only displays the second files records.
This is a school project and we have only covered just the bare basics of Java over the last two months. So, any hints that anyone can provide can only be of what type of procedure will be needed or what procedure won?t help to complete the task, without giving away the solution.
I have spent approximately 40 hours of study time on this project and believe that I have definitely run into a major snag.
The following is the code I have so far:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StudentRead extends JFrame implements ActionListener
private JLabel gradStudentList = new JLabel("GRADUATE Student List");
private JLabel undergradStudentList = new JLabel("UNDERGRADUATE Student List");
private Font bigFont = new Font("Helvetica", Font.ITALIC, 24);
private JLabel userprompt = new JLabel("View the students");
private JTextField idNumText = new JTextField(4);
private JTextField lastNameText = new JTextField(15);
private JTextField firstNameText = new JTextField(15);
private JButton viewRecordButton = new JButton("View Record");
private JLabel idNumberLabel = new JLabel("ID Number");
private JLabel lastNameLabel = new JLabel("Last name");
private JLabel firstNameLabel = new JLabel("First name");
private Container con = getContentPane();
DataInputStream gradStudentInStream;
DataInputStream undergradStudentInStream;
public StudentRead()
super("Read Student Records");
try
gradStudentInStream = new DataInputStream(new FileInputStream("GradStudents.dat"));
undergradStudentInStream = new DataInputStream(new FileInputStream("UndergradStudents.dat"));
catch(IOException e)
System.err.println("File not opened");
System.exit(1);
setSize(325, 200);
con.setLayout(new FlowLayout());
gradStudentList.setFont(bigFont);
con.add(gradStudentList);
con.add(userprompt);
con.add(idNumberLabel);
con.add(idNumText);
con.add(lastNameLabel);
con.add(lastNameText);
con.add(firstNameLabel);
con.add(firstNameText);
con.add(viewRecordButton);
viewRecordButton.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public void actionPerformed(ActionEvent e1)
String lastName, firstName;
int IdNum;
try
IdNum = gradStudentInStream.readInt();
lastName = gradStudentInStream.readUTF();
firstName = gradStudentInStream.readUTF();
idNumText.setText(String.valueOf(IdNum));
lastNameText.setText(lastName);
firstNameText.setText(firstName);
catch(EOFException e2)
closeFile();
System.exit(0);
catch(IOException e3)
System.err.println("Error reading file");
System.out.println("out");
System.exit(1);
public void closeFile()
try
gradStudentInStream.close();
System.exit(0);
catch(IOException e)
System.err.println("Error closing file");
System.exit(1);
public static void main(String[] args)
StudentRead rsr = new StudentRead();
}deepak_1your.com wrote:
hi,
If you want to read a file guarding yourself agianst exceptions....
check this article.... the code presented in this article might suit your needs...
[http://1your.com/fusion/infusions/articles/readarticle.php?article_id=17|http://1your.com/fusion/infusions/articles/readarticle.php?article_id=17]
And how does that help with a DataInputStream? -
How to read an xml file from headers
Hi ,
I am not getting how to read an xml file sent by client device in header to server.
Thankx.There is a getHeader() in HttpServletRequest interface
String locationURL=request.getHeader("Location");If URL of your file was set in Location attribute of header.
Edited by: ngpgeeta on Dec 19, 2008 8:03 AM -
How to read a text file using Java
Guys,
Good day!
Please help me how to read a text file using Java and create/convert that text file into XML.
Thanks and God Bless.
Regards,
I-Talkpublic void fileRead(){
File aFile =new File("myFile.txt");
BufferedReader input = null;
try {
input = new BufferedReader( new FileReader(aFile) );
String line = null;
while (( line = input.readLine()) != null){
catch (FileNotFoundException ex) {
ex.printStackTrace();
catch (IOException ex){
ex.printStackTrace();
}This code is to read a text file. But there is no such thing that will convert your text file to xml file. You have to have a defined XML format. Then you can read your data from text files and insert them inside your xml text. Or you may like to read xml tags from text files and insert your own data. The file format of .txt and .xml is far too different.
cheers
Mohammed Jubaer Arif. -
How to Read a CAB File from JAVA?
Hi,
Anyone knows how to read a CAB File from java. I need to read a property file of txt file that is packaged in CAB file & then based on that, I have to do processing. Is there anyway to do it.
I had tried using java.util.zip.ZipFile Class, but it does work for JAR, but not for CAB.
siva.Perhaps there's something in the Cabinet SDK that will help:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncabsdk/html/cabdl.asp -
How to read a text file through pl/sql
How to read a text file through pl/sql
pl/sql runs inside the database. so your file also should be on the database server file system for you to be able to read.
check out UTL_FILE package. This is the database package to read/write files on the database server. -
How to read an excel file in webdynpro application
Hello Experts,
Can someone please tell me how to read an excel file in a webdynpro application?
There is a tutorial for how to write contect into an excel, but i want to read the excel.
Can someone help please !!
Thanks and Kind regards,
G.Singh.Hello Experts,
I have done all the given above.
I want to read a excel file from KM. My code is as below
ResourceContext resourceContext = buildResourceContext();
IResourceFactory resourceFactory = ResourceFactory.getInstance();
RID pathRID = RID.getRID("/documents/ExcelReport.xls");
IResource resource = resourceFactory.getResource(pathRID, resourceContext);
Workbook wb = Workbook.getWorkbook(resource.getURI().getPath());
Sheet sh = wb.getSheet(0);
int columns = sh.getColumns();
int rows = sh.getRows();
wdComponentAPI.getMessageManager().reportSuccess(" Rows: " + rows);
wdComponentAPI.getMessageManager().reportSuccess(" Columns: " + columns);
This does not give me the excel file form the KM
Can you please just what i can do at this point?
Kind Regards,
G Singh. -
Java.lang.NullPointerException in the code to read a properties file
I have written the following code to read a properties file...........
<b>
public Property(String fileName)
try
InputStream is = MyClassName.class.getClass().getClassLoader().getResourceAsStream(fileName);
prop.load(is);
is.close();
catch (Exception ex)
System.out.println("The Exception is: " + ex.getMessage());
ex.printStackTrace();
</b>
But this code is giving <b> java.lang.NullPointerException </b> at <b> prop.load(is) </b>
please help me....
regards
Brahmachaitanyahi,
If you are loading a resource (property) file from a jar file you can use
getResourceAsStream("com/test/test.properties").
if the propery file is in outside of your jar file . you can use
Property portalProps = new Property ();
try {
FileInputStream fileInputStream = new FileInputStream("<FilePath + FileName>");
portalProps.load(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
//Log the error
refer the following link for more details
http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
Maybe you are looking for
-
Is there any way to speak to someone at BT with te...
After a ridiculous hour and a half long patience-testing chat with a BT customer support representative who contradicted himself and couldn't answer any questions, followed by his manager who "saw what was going on" and "fixed" the issue by (it seems
-
i can'use firefox automatically,in my computer i have got write arima in my start autimatically.what's the meaning?since 22june i got message mozilla firefox 3.6.4:executable files may contain viruses or other malicious code that could harm your comp
-
The dvd menus does not accept custom background images. I can place images in the dvd menu, but when the dvd is rendered all images are gone. What is wrong with this software? This problem has been written by others, but it seems to be no solution to
-
I look up order number but nothing coming up!!
I looked up the order number, checked out as a guest, {removed per forum guidelines}, email is {removed per forum guidelines}. I did not recieve confirmation nor do i know the status of this order number.
-
Hi Experts, I am new to sap domain.i hv done training in abap. i want to know about the career options in sap can u tell me which is good functional tester, devoloper, consultant. which one is good and interesting for career