当前位置:网站首页>Record the time to read the file (the system cannot find the specified path)
Record the time to read the file (the system cannot find the specified path)
2022-06-25 15:23:00 【Running pig ZC】
Catalog
2.1: Environmental preparation
1: Write it at the front
We usually do projects , It must involve the reading of files , But there is such a situation occasionally , That is, the project is good when tested locally , Once packaged into jar package ( At present, the mainstream architecture generally uses microservices , Convenient and quick ) function , The file path cannot be found java.lang.RuntimeException: file\test.txt ( The system could not find the specified path .) Similar questions , A thorough analysis of the problems encountered by the author before .
2: The process
2.1: Environmental preparation
1: Create a springBoot project , Structure is shown in figure , among test.txt The content is casual 
2:service structure

3:controller structure

2.2: Result demonstration
1: Start the project locally for testing , The result was perfect , You can read the file

2: So let's use maven The tool compiles the project , pack ---- > formation read_file-0.0.1-SNAPSHOT.jar

3: find jar Directory address

4: I tested it with windows System , Enter... Directly under the current directory cmd, perform cmd command ( This operation is an excellent one ), Then a small black box pops up , The project started successfully

5: Next, let's continue to call the interface to get the file to see the results , The result is perfect and the result we want

2.3: improvement
1: This time we are reading the file , Use getResourceAsStream Read out

2: Local read succeeded , No problem

3: Back to the previous , Type the project as a jar Package to run , See if you can read . The same way as before .

4: Run this new jar, Still use the previous operation , To the current directory of the project , Input cmd function cmd Command line

5: test result , No report error , The description file has been read .

3: summary
Why use the former method , You can run locally , Just put it on the server , The reason is that this file does exist locally , Can directly File Class to read files , But once it's packaged , because springboot The project will be packaged into a jar package , Read jar What's in it , It is definitely not feasible to use the previous method of reading files , Want to read a jar What's inside , We can use class loaders ---> getResourceAsStream(path) The method provided by , Bear in mind , Bear in mind , Bear in mind .
The source code has been uploaded to Code cloud hosting Address : https://gitee.com/zccpojo/read_file
边栏推荐
- basic_ String mind map
- Common operations in VIM
- QT opens the print dialog box in a text editor
- [paper notes] overview of case segmentation
- Graphic control and layout basis of R visualization
- Software packaging and deployment
- Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
- [paper notes] contextual transformer networks for visual recognition
- System Verilog - function and task
- The last glory of the late Ming Dynasty - the battle of Korea
猜你喜欢

Learning C language today is the first time to learn C language. In college, C linguistics is not good, but I want to make progress, so I found a beep video on the Internet to learn C language

QT pattern prompt box implementation

How to download and install Weka package

1090.Phone List

Stack and queue

Common dynamic memory errors

QT excel table read / write library - qtxlsx

Sequential programming 1

Introduction to flexible array

Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
随机推荐
Stack and queue
Custom structure type
New title of PTA
Shared memory synchronous encapsulation
(2) Relational database
Vs2019 scanf error
One question per day, a classic simulation question
Esp8266 building smart home system
Sampling method and descriptive statistical function in R language
QT loading third-party library basic operation
Go language template text/template error unexpected EOF
Core mode and immediate rendering mode of OpenGL
What moment makes you think there is a bug in the world?
QT source code online view
p1408
QT set process startup and self startup
Compile Caffe's project using cmake
System Verilog - function and task
3. Sequential structure multiple choice questions
Go build reports an error missing go sum entry for module providing package ... to add: