如何把文件内容读取到一个String 并且修改后又回写到这个文件 问题就是 从一个文件中读取内容到String然后替代这个string的某些内容 ,最后写回那个文件中请问怎么实现 谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 建议你生成一个新的临时文件,保存后再删除老文件,新文件改名为老文件,这里有一个参考代码具体修改或删除哪一行,请把里面的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); } 应该存到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); } } 顶一楼,问题被二楼复杂化了该问题由三个组成1、如何读文件2、如何在String中查找特征字符串String string = “读出字符串”;string.replaceAll("查找的字串", "替换的字串");3、如何写文件 自己网上找找 看看JDK 很快就好解决了 操作文件我喜欢使用 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")); 生兔子问题求第三解 求助有关List和Set的算法问题 这样的字符串如何用JS排序? 这两种环境设置的方法有什么异同? 小妹重新踏上程序员之路,请各位师哥师姐多多帮助 JAVAMAIL如何建立发件箱 寻求 Cad 操作帮助 mysql的ResultSet为什么不支持更新(读操作正常)? 2个都含有"*"(代表任意1位或多位)字符串匹配的问题~~ jcreator有linux版么? 日期转换问题 菜鸟级问题
具体修改或删除哪一行,请把里面的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);
}
要是存到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);
}
}
1、如何读文件2、如何在String中查找特征字符串
String string = “读出字符串”;
string.replaceAll("查找的字串", "替换的字串");3、如何写文件
String data = IOUtils.toString(new FileInputStream("filePath"));
//替换字符串中的内容
data = data.replaceAll("regex", "content");
//将替换后的数据保存在文件中
IOUtils.write(data, new FileOutputStream("filePath"));