【java代码】
- String dir = System.getProperty("user.dir");
- Scanner in = new Scanner(new File(dir));
- System.out.printf("%s", in.nextLine().toString());
出错:Exception in thread "main" java.io.FileNotFoundException: G:\WorkZone\Eclipse\test1 (拒绝访问。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.
(FileInputStream.java:106)
at java.util.Scanner.(Scanner.java:636)
at rbsrcy.Main.main(Main.java:70)
原因:
原因在实例化File file=new File(fileName); 的时候fileName是一个目录,而不是文件路径
决解方法:
- String dir = System.getProperty("user.dir");
- //在Eclipse集成开发环境,启动路径将有IDE控制,可以使用下面语句找到路径位置
- Scanner in = new Scanner(new File(dir+"\\myfile.txt"));
- //或者用绝对路径
- //Scanner in = new Scanner(new File("G:\\WorkZone\\Eclipse\\test1\\bin\\rbsrcy\\myfile.txt"));
- System.out.printf("%s", in.nextLine().toString());
阅读(7968) | 评论(0) | 转发(0) |