简体   繁体   English

读取资源文件夹中的文件时出现 FileNotFoundException

[英]FileNotFoundException when reading file in resources folder

Code:代码:

  String filePath = "src//main//resources//QR_CODE//QR_Code.png";
            //String filePath = "C:\\Work\\QR_CODE\\QR_Code.png";
            String charset = "UTF-8"; // or "ISO-8859-1"
            Map <EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap < EncodeHintType, ErrorCorrectionLevel > ();
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            BitMatrix matrix = new MultiFormatWriter().encode(
                    new String(qrCodeData.getBytes(charset), charset),
                    BarcodeFormat.QR_CODE, 200, 200, hintMap);
            MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
                    .lastIndexOf('.') + 1), new File(filePath));

Output: Output:

12:33:09,948 ERROR [stderr] (default task-17) java.io.FileNotFoundException: src\main\resources\QR_CODE\QR_Code.png (The system cannot find the path specified)
12:33:09,948 ERROR [stderr] (default task-17)   at java.io.RandomAccessFile.open0(Native Method)
12:33:09,950 ERROR [stderr] (default task-17)   at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
12:33:09,950 ERROR [stderr] (default task-17)   at java.io.RandomAccessFile(RandomAccessFile.java:243)
12:33:

if you want to use dynamic addressing in linux based system:如果您想在基于 linux 的系统中使用动态寻址:

String filePath = System.getProperty("user.dir")+"/path/to/file/QR_Code.png";

in windows based system:在基于 windows 的系统中:

String filePath = System.getProperty("user.dir")+"\\path\\to\\file\\QR_Code.png";

System.getProperty("user.dir") is your project address. System.getProperty("user.dir") 是你的项目地址。

if you want to use static addressing in linux based system:如果您想在基于 linux 的系统中使用 static 寻址:

String filePath = "/home/username/eclipse-workspace/test/path/to/file/QR_Code.png";

in windows based system:在基于 windows 的系统中:

String filePath = "C:\\Work\\QR_CODE\\QR_Code.png";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从资源文件夹中读取 txt 文件时出现 Java FileNotFoundException - Java FileNotFoundException when trying to read txt file from resources folder Spring boot:从 jar 读取时尝试读取资源文件夹中的目录内容会产生 FileNotFoundException - Spring boot: trying to read directory content inside resources folder produces FileNotFoundException when reading from jar 读取属性文件时出现 FileNotFoundException - FileNotFoundException when reading properties file 使用 getResource() 从资源文件夹获取文件时出现 FileNotFoundException - FileNotFoundException while getting file from resources folder using getResource() FileNotFoundException启动jar-在资源文件夹中看不到文件 - FileNotFoundException starting jar - can't see file in resources folder 使用绝对路径读取文件时出现FileNotFoundException - FileNotFoundException when reading a file, using absolute path 读取文件时出现 java.io.FileNotFoundException - java.io.FileNotFoundException when reading a file 读取.xml文件进行解析时出现FileNotFoundException - FileNotFoundException when reading .xml file to parse 使用 SpringBoot 读取资源文件夹中可用的文件 - Reading file available in resources folder with SpringBoot 读取文件时出现FileNotFoundException - FileNotFoundException while reading file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM