帮手写段java源程序啊~

来源:百度知道 编辑:UC知道 时间:2024/06/23 23:32:11
打开一个文本文件,向其中写入0~9这10个数字,如果这个文件不存在,就创建一个。其中,在打开文件及向文件书写时,都可能发生异常,而且可能发生两种异常,需要用catch语句进行捕获和处理。
分析提示:关于文件的操作,这里要用到的是如下语句:
out=new PrintStream(new FileOutputStream(“OutFile.txt”));
生成对象out后,可以对其进行操作,题目要求是写入数字,可以按如下方式实现:
out.println(value);
可以把10个数字放在一个数组中,用Vector实现。

//生成文件
try {
//以年月日来创建文件
//String dirname = "2005-11-23";
//File file = new File("e:/test/oml/salary/"+dirname.trim()+".txt");

// Write the output to a file
String fileName = String.valueOf(System.currentTimeMillis());
FileOutputStream fileOut = new FileOutputStream(path + fileName);
//String data = buffer.toString();
fileOut.write(dataStr.getBytes());
fileOut.close();

return fileName;
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
throw new DBException(ex.getMessage());
}