public void exportToCSV() throws IOException {
File pathfile = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ "csvData");
if (!pathfile.isDirectory()) {
pathfile.mkdir();
}
File file = new File(pathfile,
File.separator + "csvDataFile.csv");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
writer = new CSVWriter(new FileWriter(file));
new Thread() {
public void run() {
if (csvThreadRunning) {
while (!Thread.currentThread().isInterrupted()) {
try {
String s1 = String.valueOf(globalData
.getLatestGraphData1()) + "pin39"; // array
// of
// your
// values
writer.writeNext(s1);
s1 = String.valueOf(globalData
.getLatestGraphData2()) + "pin40";
writer.writeNext(s1);
s1 = String.valueOf(globalData
.getLatestGraphData3()) + "pin41";
writer.writeNext(s1);
s1 = String.valueOf(globalData
.getLatestGraphData4()) + "pin42";
writer.writeNext(s1);
s1 = String.valueOf(globalData
.getLatestGraphData5()) + "pin43";
writer.writeNext(s1);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}.start();
}
CSV 文件出现在正确的路径中,但没有写入任何数据?
最佳答案
问题是我关闭了 writer
太早了;导致 csv 文件写入一列然后停止。
解决方案是移动 writer.close();
在运行循环之外。
关于java - Android openCSV,文件出现 - 但不会写入任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25111054/