[英]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.