使用工具类Properties
首先将文件写成流(输入)
File file=new File(confPath)
in = new FileInputStream(file)
加载流load(in)如果需要 *** 作则完成具体 *** 作
输出流并保存文件
out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK")
cp.store(out2)
完成
具体实例代码
public String updateConfParam(ConfParam cpl) throws IOException {
String error = null
Properties cp=new Properties()
InputStream in= null
OutputStreamWriter out1=null
OutputStreamWriter out2=null
JSONObject jObj = new JSONObject()
try {
String confPath=validateSystem(cpl.getConfAddress()+"/"+cpl.getConfName())
File file=new File(confPath)
in = new FileInputStream(file)
cp.load(in)
out1=new OutputStreamWriter(new FileOutputStream(confPath+".bak"),"GBK")
cp.store(out1)
cpl.setParaOldValue(cp.getProperty(cpl.getParaKey()))
cp.setProperty(cpl.getParaKey(), cpl.getParaValue())
out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK")
cp.store(out2)
jObj.put("paraOldValue", cpl.getParaOldValue())
jObj.put("paraValue", cpl.getParaValue())
} catch (FileNotFoundException e) {
error=e.getMessage()
} catch (IOException e1) {
error = e1.getMessage()
}
finally{
if(in !=null){
in.close()
}
if(out1 !=null){
out1.close()
}
if(out2 !=null){
out2.close()
}
if(error != null){
jObj.put("error", error)
}
}
return jObj.toString()
}
import java.io.BufferedReaderimport java.io.BufferedWriter
import java.io.FileReader
import java.io.FileWriter
import java.io.IOException
public class Test14 {
public static void main(String[] args) throws IOException {
String fPath = "C:/test.txt"
// 读
BufferedReader br = new BufferedReader(new FileReader(fPath))
System.out.println(br.readLine())
br.close()// // 使用后记得关闭
// 写
BufferedWriter bw = new BufferedWriter(new FileWriter(fPath))
bw.write("写一段话到文件里")
bw.flush()
bw.close()// 使用后记得关闭
}
}
import java.io.BufferedReaderimport java.io.FileReader
import java.io.IOException
class ThreadDemo{
public static void main(String[] args) throws IOException{
BufferedReader fr=new BufferedReader(new FileReader("a.txt"))//a.txt代表文件
String line=null
while((line=fr.readLine())!=null){
System.out.println(line)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)