使用arraylist、Java在文件中搜索和替换
我编写了代码的以下部分,但无法使用搜索和替换绑定arraylist 所以我的csv文件如下所示 1/1/1;7/6/1 1/1/2;7/7/1 我想在1.cfg文件中搜索1/1/1并将其更改为7/6/1,将1/1/2更改为7/7/1,依此类推 提前谢谢大家 现在只在新文件中打印旧文件的最后一行使用arraylist、Java在文件中搜索和替换,java,arrays,search,replace,Java,Arrays,Search,Replace,我编写了代码的以下部分,但无法使用搜索和替换绑定arraylist 所以我的csv文件如下所示 1/1/1;7/6/1 1/1/2;7/7/1 我想在1.cfg文件中搜索1/1/1并将其更改为7/6/1,将1/1/2更改为7/7/1,依此类推 提前谢谢大家 现在只在新文件中打印旧文件的最后一行 import java.io.*; import java.util.ArrayList; import java.util.List; public class ChangeConfiguration
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ChangeConfiguration {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args)
{
try{
// Open the file that is the first
// command line parameter
FileInputStream degistirilecek = new FileInputStream("c:/Config_Changer.csv");
FileInputStream config = new FileInputStream("c:/1.cfg");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(config);
DataInputStream degistir = new DataInputStream(degistirilecek);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
BufferedReader brdegis = new BufferedReader(new InputStreamReader(degistir));
List<Object> arrayLines = new ArrayList<Object>();
Object contents;
while ((contents = brdegis.readLine()) != null)
{
arrayLines.add(contents);
}
System.out.println(arrayLines + "\n");
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
//Couldn't modify this part error is here :(
BufferedWriter out = new BufferedWriter(new FileWriter("c:/1_new.cfg"));
out.write(strLine);
out.close();
}
in.close();
degistir.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
import java.io.*;
导入java.util.ArrayList;
导入java.util.List;
公共类更改配置{
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)
{
试一试{
//打开第一个文件
//命令行参数
FileInputStream degistirilecek=新的FileInputStream(“c:/Config\u Changer.csv”);
FileInputStream config=newfileinputstream(“c:/1.cfg”);
//获取DataInputStream的对象
DataInputStream in=新的DataInputStream(配置);
DataInputStream degistir=新的DataInputStream(degistirilecek);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
BufferedReader brdegis=新的BufferedReader(新的InputStreamReader(degistir));
List arrayLines=新建ArrayList();
客体内容;
而((contents=brdegis.readLine())!=null)
{
添加(内容);
}
System.out.println(arrayLines+“\n”);
弦斯特林;
//逐行读取文件
而((strLine=br.readLine())!=null){
//无法修改此部件错误如下:(
BufferedWriter out=新的BufferedWriter(新文件写入程序(“c:/1_new.cfg”);
写出(strLine);
out.close();
}
in.close();
degistir.close();
}catch(异常e){//catch异常(如果有)
System.err.println(“错误:+e.getMessage());
}
}
}
声明以下内容时,您正在打开文件进行读取:
BufferedReader br = new BufferedReader(new InputStreamReader(in));
如果您知道整个文件将放入内存,我建议您执行以下操作:
- 打开该文件,将其内存中的内容读入一个巨大的字符串,然后关闭该文件
- 把你的替代品一枪打到巨大的绳子上
- 打开文件并写出(例如使用a)巨型字符串的内容,然后关闭文件
作为旁注,您发布的代码将无法编译。您收到的回答的质量与所问问题的质量相关。请始终在问题中添加一个,以增加获得问题准确答案的机会。您能否详细说明该程序的目的 如果是文件中的简单内容替换 然后读取一行并将其存储在字符串中。然后使用字符串替换方法替换字符串中的文本 例如:
newStrog=oldString.replace(oldVlue,newValue);什么是函数replace(arrayName,replaceTo,replacetwith)?这不会用Java编译。首先,如果你想修改一个文件,你需要对它进行写入。而且你不能写入
阅读器
或输入流
,你需要一个编写器
或输出流
。是的,函数部分是我的问题,我不能为Java编写那部分从数组中删除并替换它。@Mark我暂时将其放入缓冲区,但我计划稍后在它被更改并写入新文件后从缓冲区中删除。我还没有添加该部分。但它会像这样工作,对吗?是的,我也这样认为以打开并缓冲文件(顺便说一下,文件大约为500kb)更改字符串,然后作为新文件写入,但我现在没有添加该部分。我现在尝试从数组中搜索替换,但无法管理它。我还应该修剪数组,对吗?你没有抓住要点,对数组所做的任何更改都不会反映在文件中。你可以更改数组中的任何内容,而文件将是一样的。那么你的意思是我应该把所有的缓冲文件变成一个字符串?我怎么做?