我的代码的目标是用用户输入的文本字段替换. csv文件中的某个文本值。
我的。csv文件的值用逗号分隔:hey,hi
。如果我只是想替换'hey',那么我会从文本字段收集输入并将'hey'替换为'bye'。输出:bye,hi
.
在我的代码中,我认为我正在读取文件并将文件的内容写入以逗号分隔的列表。
然后我将遍历列表,用另一个用户输入替换列表中用户输入的实例,并将其写回file。
然而,我不能把它写回文件,因为我得到Object[]不能转换为String[]错误。因此,我被困在如何替换文本文件中的用户输入的实例。
下面是我的代码:
try{
//Convert user input into strings
String strSerial = editSerialField.getText();
String strLocation = editLocationField.getText();
//Read existing file
CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
List myEntries = reader.readAll();
//Iterate through my array
for (int i = 0; i < myEntries.size(); i++)
{
//If an entry matches the user input
if (myEntries.get(i).equals(strSerial))
{
//Set the match to the user input from strLocation
myEntries.set(i, strLocation);
break;
}
}
//Write to existing file
CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',');
//Error is here**********************
//Write the new string with the replaced word OVER the same file
writer.writeNext(myEntries.toArray(new String[myEntries.size()]));
writer.close();
}catch (IOException e)
{
System.out.println(e);
}
}
我如何修改我的代码,以便它将我的更改写入。csv文件?
一开始writeNext
会一次写一行,所以你需要循环。
其次考虑不使用原始的List
,而是使用泛型。
第三,边写边写可能更干净
最后,每行将包含一个字符串数组
考虑以下代码(未测试)
CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
List<String []> myEntries = reader.readAll();
reader.close ();
CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',');
//Iterate through my array
for (String [] line : myEntries)
{
ArrayList<String> newLine = new ArrayList <String>();
for (String word : line) {
{
String newVal = word.replace(strSerial, strLocation);
newLine.add (newVal);
}
writer.writeNext(newLine.toArray(new String[newLine.size()]));
}
你的问题是/从这行开始:
List myEntries = reader.readAll();
假设您没有注意到方法readAll()的返回类型是
List<String[]>
例如,如果您的测试文件看起来像:
hey, hi
hallo, hello
sth, sthelse
调用readAll()后,你的变量myEntries将是一个字符串数组列表;表示文件中每一行的每个数组以及作为数组元素的该行中的每个字符串
myEntries : [hey, hi]
[hallo, hello]
[sth, sthelse]
记住这一点,下一个问题是
if (myEntries.get(i).equals(strSerial))
,其中你试图比较String[]与不为真的String。试着这样做:
try{
//Convert user input into strings
String strSerial = editSerialField.getText();
String strLocation = editLocationField.getText();
CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
// valid but not a good practice how you declare your variable before
// List myEntries = reader.readAll(); // List of what??
List<String[]> myEntries = reader.readAll();
for (String[] row : myEntries){ // go through each array from your list representing a row in your file
for (int i = 0; i < row.length; i++){ //go through each element of that array
if (row[i].equalsIgnoreCase(strSerial)){
row[i] = strLocation;
}
}
}
//add the parameter CSVWriter.NO_QUOTE_CHARACTER to prevent opencsv from writing quotes to file
CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',',CSVWriter.NO_QUOTE_CHARACTER);
for (String[] row : myEntries){
writer.writeNext(row);
}
writer.close();
}catch (IOException e){
System.out.println(e);
}
不是很重要,但我更喜欢test.csv而不是test.txt作为文件名,只要你在那里存储逗号分隔的值