JAVA 文件操作问题 我想用java操作.txt文件,首先判断文件是否存在,如果存在则读取,不存在则创建,并将一维数组写入文件,该如何实现,请求指点,万分感谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 读文件的 你可以看看我的这篇blog吧http://blog.csdn.net/justinavril/archive/2008/08/06/2775767.aspx 按你的要求写的代码,运行了,是正确的! public static void main(String[] args) throws FileNotFoundException, IOException { File file = new File("c:/789.txt"); if (file.exists()) { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); while(reader.ready()) { System.out.println(reader.readLine()); } } else { FileOutputStream os = new FileOutputStream(file); String[] array = {"a","b","c"}; for (int i = 0; i < array.length; i++) { os.write(array[i].getBytes()); os.flush(); } } } WebSphere数据源的问题,已经纠结两天了,大家都把帮帮忙 安装jre后,控制面板中没有java plug-in图标 關於LineNumberReader 的問題 eclipse通过编译但是没有输出结果(I/O类) Hibernate如何处理Mysql中默认编码 手机闹钟、锁键盘源代码 紧急求救各位身份证的校验程序。。。。。 急求 关于JInternalFrame之间的操作问题! 麻烦给出在JAVA里实现2字节数组到short转换的代码 菜鸟问题:装上了jdk1.2,如何配置环境? 一个javascript的小问题?? 学java用什么编译器好???
http://blog.csdn.net/justinavril/archive/2008/08/06/2775767.aspx
public static void main(String[] args) throws FileNotFoundException, IOException
{
File file = new File("c:/789.txt");
if (file.exists())
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while(reader.ready())
{
System.out.println(reader.readLine());
}
}
else
{
FileOutputStream os = new FileOutputStream(file);
String[] array = {"a","b","c"};
for (int i = 0; i < array.length; i++)
{
os.write(array[i].getBytes());
os.flush();
}
}
}