当前位置:网站首页>Try with resource close resource flow
Try with resource close resource flow
2022-06-25 05:36:00 【Which floor do you rate moto】
- JDK7 Close stream resource before
public void oldCopyFile(String orignalUrl,String targetUrl){
InputStreamReader is = null;
OutputStreamWriter os = null;
try {
is = new InputStreamReader(new FileInputStream(orignalUrl));
os = new OutputStreamWriter(new FileOutputStream(targetUrl));
char[] arr = new char[2048];
while (is.read(arr) != -1){
os.write(arr);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
// Need to be in final In it, various flows are closed manually , It's very cumbersome
}finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}- JDK7 Close stream resource :try-with-resource: It is not necessary to manually close the flow
/**
* try(
* It is declared in parentheses
* ){
* Resources are used in the execution body
* }
*/
public void NewCopyFile(String orignalUrl,String targetUrl){
try (
InputStreamReader is = new InputStreamReader(new FileInputStream(orignalUrl));
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(targetUrl));
){
char[] arr = new char[2048];
while (is.read(arr) != -1){
os.write(arr);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}- try-with-resource brief introduction
Java7 Introducing new features
Closing resources gracefully
A kind of Java Grammatical sugar : After decompilation, it still adopts jdk7 Close the flow as before
- try-with-resource Use
Automatic shutdown of multiple resources
Resources that need to be automatically closed need to be implemented AutoCloseable Interface
Avoid abnormal shielding ( Will throw out exceptions layer by layer )
- Special case of resource shutdown
Resource object is return Under the circumstances , Closed by caller
At this point, the resource is closed inside the method , The resources obtained by the caller are not available
- ByteArrayInputStream No need to close
Use Socket Acquired InputStream and OutputStream Object does not need to be closed
InputStream and OutputStream Closing an object will result in Socket close
call Socket Of shutdowmInput and shutdownOutput: Will only turn off based on Socket Input and output streams without closing the entire Socket Connect
边栏推荐
- Interface learning
- JS verification code input number auto skip
- A summary of the experiment of continue and break in C language
- Word quickly makes multiple single-sided table labels, number plates, etc
- Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
- Dynamic programming example 1 leetcode 322 coin change
- Creation and use of MySQL index
- CUDA compilation error
- Japanese fifty tone diagram
- Reading and writing of nodejs excel (.Xlsx) files
猜你喜欢

Matlab notes

Penetration test - right raising topic

2022.1.23 diary

Semantic segmentation cvpr2019-advance: advantageous enterprise minimization for domain adaptation in semantic segmentation

Detailed summary of flex layout

Depth of binary tree

渗透测试-提权专题

2022.1.21 diary

Personalized Federated Learning with Moreau Envelopes

Array introduction plus example 01
随机推荐
Create an environment for new projects
Activereportsjs V3.0 comes on stage
HR took the initiative to raise the salary of the test lady. How did she do it?
Basic knowledge of web pages (URL related)
C language -- Sanzi chess
CSRF (Cross Site Request Forgery) &ssrf (server request forgery) (IV)
Japanese fifty tone diagram
On Transform
Rce code execution & command execution (V)
JSON Library Tutorial from scratch (III): parsing strings, learning and sorting notes
Analysis of IM project framework
Deep analysis of epoll reactor code
3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)
2022.1.25
TX Text Control 30.0 ActiveX
Tanhaoqiang C language practice
A brief talk on media inquiry
H5 canvas drawing circle drawing fillet [detailed explanation]
Various pits encountered in the configuration of yolov3 on win10
Deep analysis of recursion in quick sorting