请大家帮帮俺吧,帮我写下这个程序,谢谢了,俺实在写不出来~~~·谢谢啦(最好写上注释)o(∩_∩)o...
编写一个程序,使用一个数据输出流来写两个整数到一个文件中去,然后打开这个文件,尝试以浮点值的方式来读取这个数据,是否可以正常工作?

解决方案 »

  1.   

        // 2.利用StringBuffer写文件
        public void StringBufferDemo() throws IOException
        {
            File file = new File("d:\\1.txt");
            if (!file.exists())
                file.createNewFile();
            FileOutputStream out = new FileOutputStream(file, true);
            for (int i = 0; i < 10000; i++)
            {
                StringBuffer sb = new StringBuffer();
                sb.append("1234\n");
                sb.append("5678");
                out.write(sb.toString().getBytes("utf-8"));
            }
            out.close();
        }
      

  2.   

        public String BufferedReaderDemo(String path) throws IOException
        {
            File file = new File(path);
            if (!file.exists() || file.isDirectory())
                throw new FileNotFoundException();
            BufferedReader br = new BufferedReader(new FileReader(file));
            String temp = null;
            StringBuffer sb = new StringBuffer();
            temp = br.readLine();
            while (temp != null)
            {
                sb.append(temp + " ");
                temp = br.readLine();
            }
            return sb.toString();
        }