I have a simple question regarding how to write my object array data into a file Using a buffered writer, I have had no joy and I need some help please.
Please excuse my lack of knowledge I have only just started learning Java.
Object[][] data = {
{"dd/mm/yyyy", new Double(5), new Boolean(false),
{"21/12/2013", new Double(5), new Boolean(false)};
开发者_运维知识库 }
String sDividendDate1 = (String)data[0][0];
Double dDividend1 = (Double)data[0][1];
try
{
FileWriter file = new FileWriter(NewCodeFile);
BufferedWriter buffer = new BufferedWriter(file);
buffer.write(sDividendDate1);
buffer.newLine();
**buffer.write(sDividend1); /*This is where the compiler does not like
[I think it is because I am trying to write a double variable into a char
text buffer?]**/
}
How do I write the double value stored in dDividend1 in the same file after I have written sDividendDate1 into the same file. Any help is always greatly appreciated.
Many thanks
Kind Regards Stephen
Try String.valueOf(sDividend1)
or Double.toString(...)
.
You might also want to check java.text.NumberFormat
.
Stephen, Based on your code above you have referenced an undeclared variable:
Object[][] data = {
{
{
"dd/mm/yyyy",
new Double(5),
new Boolean(false)
},
{
"21/12/2013",
new Double(5),
new Boolean(false)
}
};
String sDividendDate1 = (String)data[0][0];
Double dDividend1 = (Double)data[0][1];
try {
FileWriter file = new FileWriter(NewCodeFile);
BufferedWriter buffer = new BufferedWriter(file);
buffer.write(sDividendDate1);
buffer.newLine();
buffer.write(dDividend1); /* you had this as sDividend1 which was not declared */
} catch(Exception ex) {
//catch logic here
}
If you want to write Objects in a binary representation you should use an ObjectOutputStream
精彩评论