当前位置: 技术问答>java相关
怎样对一个文件里的内容进行修改???急……
来源: 互联网 发布时间:2015-05-21
本文导语: 怎样对一个文件的内容进行修改,如在文件里找到一字符串,并把该字符串用别的字符串替换掉? | 这是一段代码,如下: //:ModifyFile.java //This class change a *.jsp file // import java.util.StringT...
怎样对一个文件的内容进行修改,如在文件里找到一字符串,并把该字符串用别的字符串替换掉?
|
这是一段代码,如下:
//:ModifyFile.java
//This class change a *.jsp file
//
import java.util.StringTokenizer;
import java.io.*;
public class ModifyFile{
//a file which will be modify
public FileInputStream fisRWF;
public FileOutputStream fosRWF;
//the file name which will be modify
String fileName;
//store the string from the file
String strInputFile;
//store the string to the file
String strOutputFile;
//the string buffer which are used to store the string out to the file
StringBuffer strbfBuffer;
//constructor ,the argument is file
ModifyFile(File mf){
fileName = new String(mf.getName());
strInputFile = "";
}
//constructor ,the argument is string
ModifyFile(String mf){
fileName = new String(mf);
strInputFile = "";
}
//open a input stream of the file called fileName
//init the strInputFile and strbfBuffer
public void getFile()throws Exception{
fisRWF = new FileInputStream(fileName);
byte buffer[] = new byte[80];
String str;
int bytes;
while((bytes=fisRWF.read(buffer,0,80))!=-1){
str = new String(buffer,0,bytes,"Default");
strInputFile = strInputFile+str;
}
strbfBuffer = new StringBuffer(strInputFile);
fisRWF.close();
}
//modify the strbfBuffer and strOutputFile
//argument brp is the string which would be replace
//argument rp is the string which would be used to replace
public void modify(String brp,String rp){
int intBrpFirst;
strInputFile = strbfBuffer.toString();
intBrpFirst = strInputFile.indexOf(brp);
if (intBrpFirst!=-1){
strbfBuffer.replace(intBrpFirst,intBrpFirst+brp.length(),rp);
modify(brp,rp);
}
strOutputFile = new String(strbfBuffer);
}
//output the strOutputFile to the file
//close the file
public void setFile()throws Exception{
int intSOFNum;
intSOFNum = strOutputFile.length();
byte btOutput[] = new byte[intSOFNum];
btOutput = strOutputFile.getBytes();
fosRWF = new FileOutputStream(fileName);
fosRWF.write(btOutput);
fosRWF.close();
}
}
//:ModifyFile.java
//This class change a *.jsp file
//
import java.util.StringTokenizer;
import java.io.*;
public class ModifyFile{
//a file which will be modify
public FileInputStream fisRWF;
public FileOutputStream fosRWF;
//the file name which will be modify
String fileName;
//store the string from the file
String strInputFile;
//store the string to the file
String strOutputFile;
//the string buffer which are used to store the string out to the file
StringBuffer strbfBuffer;
//constructor ,the argument is file
ModifyFile(File mf){
fileName = new String(mf.getName());
strInputFile = "";
}
//constructor ,the argument is string
ModifyFile(String mf){
fileName = new String(mf);
strInputFile = "";
}
//open a input stream of the file called fileName
//init the strInputFile and strbfBuffer
public void getFile()throws Exception{
fisRWF = new FileInputStream(fileName);
byte buffer[] = new byte[80];
String str;
int bytes;
while((bytes=fisRWF.read(buffer,0,80))!=-1){
str = new String(buffer,0,bytes,"Default");
strInputFile = strInputFile+str;
}
strbfBuffer = new StringBuffer(strInputFile);
fisRWF.close();
}
//modify the strbfBuffer and strOutputFile
//argument brp is the string which would be replace
//argument rp is the string which would be used to replace
public void modify(String brp,String rp){
int intBrpFirst;
strInputFile = strbfBuffer.toString();
intBrpFirst = strInputFile.indexOf(brp);
if (intBrpFirst!=-1){
strbfBuffer.replace(intBrpFirst,intBrpFirst+brp.length(),rp);
modify(brp,rp);
}
strOutputFile = new String(strbfBuffer);
}
//output the strOutputFile to the file
//close the file
public void setFile()throws Exception{
int intSOFNum;
intSOFNum = strOutputFile.length();
byte btOutput[] = new byte[intSOFNum];
btOutput = strOutputFile.getBytes();
fosRWF = new FileOutputStream(fileName);
fosRWF.write(btOutput);
fosRWF.close();
}
}
|
你要的是什么?
用InputStream之类的东西将文件内容读到String中,使用Regexp进行字符串替换,然后再用OutputStream写回去不行吗?
用InputStream之类的东西将文件内容读到String中,使用Regexp进行字符串替换,然后再用OutputStream写回去不行吗?
|
仔细看看这篇文章
http://www.csdn.net/expert/topic/658/658351.xml?temp=8.713931E-02
http://www.csdn.net/expert/topic/658/658351.xml?temp=8.713931E-02
|
那当然要将整个文件读出来再写回去了,这是肯定的!
除非你用数据库,或者是添加文本到末尾!
用xml组织你的文档或许使用起来会好点!
除非你用数据库,或者是添加文本到末尾!
用xml组织你的文档或许使用起来会好点!