如何把文件内容读取到一个String 并且修改后又回写到这个文件

wswhp 2008-01-14 06:26:20
问题就是 从一个文件中读取内容到String

然后替代这个string的某些内容 ,最后写回那个文件中

请问怎么实现 谢谢了
...全文
470 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
大海Online 2008-01-16
  • 打赏
  • 举报
回复
操作文件我喜欢使用 commons-io 包, 很方便

这里用了包中的 org.apache.commons.io.IOUtils

参考以下代码

//把文件中的数据保存在字符串中
String data = IOUtils.toString(new FileInputStream("filePath"));
//替换字符串中的内容
data = data.replaceAll("regex", "content");
//将替换后的数据保存在文件中
IOUtils.write(data, new FileOutputStream("filePath"));
aurual 2008-01-14
  • 打赏
  • 举报
回复
自己网上找找 看看JDK 很快就好解决了
aunty_flybird 2008-01-14
  • 打赏
  • 举报
回复
顶一楼,问题被二楼复杂化了

该问题由三个组成
1、如何读文件

2、如何在String中查找特征字符串
String string = “读出字符串”;
string.replaceAll("查找的字串", "替换的字串");

3、如何写文件
yes152 2008-01-14
  • 打赏
  • 举报
回复
应该存到String[]数组中吧
要是存到String[]数组中,应该先分配空间吧
我原来写过一个比较2个文件不同的地方,感觉方法好笨呀!!!!
import java.io.*;   
class SrengHelper {
private String TEMP = "***";
private String[] oldFile;
private String[] newFile;
private String OLDSREngLOG = "D:/oldSREngLOG.log"; // 备份文件的路径;
private String NEWSREngLOG = "D:/newSREngLOG.log"; // 新文件的路径;
private String RESULTSREngLOG = "D:/resultFile.log"; // 输出文件的路径;
//------------------------------
// replaceLine Method
public SrengHelper replaceLine(String[] oldFile, String[] newFile) { // 选择法比较备份文件和新文件;
for(int i = 0; i < oldFile.length; i++) {
for(int j = 0; j < newFile.length; j++) {
if(oldFile[i].equals(newFile[j])) {
newFile[j] = newFile[j].replace(newFile[j], TEMP);
}
}
}
return this;
}
//------------------------------
// Output Method
public void Output(String[] newFile) { // 输出备份文件与新文件不同的行;
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(RESULTSREngLOG));
String s = null;
for(int i = 0; i < newFile.length; i++) {
if(!(newFile[i].equals(TEMP))) {
s = i + "." + newFile[i];
System.out.println(s);
bw.write(s);
bw.newLine();
}
}
bw.flush();
bw.close();
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}
}
//------------------------------
// count oldFile
public int oldFileCount() { // 统计备份文件行数;
int oldFileCount = 0;
BufferedReader o = null;
try {
o = new BufferedReader(new FileReader(OLDSREngLOG));
while((o.readLine()) != null) {
oldFileCount ++;
}
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}finally {
try {
o.close();
} catch (IOException e) {
System.out.println("文件关闭错误!");
System.exit(-1);
}
}
return oldFileCount;
}
//------------------------------
// count newFile
public int newFileCount() { // 统计新文件行数
int newFileCount = 0;
BufferedReader n = null;
try {
n = new BufferedReader(new FileReader(NEWSREngLOG));
while((n.readLine()) != null) {
newFileCount ++;
}
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}finally {
try {
n.close();
} catch (IOException e) {
System.out.println("文件关闭错误!");
System.exit(-1);
}
}
return newFileCount;
}
//------------------------------
// save oldFile
public SrengHelper saveOldFile(String[] oldFile) { // 把备份文件保存到String[]数组;
BufferedReader br = null;
String s = null;
try {
br = new BufferedReader(new FileReader(OLDSREngLOG));
for(int i = 0; (s = br.readLine()) != null; i++) {
oldFile[i] = s;
}
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}finally {
try {
br.close();
}catch (IOException e) {
System.out.println("文件关闭错误!");
System.exit(-1);
}
}
return this;
}
//------------------------------
// save newFile
public SrengHelper saveNewFile(String[] newFile) { // 把新文件保存到String[]数组;
BufferedReader bs = null;
String s = null;
try {
bs = new BufferedReader(new FileReader(NEWSREngLOG));
for(int i = 0; (s = bs.readLine()) != null; i++) {
newFile[i] = s;
}
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}finally {
try {
bs.close();
}catch (IOException e) {
System.out.println("文件关闭错误!");
System.exit(-1);
}
}
return this;
}
}
//------------------------------------------------------------
// Main Method
public class TestString {
public static void main(String[] args) {
SrengHelper sh = new SrengHelper();
String[] oldFile= new String[sh.oldFileCount()];
String[] newFile= new String[sh.newFileCount()];
sh.saveOldFile(oldFile).saveNewFile(newFile);
sh.replaceLine(oldFile, newFile).Output(newFile);

}

}
老紫竹 2008-01-14
  • 打赏
  • 举报
回复
建议你生成一个新的临时文件,保存后再删除老文件,新文件改名为老文件,这里有一个参考代码
具体修改或删除哪一行,请把里面的if语句进行处理就可以了。修改的话,别忘了把新内容写入到新临时文件中

  public static void main(String[] args) throws IOException {
File file = new File("d:/55.txt");
File file2 = new File("d:/55.txt.tmp");
BufferedReader reader = new BufferedReader(new FileReader(file));
PrintWriter writer = new PrintWriter(file2);
String line;
while ((line = reader.readLine()) != null) {
// 判断条件,根据自己的情况书写,会删除所有符合条件的行
if (line.startsWith("1899-12-30") && line.indexOf("0.000000") != -1) {
// 读取后面的几行,废弃
// reader.readLine();
// reader.readLine();
// reader.readLine();
continue;
}
writer.println(line);
writer.flush();
}
reader.close();
writer.close();

// 删除老文件
file.delete();
file2.renameTo(file);
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧