最笨最实在思路最全面的做法,用于最基础的代码锻炼非常有效,推荐给大家打打java底^_^
package
cn.net.withub.xfgl.common;
/***/ /**
*@authorYuLuo
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
// 前一篇已介绍此方法
import cn.net.withub.xfgl.common.ToUnicode;
public class CreatePro {
//读文件
publicstaticStringreadFile(StringfilePath,StringparameterName)throwsIOException,FileNotFoundException{
Stringresult=null;
Filefile=newFile(filePath);
if(file.exists()){
FileInputStreamfis=newFileInputStream(file);
byte[]b=newbyte[fis.available()];
fis.read(b);
result=newString(b,"UTF-8");
fis.close();
}
returnresult;
}
//修改后存储
publicstaticvoidsaveFile(Stringcontent,Stringpath,StringfileName)throwsIOException{
Filef=newFile(path);
if(!f.exists()){
f.mkdirs();
}
Filefn=newFile(f,fileName);
FileOutputStreamfos=newFileOutputStream(fn);
fos.write(content.getBytes());
fos.close();
}
//删除旧文件
publicstaticvoiddeleteFile(Stringpath)throwsIOException{
Filef=newFile(path);
if(f.exists()){
f.delete();
}else{
thrownewIOException("未找到相关文件");
}
}
//执行方法
publicstaticbooleanwriteProperties(StringfilePath,StringparameterName,StringparameterValue){
booleanflag=false;
try{
//取得文件所有内容
Stringall=CreatePro.readFile(filePath,parameterName);
Stringresult=null;
//如果配置文件里存在parameterName
if(all.indexOf(parameterName)!=-1){
//得到parameterName前的字节数
inta=all.indexOf(parameterName);
//取得以前parameterName的值
Stringold=readProperties(filePath,parameterName);
//得到parameterName值前的字节数
intb=a+(parameterName.length()+"=".length());
//新的properties文件所有内容为:旧的properties文件中内容parameterName+"="+新的parameterName值(parameterValue)+旧的properties文件中parameterName值之后的内容
result=all.substring(0,a)+parameterName+"="+parameterValue+all.substring(b+ToUnicode.convert(old).length(),all.length());
}
//删除以前的properties文件
CreatePro.deleteFile(filePath);
//存储新文件到以前位置
String[]arrPath=filePath.split("WEB-INF");
Stringpath=arrPath[0]+"WEB-INF\\configs";
CreatePro.saveFile(result,path,"xf.properties");
flag=true;
}catch(IOExceptione){
e.printStackTrace();
flag=false;
}
returnflag;
}
//Properties方法写properties文件
/**//*Propertiesprop=newProperties();
try{
InputStreamfis=new
FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)prop.load(fis);
//调用Hashtable的方法put。使用getProperty方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是Hashtable调用put的结果。
OutputStreamfos=newFileOutputStream(filePath);
prop.setProperty(parameterName,parameterValue);
//以适合使用load方法加载到Properties表中的格式,
//将此Properties表中的属性列表(键和元素对)写入输出流
prop.store(fos,"Update'"+parameterName+"'value\n");
falg=true;
}catch(IOExceptione){
e.printStackTrace();
System.err.println("Visit"+filePath+"forupdating"+parameterName+"valueerror");
falg=false;
}*/
//读properties文件,Properties方法
publicstaticStringreadProperties(StringfilePath,StringparameterName){
Stringvalue="";
Propertiesprop=newProperties();
try{
InputStreamfis=newFileInputStream(filePath);
prop.load(fis);
value=prop.getProperty(parameterName);
}catch(IOExceptione){
e.printStackTrace();
}
returnvalue;
}
/***/ /**
*@authorYuLuo
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
// 前一篇已介绍此方法
import cn.net.withub.xfgl.common.ToUnicode;
public class CreatePro {
//读文件
publicstaticStringreadFile(StringfilePath,StringparameterName)throwsIOException,FileNotFoundException{
Stringresult=null;
Filefile=newFile(filePath);
if(file.exists()){
FileInputStreamfis=newFileInputStream(file);
byte[]b=newbyte[fis.available()];
fis.read(b);
result=newString(b,"UTF-8");
fis.close();
}
returnresult;
}
//修改后存储
publicstaticvoidsaveFile(Stringcontent,Stringpath,StringfileName)throwsIOException{
Filef=newFile(path);
if(!f.exists()){
f.mkdirs();
}
Filefn=newFile(f,fileName);
FileOutputStreamfos=newFileOutputStream(fn);
fos.write(content.getBytes());
fos.close();
}
//删除旧文件
publicstaticvoiddeleteFile(Stringpath)throwsIOException{
Filef=newFile(path);
if(f.exists()){
f.delete();
}else{
thrownewIOException("未找到相关文件");
}
}
//执行方法
publicstaticbooleanwriteProperties(StringfilePath,StringparameterName,StringparameterValue){
booleanflag=false;
try{
//取得文件所有内容
Stringall=CreatePro.readFile(filePath,parameterName);
Stringresult=null;
//如果配置文件里存在parameterName
if(all.indexOf(parameterName)!=-1){
//得到parameterName前的字节数
inta=all.indexOf(parameterName);
//取得以前parameterName的值
Stringold=readProperties(filePath,parameterName);
//得到parameterName值前的字节数
intb=a+(parameterName.length()+"=".length());
//新的properties文件所有内容为:旧的properties文件中内容parameterName+"="+新的parameterName值(parameterValue)+旧的properties文件中parameterName值之后的内容
result=all.substring(0,a)+parameterName+"="+parameterValue+all.substring(b+ToUnicode.convert(old).length(),all.length());
}
//删除以前的properties文件
CreatePro.deleteFile(filePath);
//存储新文件到以前位置
String[]arrPath=filePath.split("WEB-INF");
Stringpath=arrPath[0]+"WEB-INF\\configs";
CreatePro.saveFile(result,path,"xf.properties");
flag=true;
}catch(IOExceptione){
e.printStackTrace();
flag=false;
}
returnflag;
}
//Properties方法写properties文件
/**//*Propertiesprop=newProperties();
try{
InputStreamfis=new
FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)prop.load(fis);
//调用Hashtable的方法put。使用getProperty方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是Hashtable调用put的结果。
OutputStreamfos=newFileOutputStream(filePath);
prop.setProperty(parameterName,parameterValue);
//以适合使用load方法加载到Properties表中的格式,
//将此Properties表中的属性列表(键和元素对)写入输出流
prop.store(fos,"Update'"+parameterName+"'value\n");
falg=true;
}catch(IOExceptione){
e.printStackTrace();
System.err.println("Visit"+filePath+"forupdating"+parameterName+"valueerror");
falg=false;
}*/
//读properties文件,Properties方法
publicstaticStringreadProperties(StringfilePath,StringparameterName){
Stringvalue="";
Propertiesprop=newProperties();
try{
InputStreamfis=newFileInputStream(filePath);
prop.load(fis);
value=prop.getProperty(parameterName);
}catch(IOExceptione){
e.printStackTrace();
}
returnvalue;
}