Exception in thread “main“ java.io.FileNotFoundException: x:\xxx\xxx (拒绝访问。)详细解决方案

话不多说先看问题:
在这里插入图片描述

问题描述:

public class FileNotFoundException extends IOException

FileNotFoundException 继承于IOException(读写异常)
FileNotFoundException :指示尝试打开由指定路径名表示的文件失败。

场景在线:

使用字节缓冲流复制单继文件夹中的文件:

package 复制单级文件夹;

import java.io.*;

public class demo1 {
    public static void main(String[] args) throws IOException {
        //1、创键数据源源File对象
        File data_source=new File("D:\\Java\\code");
        //2、获取数据源对象的File对象名称
        String source_name = data_source.getName();
        //3、创键目标源File对象
        File target=new File("File",source_name);
        //4、判断目标文件是否存在,不存在则创键,存在则跳过
        if(!target.exists()){
            target.mkdir();
        }
        //5、获取数据源下的所有文件
        File[] files = data_source.listFiles();
        //6、获取files的文件名称
        for(File f:files){
            String file_name = f.getName();
            File fina=new File(target,file_name);//创键目标源文件路径是target路径拼接数据源文件名;
            copy(data_source,fina);//创键函数使用字节缓冲流从数据源中复制文件至目标文件中
            /*
            * 参数解析:
            * 1、f:(D:\Java\code\aa.jpg) 数据源文件
            * 2、fina:(File\code\aa.jpg) 目标文件
            */
        }
    }

    private static void copy(File f, File re) throws IOException {
        //创键字节缓冲输入流对象————读取数据
        BufferedInputStream r=new BufferedInputStream(new FileInputStream(f));
        //创键字节缓冲输出流对象————写数据
        BufferedOutputStream w=new BufferedOutputStream(new FileOutputStream(re));
        //利用字节数据的方式读取数据
        byte[] c=new byte[1024];
        int len;
        while ((len=r.read(c))!=-1){
            w.write(c,0,len);
            w.flush();//刷新
        }
        //释放资源
        w.close();
        r.close();
    }
}

解决方案:

请将目标锁定在copy参数的位置:
在这里插入图片描述
将两个参数打印出来:
data_source:
在这里插入图片描述
fina:
在这里插入图片描述
发现问题:数据源路径并没有指向文件
打印数据源文件数组:

在这里插入图片描述
在这里插入图片描述

成功解决:将copy的参数data_source改为f

如果你还存在疑惑欢迎私信。

import java.io.BufferedReader; import java.io.FileReader; import java.io.Reader; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Demo { public static void main(String[] args) throws Exception { //1.先选择功能 出题 答题 Scanner scan=new Scanner(System.in); System.out.println("1.出题 2.答题"); int chiose=scan.nextInt(); //2.读取问题数据库 if(chiose==2){ Map<String,String> map=new HashMap<>(); //先读取所有答案 Reader r1=new FileReader("result.txt"); BufferedReader br1=new BufferedReader(r1); String line1=null; while((line1=br1.readLine())!=null){ String[] arr=line1.split("\\."); map.put(arr[0],arr[1]); } //在答题 Reader r2=new FileReader("question.txt"); BufferedReader br2=new BufferedReader(r2); String line=null; while((line=br2.readLine())!=null) { //3.循环显示问题 System.out.println(line); System.out.println("请输入答案:"); String r = scan.next(); //4.我出答案,要对比答案(可以在答题之前先把答案读出来,放在map集合中) String result = map.get(line.split("\\.")[0]); if (r.equalsIgnoreCase(result)) { System.out.println("回答正确"); } else { System.out.println("回答错误"); } } } } } 控制态报Exception in thread "main" java.io.FileNotFoundException: result.txt (系统找不到指定的文件。) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:213) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:152) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:106) at java.base/java.io.FileReader.<init>(FileReader.java:60) at Demo.main(Demo.java:18)怎么解决,什么原因导致的
最新发布
10-20
java.nio.file.FileSystemNotFoundException at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171) at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157) at java.nio.file.Paths.get(Paths.java:143) at com.ruoyi.kettle.tools.KettleUtil$KettleEnv.init(KettleUtil.java:448) at com.ruoyi.kettle.tools.KettleUtil.runKettleJob(KettleUtil.java:238) at com.ruoyi.kettle.service.impl.KettleJobServiceImpl.runJobRightNow(KettleJobServiceImpl.java:259) at com.ruoyi.kettle.tools.RedisStreamUtil.readGroup(RedisStreamUtil.java:251) at com.ruoyi.kettle.tools.CommandLineRunnerImpl$1.run(CommandLineRunnerImpl.java:26) 16:51:35.413 [Thread-3] ERROR c.r.k.t.KettleUtil - [init,461] - Kettle环境初始化失败 java.nio.file.FileSystemNotFoundException public static class KettleEnv { public static void init() { try { //增加自定义的插件库 int lengths = StepPluginType.getInstance().getPluginFolders().size(); URL resourceUrl = KettleUtil.class.getClassLoader().getResource(""); if (resourceUrl == null) { throw new FileNotFoundException("无法定位资源目录"); } Path homePath = Paths.get(resourceUrl.toURI()); Path kettleRoot = homePath.resolve("kettlePlugins").normalize(); String pluginPath = String.valueOf(kettleRoot); log.info("kettle插件路径,{}",pluginPath); if (!StepPluginType.getInstance().getPluginFolders().stream().anyMatch(a -> a.getFolder().equals(pluginPath))) { StepPluginType.getInstance().getPluginFolders().add(new PluginFolder(pluginPath, false, true)); } KettleEnvironment.init(); EnvUtil.environmentInit(); log.info("Kettle环境初始化成功"); } catch (Exception e) { e.printStackTrace(); log.error("Kettle环境初始化失败"); } } } kettle插件在D:\IDEA\java_kettle\smart-kettle\RuoYi-master\ruoyi-admin\src\main\resources\kettlePlugins,我希望执行kettle作业时候直接用本地程序的插件,ruoyi框架打jar包部署,提示上面的情况,怎么调整。
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值