📄 properties.java
字号:
package org.net9.oops.jsee;
/**
* Neo Maohesong 2002-03-05
*/
import java.io.*;
/**
* A class set properties to the user's home directory and
* get properties from it
* @version created 2002-03-05 last modified 2002-03-09
* @author Neoya M <neoyaa@yahoo.com>
*/
public class Properties{
//Property names
public static final String SMALL_SPANE= "SMALL_SPANE";
public static final String SPANE= "SPANE";
public static final String COLUMN_LAYOUT= "COLUMN_LAYOUT";
public static final String SCALE_QUALITY= "SCALE_QUALITY";
public static final String VIEW_FULL_SCREEN= "VIEW_FULL_SCREEN";
public static final String AUTO_SIZE_PREVIEW= "AUTO_SIZE_PREVIEW";
public static final String HIDE_PREVIEW= "HIDE_PREVIEW";
public static final String THUMBNAIL_SHOW= "THUMBNAIL_SHOW";
public static final String SAVE_PREVIOUS_OPTION= "SAVE_PREVIOUS_OPTION";
public static final String PATH= "PATH";
public static final String WIDTH= "WIDTH";
public static final String HEIGHT= "HEIGHT";
public static final String SHOW_COLUMN_SIZE= "Size";
public static final String SHOW_COLUMN_DATE= "Date";
public static final String SHOW_COLUMN_TYPE= "Type";
public static final String SHOW_COLUMN_PROPERTIES= "Properties";
//Property values
public static final int COLUMN_LAYOUT_PREVIEW= 1;
public static final int COLUMN_LAYOUT_LIST= 2;
private BufferedReader reader;
private BufferedWriter writer;
private String filename;
Properties(){
String userHomeDir = System.getProperty( "user.home" );
filename=userHomeDir + "/" + ".JCDSee.cfg";
}
/**
* Get the property by name
* Read lines throw the Properties file until a matched name
*/
public String getProperty(String name){
try{
reader=new BufferedReader(
new FileReader(filename));
String s;
while(!((s=reader.readLine())==null)){
if (s.startsWith(name))
return s.substring(name.length()+3);
}
reader.close();
return "";
}catch(FileNotFoundException e){
System.out.println("Property File Not Found,Use Default Value");
return "";
}catch(IOException e){
e.printStackTrace();
return "";
}catch(NullPointerException e){
return "";
}
}
public int getIntProperty(String name){
String s=getProperty(name);
try{
if (s==null || s.equals(""))
return 0;
else
return Integer.parseInt(s);
}catch(NumberFormatException e){
System.out.println("NumberFormatException at getIntProperty()");
return 0;
}
}
public boolean getBooleanProperty(String name){
String s=getProperty(name);
try{
if (s==null || s.equals("")) {
return true;//Default true
}
else {
return Boolean.valueOf(s).booleanValue();
}
}catch(NumberFormatException e){
System.out.println("NumberFormatException at getBooleanProperty()");
return false;
}
}
/**
* Set the property to the file by name
* First read the whole properties files and replace the new preperty by name
* Second write the replaced string to the file
*/
public void setProperty(String name,String value){
StringBuffer sb=new StringBuffer();
boolean exist=false;
try{
reader=new BufferedReader(
new FileReader(filename));
String s;
while(!((s=reader.readLine())==null)){
if (s.startsWith(name)){
sb.append(name+" = "+value+"\n");
exist=true;
}else
sb.append(s+"\n");
}
reader.close();
}catch(FileNotFoundException e){
System.out.println("Property File Not Found,Use Default Value");
}catch(IOException e){
e.printStackTrace();
}catch(NullPointerException e){
}
//The file may not exist,so put these codes here not in the above brock
if(exist==false)
sb.append(name+" = "+value);
try{
writer=new BufferedWriter(
new FileWriter(filename));
writer.write(sb.toString());
writer.close();
}catch(IOException e){
e.printStackTrace();
}
}
/**
* Overloaded method
* @param int value
*/
public void setProperty(String name,int value){
String s=String.valueOf(value);
this.setProperty(name,s);
}
/**
* Overloaded method
* @param boolean value
*/
public void setProperty(String name,boolean value){
String s=value?"true":"false";
this.setProperty(name,s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -