我不确定我需要做什么来比较这两个元素,我知道我需要读取数据文件并且我有一个类来做到这一点。
/**
* Helper methods to manage file content.
*/
public class FileUtilities {
/**
* Reads a comma separated file from the classpath.
*/
public List<List<String>> readFileFromClasspath(String file)
throws FileNotFoundException, IOException {
InputStream is = getClass().getClassLoader().getResourceAsStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(is);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
return readFileFromBufferedReader(bufferedReader);
}
/**
* Reads a comma separated file from the filesystem.
*/
public List<List<String>> readFileFromFilesystem(String file)
throws FileNotFoundException, IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
return readFileFromBufferedReader(bufferedReader);
}
/**
* Reads a comma separacted file from a Reader.
* <ul>
* <li>Lines started with '#' are ignored.</li>
* <li>Spaces before and after the comma are ignored.</li>
* <li>Fields can be surrounded by quotes.
*/
private List<List<String>> readFileFromBufferedReader(
BufferedReader bufferedReader) throws FileNotFoundException,
IOException {
List<List<String>> fileRows = new ArrayList<List<String>>();
String line = bufferedReader.readLine();
while (line != null && line.length() > 0) {
if (line.charAt(0) != '#') {
List<String> rowValues = new ArrayList<String>();
String[] tokens = line
.split(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
for (String token : tokens) {
String processedToken = stripQutoes(token);
rowValues.add(processedToken);
}
fileRows.add(rowValues);
}
line = bufferedReader.readLine();
}
bufferedReader.close();
return fileRows;
}
private String stripQutoes(String token) {
String tokenWithoutSpaces = token.trim();
if (tokenWithoutSpaces.length() > 0) {
if (tokenWithoutSpaces.charAt(0) == '"'
&& tokenWithoutSpaces
.charAt(tokenWithoutSpaces.length() - 1) == '"') {
return tokenWithoutSpaces.substring(1,
tokenWithoutSpaces.length() - 1);
}
}
return tokenWithoutSpaces;
}
}
另外,当我读取数据文件时,我没有错误,所以我想我读取正确,此外,我已经使用Eclipse Luna 4.4.0和PostgreSQL 9.4实现了将 java 与数据库正确连接。
这是我拥有的主要课程的一部分:
public class Exercise1UpdateOrInsertDataFromFile {
private FileUtilities fileUtilities;
public Exercise1UpdateOrInsertDataFromFile() {
super();
fileUtilities = new FileUtilities();
}
public static void main(String[] args) {
Exercise1UpdateOrInsertDataFromFile app = new Exercise1UpdateOrInsertDataFromFile();
app.run();
}
private void run() {
List<List<String>> fileContents = null;
try {
fileContents = fileUtilities.readFileFromClasspath("exercise1.data");
} catch (FileNotFoundException e) {
System.err.println("ERROR: File not found");
e.printStackTrace();
} catch (IOException e) {
System.err.println("ERROR: I/O error");
e.printStackTrace();
}
if (fileContents == null) {
return;
}
//at this point we asume that we read from file exercise1.data
DBAccessor dbaccessor = new DBAccessor();
dbaccessor.init();
Connection conn = dbaccessor.getConnection();
编辑
我已经用另一个更准确和最简单的答案替换了我的答案。
我需要一个关于如何获取例如从数据文件中读取的第一行的示例,如果我知道如何获取数据,也许我会做下一部分。我想我应该以某种方式使用 fileUtilities 但不知道是什么。
这是 file.data 的内容:
# Doc_Nmber, Pat_Number, Visit_Date, Price
26902,6574405,30/03/2011,315
26507,6392432,14/03/2010,322
35356,6574405,15/02/2011,475
35252,9062865,07/07/2011,140
我需要更新的表具有相同的内容。