import java.io.*;/**
 * Read a file and print, using BufferedReader and System.out
 */
public class CatFile {    public static void main(String[] av) {
        CatFile c = new CatFile();
        if (av.length == 0)
            c.process(new BufferedReader(
new InputStreamReader(System.in)));
else for (int i=0; i<av.length; i++)
try {
c.process(new BufferedReader(new FileReader(av[i])));
} catch (FileNotFoundException e) {
System.err.println(e);
}
    }    /** print one file, given an open BufferedReader */
    public void process(BufferedReader is) {
        try {
            String inputLine;            while ((inputLine = is.readLine()) != null) {
                System.out.println(inputLine);
            }
            is.close();
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
    }
}

解决方案 »

  1.   

    import java.io.*;
    class DataReadDemo {
      public static void main (String args[]) {
        try{
          FileInputStream in = new FileInputStream("datatypes.txt");
          DataInputStream datain = new DataInputStream(in);
          char c=datain.readChar(); 
          boolean b=datain.readBoolean(); 
          byte b2=datain.readByte(); 
          int i=datain.readInt(); 
          double d=datain.readDouble(); 
          String s=datain.readLine(); 
          System.out.println("c="+c);
          System.out.println("b="+b);
          System.out.println("b2="+b2);
          System.out.println("d="+d);
          System.out.println("s="+s);
          in.close();
          datain.close();
        }catch(FileNotFoundException e) {System.out.println(e);}
         catch(IOException e) {
           System.out.println(e);
        }
      }
    }
    import java.io.*;
    class DataWriteDemo {
      public static void main (String args[]) {
        try{
          FileOutputStream out = new FileOutputStream("datatypes.txt");
          DataOutputStream dataout = new DataOutputStream(out);
          char c='a';
          dataout.writeChar(c); 
          boolean b=false;
          dataout.writeBoolean(b); 
          byte b2=040;
          dataout.writeByte(b2); 
          int i=12345;
          dataout.writeInt(i); 
          double d=123.45;
          dataout.writeDouble(d); 
          String s="uvwxyz";
          dataout.writeUTF(s); 
          System.out.println("b="+b);//...
          out.close();
          dataout.close();
        }catch(FileNotFoundException e) {System.out.println(e);}
         catch(IOException e) {
           System.out.println(e);
        }
      }
    }