关于 new FileOutputStream("D:/sd/asd/as/das/xx.xxx")出现 系统找不到指定文件的错误异常问题

public class output {
    public static void main(String[] args) {
        try {
            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(new File("D:\\123\\vmware.doc")));
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("D:\\123qwe.doc"));
            byte[] bytes = new byte[2048];
            int len;
            while (-1 != (len = inputStream.read(bytes))) {
                outputStream.write(bytes, 0, len);
            }
            System.out.println("结束");
        inputStream.close();
        outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        }
    }
}

 

出现错误java.io.FileNotFoundException: D:\1233\123\123\123\qwe.doc (系统找不到指定的路径。)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
    at fileStream.output.main(output.java:16)

原因是,一般认为new FileOutputStream()有则读出到该文件,没有则创建文件,指的是创建文件,只会在根目录下面才会生效,如果输出的路径包含了不存在的文件夹,则会报错: 系统找不到指定的路径。

所以,为了避免出现这个错误,一般会首先半段文件夹是否存在,不存在就要创建。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值