我正在尝试将帐号(例如:00000900123)写入java中的csv文件中。创建文件后,编写帐号会自动转换为900123。我如何编码使其在文件中保留前面的零。
public class WriteData {
public static void main(String[] args) {
BufferedWriter bufferedWriter = null;
FileOutputStream fileOutputStream = null;
String header = "name, accountNumber, amount";
String record = "savings, 000090123, 12000.0";
File file = new File("testfile.csv");
try {
fileOutputStream = new FileOutputStream(file);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferedWriter.write(header);
bufferedWriter.newLine();
bufferedWriter.write(record);
bufferedWriter.newLine();
} catch(FileNotFoundException fne) {
} catch(IOException ioe) {
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
}
}
}
}
}
运行上述程序后,它会生成一个testfile.csv。如果我在文本编辑器中打开它,它会显示如下所示的完整字符串,
name, accountNumber, amount
savings, 000090123, 12000.0
但是,如果我在csv中打开帐号将是90123.我们需要向客户提供csv文件,因此需要完整的帐号。