回 帖 发 新 帖 刷新版面

主题:[讨论]大家看下这个io程序错在哪里

把代码运行后怎么老出现  IOException 的问题呢?
import java.io.*;
public class c14_10
{
   public static void main(String args[])
   {  int  num,a;
      long fp;
      try
      {
         InputStreamReader din=new InputStreamReader(System.in);//键盘输入
         BufferedReader in=new BufferedReader(din);
         //建立随机存取文件  
         RandomAccessFile rf=new RandomAccessFile(args[0],"rw");
         System.out.println("请输入五个整数");
         int b[]=new int[5];
         for(int i=0;i<5;i++)
         {
            System.out.print("第" + (i+1)+"个数   ");
            b[i]=Integer.parseInt(in.readLine());
            rf.writeInt(b[i]);      //写入文件
         }
         while(true)
         {  fp=0;
            rf.seek(0);     //移动文件指针到文件头
            System.out.print("请输入要显示第几个数(1-5):");
            num=Integer.parseInt(in.readLine()); //读入序号
            num=num-1;    
            fp=(num)*4;   //每个整数4个字节,计算移动位数
            rf.seek(fp);     //移动文件指针到要显示数的首位 
            a=rf.readInt();
            System.out.println("第"+(num+1)+"个数是: "+a);
            System.out.print("改写此数 ");
            b[num]=Integer.parseInt(in.readLine());
            fp=num*4; rf.seek(fp);
            rf.writeInt(b[num]);      //写入文件
            System.out.print("继续吗?(y/n) ");
            if((in.readLine()).equals("n")) break;
         }
         rf.close();
      }
      catch(Exception E)
      {   System.out.println("I/O错误!");  }
   }
}

回复列表 (共1个回复)

沙发

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

public class C14_10 {
    public static void main(String args[]) {
        int num, a;
        long fp;
        try {
            InputStreamReader din = new InputStreamReader(System.in);//键盘输入
            BufferedReader in = new BufferedReader(din);
            
            //建立随机存取文件  
            //RandomAccessFile rf = new RandomAccessFile(args[0], "rw");
            String str = in.readLine();
            RandomAccessFile rf = new RandomAccessFile(str, "rw");
            
            
            System.out.println("请输入五个整数");
            int b[] = new int[5];
            for (int i = 0; i < 5; i++) {
                System.out.print("第" + (i + 1) + "个数   ");
                b[i] = Integer.parseInt(in.readLine());
                rf.writeInt(b[i]); //写入文件
            }
            while (true) {
                fp = 0;
                rf.seek(0); //移动文件指针到文件头
                System.out.print("请输入要显示第几个数(1-5):");
                num = Integer.parseInt(in.readLine()); //读入序号
                num = num - 1;
                fp = (num) * 4; //每个整数4个字节,计算移动位数
                rf.seek(fp); //移动文件指针到要显示数的首位 
                a = rf.readInt();
                System.out.println("第" + (num + 1) + "个数是: " + a);
                System.out.print("改写此数 ");
                b[num] = Integer.parseInt(in.readLine());
                fp = num * 4;
                rf.seek(fp);
                rf.writeInt(b[num]); //写入文件
                System.out.print("继续吗?(y/n) ");
                if ((in.readLine()).equals("n"))
                    break;
            }
            
            //rf.close();
            in.close();
            din.close();
            
        } catch (Exception E) {
            System.out.println("I/O错误!");
        }
    }

}

我来回复

您尚未登录,请登录后再回复。点此登录或注册