使用java更新特定单元格csv文件
嗨,我有一个小问题,我想我只是没有在一行代码中获得正确的语法。基本上,我可以写入csv文件并使用字符串标记器查找特定记录,但它不会更新/编辑该记录的指定单元格。记录保持不变。请帮助……CSV文件只是一个文件。如果你正在阅读它,它不会被改变。 所以,写下你的改变 你有三种方法 1.使用java更新特定单元格csv文件,java,csv,Java,Csv,嗨,我有一个小问题,我想我只是没有在一行代码中获得正确的语法。基本上,我可以写入csv文件并使用字符串标记器查找特定记录,但它不会更新/编辑该记录的指定单元格。记录保持不变。请帮助……CSV文件只是一个文件。如果你正在阅读它,它不会被改变。 所以,写下你的改变 你有三种方法 1. 逐行阅读,找到要更改的单元格 如果需要,请更改单元格并合成当前行的新版本 将该行写入第二个文件 完成后,就有了源文件和结果文件。现在,如果需要,可以删除源文件并将结果文件重命名为source 2. 使用RandomAc
它已经支持您所需的内容并公开高级API。您正在执行以下操作:
String line = readLineFromFile();
line.replace(...);
这不是编辑文件,而是从文件中的一行创建一个新字符串
String
实例是不可变的,因此您正在进行的replace
调用将返回一个新的字符串,而不会修改原始字符串
或者使用一个允许您读写文件的文件流,也就是说,或者(更简单地说)写入一个新文件,然后用新文件替换旧文件
在psuedo代码中:
for (String line : inputFile) {
String [] processedLine = processLine(line);
outputFile.writeLine(join(processedLine, ","));
}
private String[] processLine(String line) {
String [] cells = line.split(","); // note this is not sufficient for correct csv parsing.
for (int i = 0; i < cells.length; i++) {
if (wantToEditCell(cells[i])) {
cells[i] = "new cell value";
}
}
return cells;
}
for(字符串行:inputFile){
字符串[]processedLine=processLine(行);
writeLine(join(processedLine,“,”);
}
私有字符串[]进程行(字符串行){
String[]cells=line.split(,“”;//注意,这不足以进行正确的csv分析。
对于(int i=0;i
另外,请看一看。有一些库可以帮助您处理csv。好的,我正在做类似的事情。我可以访问令牌指向的csv记录,但它不会更新,我认为语法不正确
private static void ChangeRecord(String sFileName) {
// TODO Auto-generated method stub
try {
BufferedReader r = new BufferedReader(
new InputStreamReader(
new FileInputStream(sFileName)));
String line;
while((line = r.readLine()) != null){
String tokens[] = line.split(",");
String Surname = tokens[1];
String network= tokens[2];
String city= tokens[4];
StringTokenizer StrTok = new StringTokenizer(party);
if(StrTok.hasMoreTokens()){
if ((city.equals("New York"))&&(Surname.equals("Smith"))){
String Lovely= "waterloo";
//this line is the one that i think is failing
line.replace(network, Lovely);
System.out.println(line);
}
}
}
r.close();
} catch(IOException e) {
System.out.println(" There was an Error Reading the file. " + e.getMessage());
}
我用过java
嗨,
这是通过指定行和列来更新CSV的代码
/**
*按行和列更新CSV
*
*@param filet更新要更新的CSV文件路径,例如D:\\chetan\\test.CSV
*@param replace替换您的单元格值
*@param行需要更新的行
*@param col列,您需要更新该列
*@抛出异常
*/
public static void updateCSV(字符串fileToUpdate、字符串replace、,
int行,int列)引发IOException{
文件输入文件=新文件(fileToUpdate);
//读取现有文件
CSVReader reader=新的CSVReader(新的文件读取器(inputFile),',');
List csvBody=reader.readAll();
//获取CSV行列并使用行和列替换为
csvBody.get(row)[col]=替换;
reader.close();
//写入打开的CSV文件
CSVWriter writer=new-CSVWriter(new-FileWriter(inputFile),',');
书面作者(csvBody);
writer.flush();
writer.close();
}
这个解决方案对我有效,
干杯 我使用了下面的代码,我将用另一个字符串替换一个字符串,它的工作方式完全符合我的需要:
public static void updateCSV(String fileToUpdate) throws IOException {
File inputFile = new File(fileToUpdate);
// Read existing file
CSVReader reader = new CSVReader(new FileReader(inputFile), ',');
List<String[]> csvBody = reader.readAll();
// get CSV row column and replace with by using row and column
for(int i=0; i<csvBody.size(); i++){
String[] strArray = csvBody.get(i);
for(int j=0; j<strArray.length; j++){
if(strArray[j].equalsIgnoreCase("Update_date")){ //String to be replaced
csvBody.get(i)[j] = "Updated_date"; //Target replacement
}
}
}
reader.close();
// Write to CSV file which is open
CSVWriter writer = new CSVWriter(new FileWriter(inputFile), ',');
writer.writeAll(csvBody);
writer.flush();
writer.close();
}
publicstaticvoidupdatecsv(stringfiletoupdate)抛出IOException{
文件输入文件=新文件(fileToUpdate);
//读取现有文件
CSVReader reader=新的CSVReader(新的文件读取器(inputFile),',');
List csvBody=reader.readAll();
//获取CSV行列并使用行和列替换为
对于(int i=0;我完全不知道你在做什么,所以我们无法判断你做错了什么部分…你的意思是它没有更新你的文件吗?如果你提供一个简短的例子,说明你正在尝试做什么,以及它如何失败,这个问题将更容易回答。另外,如果你需要使用csv文件,你有没有看一看没有任何可用的Java csv包吗?如果您正在做一些非琐碎的事情,它们可能会让您的生活更轻松。它不会给我代码错误,只是不更新它会打印出原始记录。另外,请注意string.split(“,”)不会处理任何任意csv。您应该看看csv库支持的这个问题:不确定如何实现RAF,并且已经阅读了一些关于它的内容,以通过代码为我指明正确的方向?lifesaver!我知道这是我需要做的,我在如何更新单元格和成功编写方面遇到了困难,但这填补了我的空白我的逻辑有漏洞,谢谢!
public static void updateCSV(String fileToUpdate) throws IOException {
File inputFile = new File(fileToUpdate);
// Read existing file
CSVReader reader = new CSVReader(new FileReader(inputFile), ',');
List<String[]> csvBody = reader.readAll();
// get CSV row column and replace with by using row and column
for(int i=0; i<csvBody.size(); i++){
String[] strArray = csvBody.get(i);
for(int j=0; j<strArray.length; j++){
if(strArray[j].equalsIgnoreCase("Update_date")){ //String to be replaced
csvBody.get(i)[j] = "Updated_date"; //Target replacement
}
}
}
reader.close();
// Write to CSV file which is open
CSVWriter writer = new CSVWriter(new FileWriter(inputFile), ',');
writer.writeAll(csvBody);
writer.flush();
writer.close();
}