文件输出/输入异常

时间:2012-11-19 04:05:23

标签: java file exception input output

这个程序在终端窗口中给我一个“例外”。 prevData.txt的位置如代码所示。我的问题是什么?

我正在使用Bluej。还有其他问题吗?请问。

import java.io.*;

public class prevDataReset{

public static void main(String args[]){

 try{
  byte bWrite [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

  OutputStream os = new FileOutputStream("C:/prevData.txt");
  for(int x=0; x < bWrite.length ; x++){
     os.write( bWrite[x] ); // writes the bytes
  }
  os.close();

  InputStream is = new FileInputStream("C:/prevData.txt");
  int size = is.available();

  for(int i=0; i< size; i++){
     System.out.print((double)is.read() + "  ");
  }
  is.close();
  }catch(IOException e){
  System.out.print("Exception");
  } 
  }
  }

3 个答案:

答案 0 :(得分:2)

它对我有用。我建议您没有写入“C:\”的写入权限,请尝试使用user.dir系统属性...

像...一样的东西。

public class SimpleTest {

    public static void main(String args[]) {

        OutputStream os = null;
        InputStream is = null;
        try {
            String userDir = System.getProperty("user.dir");
            byte bWrite[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

            try {
                os = new FileOutputStream(userDir + File.separator + "prevData.txt");
                for (int x = 0; x < bWrite.length; x++) {
                    os.write(bWrite[x]); // writes the bytes
                }
            } finally {
                try {
                    os.close();
                } catch (Exception e) {
                }
            }

            try {
                is = new FileInputStream(userDir + File.separator + "prevData.txt");
                int size = is.available();

                for (int i = 0; i < size; i++) {
                    System.out.print((double) is.read() + "  ");
                }
            } finally {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

好吧,执行此代码并将catch块更改为e.printStackTrace();而不仅仅是System.out.println("Exception");会产生以下错误:

java.io.FileNotFoundException: C:\prevData.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
    at fileexception.prevDataReset.main(prevDataReset.java:11)

确保目录是可写/可读的,然后重试。

答案 2 :(得分:0)

我刚试过


import java.io.*;

public class prevDataReset {
    public static void main(String args[]){

         try{
              byte bWrite [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

              OutputStream os = new FileOutputStream("C:/prevData.txt");
              for(int x=0; x < bWrite.length ; x++){
                 os.write( bWrite[x] ); // writes the bytes
              }
              os.close();

              InputStream is = new FileInputStream("C:/prevData.txt");
              int size = is.available();

              for(int i=0; i < size; i++){
                 System.out.print((double)is.read() + "  ");
              }
              is.close();
          }
          catch(IOException e){
           System.out.print("Exception");
          }
    }
}


它工作正常。
问题不在于代码片段。

希望这会有所帮助。