JAVA 将一个OBJECT类型的二维数组存到TXT文件里,然后再读取
Object[][]tableData={newObject[]{"李清照",29,"女"},newObject[]{"苏格拉底",56,"男"},newObject[]...
Object[][] tableData = { new Object[] { "李清照", 29, "女" },
new Object[] { "苏格拉底", 56, "男" },
new Object[] { "李白", 35, "男" },
new Object[] { "弄玉", 18, "女" },
new Object[] { "虎头", 2, "男" } };
现在有这么一个数组,我想用FileOutputStream输出到一个txt文档中,每次写到fileoutput.write()的时候总是报错,麻烦高手帮写一个完整的语句,谢谢 展开
new Object[] { "苏格拉底", 56, "男" },
new Object[] { "李白", 35, "男" },
new Object[] { "弄玉", 18, "女" },
new Object[] { "虎头", 2, "男" } };
现在有这么一个数组,我想用FileOutputStream输出到一个txt文档中,每次写到fileoutput.write()的时候总是报错,麻烦高手帮写一个完整的语句,谢谢 展开
1个回答
展开全部
public class Test{
public static void main(String args[])
{
Object[][] tableData = { new Object[] { "李清照", 29, "女" },
new Object[] { "苏格拉底", 56, "男" },
new Object[] { "李白", 35, "男" },
new Object[] { "弄玉", 18, "女" },
new Object[] { "虎头", 2, "男" } };
try {
File f=new File("D:/test.txt");
FileOutputStream out=new FileOutputStream(f);
for(Object[] o:tableData){
for(Object obj:o)
{
byte [] b=obj.toString().getBytes();
out.write(b,0,b.length);
out.write(" ".getBytes());
}
out.write("\r\n".getBytes());
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[])
{
Object[][] tableData = { new Object[] { "李清照", 29, "女" },
new Object[] { "苏格拉底", 56, "男" },
new Object[] { "李白", 35, "男" },
new Object[] { "弄玉", 18, "女" },
new Object[] { "虎头", 2, "男" } };
try {
File f=new File("D:/test.txt");
FileOutputStream out=new FileOutputStream(f);
for(Object[] o:tableData){
for(Object obj:o)
{
byte [] b=obj.toString().getBytes();
out.write(b,0,b.length);
out.write(" ".getBytes());
}
out.write("\r\n".getBytes());
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询