问题描述
我正在开发一个需要快速访问CSV逗号分隔的电子表格文件的程序。
到目前为止,我已经能够使用BufferedReader轻松读取它。
然而,现在我想能够编辑它读取的数据,然后将其导出为CSV。
电子表格包含姓名,电话号码,电子邮件地址等。该程序列出每个人的数据,当您点击它们时,它会打开一个更详细的信息页面,也从CSV中提取。在该页面上,您可以编辑数据,并且我希望能够点击保存更改按钮,然后将数据导出回CSV中的相应行,或删除旧的数据,然后附加新数据。 / p>
我不太熟悉使用BufferedWriter,或者我应该使用的任何东西。
我开始做的是创建一个名为FileIO的自定义类。它包含BufferedReader和BufferedWriter。到目前为止,它有一个方法返回bufferedReader.readLine(),称为read()。现在我想要一个函数write(String line)。
public static class FileIO {
BufferedReader read;
BufferedWriter write;
public FileIO(String file)throws MalformedURLException,IOException {
read = new BufferedReader(new InputStreamReader(getUrl(file).openStream()));
write = new BufferedWriter(new FileWriter(file));
}
public static URL getUrl(String file)throws IOException {
return // new URL(fileServer + file).openStream()));
FileIO.class.getResource(file);
}
public String read()throws IOException {
return read.readLine();
}
public void write(String line){
String [] data = line.split(\\ |);
String firstName = data [0];
// int lineNum = findLineThatStartsWith(firstName);
// write.writeLine(lineNum,line);
}
};
我希望有人知道我该怎么做?
而不是重新发明轮,你可以看看,支持读取和写入CSV文件。以下是
I'm working on a program that requires quick access to a CSV comma-delimited spreadsheet file.So far I've been able to read from it easily using a BufferedReader.However, now I want to be able to edit the data it reads, then export it BACK to the CSV.
The spreadsheet contains names, phone numbers, email addresses, etc. And the program lists everyone's data, and when you click on them it brings up a page with more detailed information, also pulled from the CSV. On that page you can edit the data, and I want to be able to click a "Save Changes" button, then export the data back to its appropriate line in the CSV--or delete the old one, and append the new.
I'm not very familiar with using a BufferedWriter, or whatever it is I should be using.
What I started to do is create a custom class called FileIO. It contains both a BufferedReader and a BufferedWriter. So far it has a method that returns bufferedReader.readLine(), called read(). Now I want a function called write(String line).
public static class FileIO {
BufferedReader read;
BufferedWriter write;
public FileIO (String file) throws MalformedURLException, IOException {
read = new BufferedReader(new InputStreamReader (getUrl(file).openStream()));
write = new BufferedWriter (new FileWriter (file));
}
public static URL getUrl (String file) throws IOException {
return //new URL (fileServer + file).openStream()));
FileIO.class.getResource(file);
}
public String read () throws IOException {
return read.readLine();
}
public void write (String line) {
String [] data = line.split("\\|");
String firstName = data[0];
// int lineNum = findLineThatStartsWith(firstName);
// write.writeLine(lineNum, line);
}
};
I'm hoping somebody has an idea as to how I can do this?
Rather than reinventing the wheel you could have a look at OpenCSV which supports reading and writing of CSV files. Here are examples of reading & writing
这篇关于Java:CSV文件轻松读/写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!