给你一个thinking in java里的程序你参考一下import java.io.*;
import mypackage.*;public class IOStreamDemo {
  public static void main(String[] args) {
    try {
      // 1. Buffered input file
      DataInputStream in =
        new DataInputStream(
          new BufferedInputStream(
            new FileInputStream(args[0])));
      String s, s2 = new String();
      while((s = in.readLine())!= null)
        s2 += s + "\n";
      in.close();      // 2. Input from memory
      StringBufferInputStream in2 =
          new StringBufferInputStream(s2);
      int c;
      while((c = in2.read()) != -1)
        System.out.print((char)c);      // 3. Formatted memory input
      try {
        DataInputStream in3 =
          new DataInputStream(
            new StringBufferInputStream(s2));
        while(true)
          System.out.print((char)in3.readByte());
      } catch(EOFException e) {
        System.out.println(
          "End of stream encountered");
      }      // 4. Line numbering & file output
      try {
        LineNumberInputStream li =
          new LineNumberInputStream(
            new StringBufferInputStream(s2));
        DataInputStream in4 =
          new DataInputStream(li);
        PrintStream out1 =
          new PrintStream(
            new BufferedOutputStream(
              new FileOutputStream(
                "iverson.txt")));
        while((s = in4.readLine()) != null )
          out1.println(
            "Line " + li.getLineNumber() + s);
        out1.close(); // finalize() not reliable!
      } catch(EOFException e) {
        System.out.println(
          "End of stream encountered");
      }      // 5. Storing & recovering data
      try {
        DataOutputStream out2 =
          new DataOutputStream(
            new BufferedOutputStream(
              new FileOutputStream("Data.txt")));
        out2.writeBytes(
          "Here's the value of pi: \n");
        out2.writeDouble(3.14159);
        out2.close();
        DataInputStream in5 =
          new DataInputStream(
            new BufferedInputStream(
              new FileInputStream("Data.txt")));
        System.out.println(in5.readLine());
        System.out.println(in5.readDouble());
      } catch(EOFException e) {
        System.out.println(
          "End of stream encountered");
      }      // 6. Reading/writing random access files
      RandomAccessFile rf =
        new RandomAccessFile("rtest.dat", "rw");
      for(int i = 0; i < 10; i++)
        rf.writeDouble(i*1.414);
      rf.close();      rf =
        new RandomAccessFile("rtest.dat", "rw");
      rf.seek(5*8);
      rf.writeDouble(47.0001);
      rf.close();      rf =
        new RandomAccessFile("rtest.dat", "r");
      for(int i = 0; i < 10; i++)
        System.out.println(
          "Value " + i + ": " +
          rf.readDouble());
      rf.close();      // 7. File input shorthand
      InFile in6 = new InFile(args[0]);
      String s3 = new String();
      System.out.println(
        "First line in file: " +
        in6.readLine());
        in6.close();      // 8. Formatted file output shorthand
      PrintFile out3 = new PrintFile("Data2.txt");
      out3.print("Test of PrintFile");
      out3.close();      // 9. Data file output shorthand
      OutFile out4 = new OutFile("Data3.txt");
      out4.writeBytes("Test of outDataFile\n\r");
      out4.writeChars("Test of outDataFile\n\r");
      out4.close();    } catch(FileNotFoundException e) {
      System.out.println(
        "File Not Found:" + args[0]);
    } catch(IOException e) {
      System.out.println("IO Exception");
    }
  }
} ///:~

解决方案 »

  1.   

    DataInputStream in = new DataInputStream(new FileInputStream("c:\input.txt"));
      

  2.   

    首先,你应该在你的扑捉异常处
    catch(IOException e){
      System.err.println("error!");
    }
    把你的异常打出来,而不是打error。应该是System.err.println(e.toString());
    这样你会比较清楚的看到提示的错误原因
      

  3.   

    没这么复杂吧?!import java.io.*;
    class FileReaderDemo{
    public static void main(String[] args) throws Exception{
    FileReader fr = new FileReader("input.txt");
    BufferedReader br = new BufferedReader(fr);
    String s;
    while ((s=br.readLine()) != null){
    System.out.println(s);
    }
    fr.close();
    }
    }
      

  4.   

    end of file exception
    怎么处理?
      

  5.   

    draco2002(Draco) :
    你看我的程序能不能修改一下,我实在找不出错
    报错就是  抛出异常EOFException
      

  6.   

    你的文件流写的有问题,,,,你先文件的内容读入到输出流,再将文件写入输入流,.
    DataInputStream in = new DataInputStream(new FileInputStream("input.txt"));
    你好好看看IO...还要写一行,,,这个怎么写呀,,你自己开了还写一行
      

  7.   

    我试了一下 只要不用readUTF 换个.readLine之类的就能读出来内容了阿
    一定要用.readUTF吗?
      

  8.   

    不用readUTF的话能读中文吗?只要能读的话也行
    我现在不是为了实现功能,就是为多用几种方法比较一下学习
    希望各位高手能给出自己的代码,谢谢
      

  9.   

    import java.io.*;
    public class test{
    String s = null;
    public static void main(String[] args){
    try{
       FileReader reader = new FileReader("input.txt");
       BufferedReader in = new BufferedReader(reader);
       while((s = in.readLine()) != null) {
           System.out.println(s);
       }
    }
    catch(IOException e){
    System.err.println(e.getMessage());
    }
    }
    }
      

  10.   

    楼上的有点点错误:应该稍微变点点,就可以了。
        import java.io.*;
    public class test{
        static String s = null;
    public static void main(String[] args){
    try{
      
       FileReader reader = new FileReader("input.txt");
       BufferedReader in = new BufferedReader(reader);
       while((s = in.readLine()) != null) {
           System.out.println(s);
       }
    }
    catch(IOException e){
    System.err.println(e.getMessage());
    }
    }
    }
      

  11.   

    import java.io.*;
    public class test
    {
       public static void main(String[] args)
      {
         
         int data;
         String str="";
         try
         {
             FileReader fRead=new FileReader("input.txt");
              
               while((data=fRead.read())!=-1)
                     str+=(char)data;       }
         catch(IOException e)
         {
              System.err.println("error!");
         }
         System.out.println(str);
      }
    }
      

  12.   

    学习,我现在也正在做一个读文件的程序,但是我的那个文件是C++builder用struct结构写进去的,而且结构中有各种变量,那么我怎么能用java给读出来呢,请高手赐教,解决后另开贴给分
      

  13.   

    public static void generate()
        throws FileNotFoundException, IOException {
            FileInputStream fis = new FileInputStream(filePath);
            BufferedReader reader = new BufferedReader( new InputStreamReader(fis) );
            }
    你只需要这样读就可以了