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();
移出运行循环。