I am trying to write account numbers(e.g: 00000900123) into csv file in java. After creating a file and writing account number gets converted into 900123 automatically. How can i code to make it retain the preceding zeros in the file.
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) {
}
}
}
}
}
After i run the above program it generates a testfile.csv. If i open it in text editor it shows the full string as below,
name, accountNumber, amount
savings, 000090123, 12000.0
But if i open in csv account number would be 90123. We need to provide the csv file to client so complete account number is required.