EditText fileNameEdit= (EditText) getActivity().findViewById(R.id.fileName);
String fileName = fileNameEdit.getText().toString();
将文件写入磁盘:
try {
String content = "Separe here integers by semi-colon";
File file = new File(fileName +".csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
阅读该文件:
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName+".csv"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
String[] intArray = sCurrentLine.split(";");
由于JDK问题,Opencsv在Android上无法工作。
如果您看到类似以下内容:
05-04 16:13:31.821 25829-25829/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pk.opencsvpoc, PID: 25829 java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;
这是因为Android仅将java.beans的子集移植到其Java版本中。您可以查看支持列表, https://developer.android.com/reference/java/beans/package-summary
计划在opencsv 5.0中切换到反射,但我怀疑它是否会消除我们对java.beans的所有依赖。 目前,针对opencsv的最佳建议是避免使用com.opencsv.bean类,直到Android完全支持java.beans或我们成功删除java.beans。
另一个可能性是尝试另一个csv库。我检查了apache commons csv和super-csv, apache不转换为bean,但super-csv只使用反射。
来源: https://sourceforge.net/p/opencsv/wiki/FAQ/#getting-noclassdeffounderror-when-using-android
意思是:当在Android上使用时,出现“NoClassDefFoundError”错误该怎么办?