0

This program gives me an "Exception" in the terminal window. prevData.txt is located as shown in the code. What's my problem?

I am using Bluej. Any other questions? Please ask.

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");
  } 
  }
  }
4

3 回答 3

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();
        }
    }
}
于 2012-11-19T04:11:29.350 回答
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)

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

于 2012-11-19T04:13:09.947 回答
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");
          }
    }
}


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

希望这可以帮助。

于 2012-11-19T04:22:04.393 回答