【问题标题】:File output/input exception文件输出/输入异常
【发布时间】:2012-11-19 04:05:22
【问题描述】:

这个程序在终端窗口中给了我一个“异常”。 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");
  } 
  }
  }

【问题讨论】:

  • 打印堆栈跟踪而不是“异常”...它会更有帮助。
  • 你应该打印 e.getMessage() ,这可能会很方便。我看到的第一个危险信号是 Windows 路径使用 \ 而不是 / 。试试看。然后尝试将 prevData 与 javascript 文件放在同一文件夹中并使其成为相对路径
  • 请使用e.printStackTrace();,而不是System.out.print("Exception");,然后发布跟踪。

标签: java file exception input output


【解决方案1】:

它对我有用。我建议您没有写入“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();
        }
    }
}

【讨论】:

    【解决方案2】:

    好吧,执行此代码并将 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)
    

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

    【讨论】:

      【解决方案3】:

      我刚试过

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

      而且效果很好。
      问题不在于代码的 sn-p。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 2015-01-06
        • 1970-01-01
        相关资源
        最近更新 更多