我想用java操作.txt文件,首先判断文件是否存在,如果存在则读取,不存在则创建,并将一维数组写入文件,该如何实现,请求指点,万分感谢。

解决方案 »

  1.   

    读文件的  你可以看看我的这篇blog吧
    http://blog.csdn.net/justinavril/archive/2008/08/06/2775767.aspx
      

  2.   

    按你的要求写的代码,运行了,是正确的! 
    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();
                }
            }
        }