开发者社区> 问答> 正文

java file操作 问题 报错

"

import java.io.*;

public class file2 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    File f= new File("D\\xx\\te.txt") ; 
    OutputStream out = null ; 

    try
    {

        out = new FileOutputStream(f) ; 

    }
    catch(Exception e)
    {
        System.out.println("asdas");
    }

    String a =new String("sadasdas")  ;
    byte[] b = a.getBytes() ;


    try
    { 
        out.write(b);

    }
    catch(Exception e)
    {
        System.out.println("asdas");
    }

    try
    {
        out.close() ;

    }
    catch(Exception e)
    {
        System.out.println("asdas");
    }

    InputStream in = null ; 
    try
    {
        in =    new FileInputStream(f ) ; 

    }
    catch(Exception e)
    {
        System.out.println("asdas");
    }

    byte[] d = new byte[1024] ; 
    int i= 0 ; 
    try
    {
        i = in.read(d) ; 
        in.close() ;

        System.out.println(i);
    }
    catch(Exception e)
    {
        System.out.println("asdas");
    }


    System.out.println(i);

}

}

调试了一下,发现好像所有的try都进catch 了, 感觉上自己应该没错啊,就是输不出我要的结果。

" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/65d366c63e74431ca6a884569f15fab8.png)

展开
收起
因为相信,所以看见。 2020-05-26 13:57:13 606 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    "

    这问题就不该给答案,你都知道它catch到exception了,就不去看一下catch到的是什么东西么?

    ######

    不知道你出的问题是什么。。。。

    ######

    File f= new File("D\xx\te.txt");

    看看 路径 正确吗?

    起码得是这样吧:

    File f= new File("D:\\xx\\te.txt");

    " ![image.png](https://ucc.alicdn.com/pic/developer-ecology/4ccc5bee8aaa4a1f931d91b3bd661759.png)
    2020-05-27 10:06:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载