Article View: borland.public.jbuilder.java.language
Article #16993Re: propertie repost
From: Greg Hamer
Date: Wed, 23 Dec 1998 00:00
Date: Wed, 23 Dec 1998 00:00
161 lines
6438 bytes
6438 bytes
For reading a properties file there are easy ways like placing the the
propeties file in a
package with a particular class. Then
particularClass.class.getResourceAsStream("propertyfile.properties") gives you
something to pass to properties.load. But if you also want to be able to write
to the properties file, the only thing I have seen is to use
System.getProperty("java.class.path") and have a scheme of where to expect to
find /write the file.
Here is a routine that we use:
import java.util.StringTokenizer;
import java.io.File;
import java.util.zip.ZipFile;
import java.util.zip.ZipException;
import java.io.IOException;
...
/**
* Return the directory that the Object's class is in + .properties
* For example, if the instance parameter is of type Column , and
borland.jbcl.dataset.Column
* is found in c:\jbuilder\lib\jbcl2.0.jar, then
* this will return c:\jbuilder\lib\Column.properties
*/
public static String getPathToPropertiesFile(Object instance) {
return getPathToPropertiesFile(instance.getClass().getName());
}
public static String getPathToPropertiesFile(String className) {
//System.err.println("getPathtoProperties");
String classpath = System.getProperty("java.class.path"); //NORES
String pathSeparator = System.getProperty("path.separator"); //NORES
StringTokenizer tokenizer = new StringTokenizer(classpath, pathSeparator);
className = className.replace('.', File.separatorChar) ; //NORES
int index = className.lastIndexOf(File.separatorChar);
String classDir = "";
if (index > 0) {
classDir = className.substring(0, index + 1); //NORES
className = className.substring(index + 1);
}
String propertyName = className + ".properties"; //NORES
className = className + ".class"; //NORES
String zipEntry = className;
if (classDir.length() > 0)
zipEntry = classDir + className;
zipEntry = zipEntry.replace('\\','/');
//System.err.println("classDir=" + classDir);
//System.err.println("className=" + className);
//System.err.println("zipEntry=" + zipEntry);
File file;
String testPath;
ZipFile zipFile = null;
while (tokenizer.hasMoreTokens()) {
testPath = tokenizer.nextToken();
// System.out.println("Searching " + testPath);
if ((testPath.length() > 5) &&
((testPath.substring(testPath.length() - 4,
testPath.length()).equalsIgnoreCase(".zip")) || //NORES
(testPath.substring(testPath.length() - 4,
testPath.length()).equalsIgnoreCase(".jar")))) { //NORES
try {
zipFile = null;
zipFile = new ZipFile(testPath);
} catch (ZipException e) {
// System.out.println("Invalid ZIP file(?): " + testPath);
} catch (IOException e) {
// System.out.println("I/O error reading file: " + testPath);
}
if ((zipFile != null) && (zipFile.getEntry(zipEntry) != null)) {
int indexx = testPath.lastIndexOf(File.separatorChar);
if (!(indexx < 0))
testPath = testPath.substring(0,indexx + 1);
//System.out.println("Found: " + testPath + "[" + zipClassName + "]" +
index);
return testPath + propertyName;
}
//System.err.println(zipEntry + " not in " + testPath );
}
else {
if (testPath.charAt(testPath.length() - 1) != File.separatorChar) {
testPath += File.separator;
}
file = new File(testPath + classDir + className);
if (file.exists()) {
// System.out.println("Found: " + testPath + className);
return testPath + classDir + propertyName;
}
//System.err.println("not " + testPath + classDir + className);
}
}
System.err.println("not found"); //NORES
// we should never get here
return null;
}
walter b. wrote:
> I'm writing a program My source path and my class path are
> c:\jbuilder2\myprog\.
> I want save customer's preferences into properties file.
>
> Now I don't want insert source path into file propetrie name (because
> when I deploy my program I install into c:\myprog or other path), but
> without path I get file not found exception.
> How can I solve this problem ?
>
> This is my code:
>
> public class Options extends JFrame {
> Properties prop;
> TextFieldControl textFieldDirArch = new TextFieldControl();
> File f = new File("settaggi.properties");
> /* jave not funds settaggi.properties, But if I write new
> File("c:/jbuilder2/myprog/settaggi.properties") it works. But like
> this I have absolute path
> */
> // etc....
>
> public Options() {
> try {
> jbInit();
>
> if (!f.isFile())
> {
> Msg.warning("Path file "+f.getPath() + " not found");
> // Msg my class messagebox
> return;
> }
>
> try {
> InputStream in = new FileInputStream(f);
> prop = new Properties();
> prop.load(in);
>
> textFieldDirArch.setText(prop.put("dir_archivi","d:/archivi").toString());
> // with this textfield customer can to set archives's path
> in.close();
> }
> catch(Exception eReadProp)
> {Msg.warning(eReadProp.getMessage());}
> }
>
> //....
> void buttonSave_actionPerformed(ActionEvent e) {
> // scrive nel file
> try {
> registra(); // my method for to set properties's keys
> OutputStream out = new FileOutputStream(f);
> prop.save(out,"bo");
> out.flush();
> out.close();
> f = null;
> prop = null;
> new Setta();
> this.dispose();
> }
> catch(Exception eSaveProp) {Msg.warning("errore nella scrittura
> del file di propriet๏ฟฝ "+eSaveProp.getMessage());}
> }
Technical Details
Message-ID:
<3681A8FE.6B399587@inprise.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<367f9882.745284@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f74b3,8dec30e424270b5d X-Google-Attributes: gidf74b3,public From: Greg Hamer <ghamer@inprise.com> Subject: Re: propertie repost Date: 1998/12/23 Message-ID: <3681A8FE.6B399587@inprise.com> X-Deja-AN: 425380027 Content-Transfer-Encoding: 8bit References: <367f9882.745284@forums.borland.com> Content-Type: text/plain; charset=iso-8859-1 Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language