当前位置:网站首页>Handling file exceptions
Handling file exceptions
2022-07-07 23:07:00 【Anny Linlin】
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.imageio.stream.FileImageInputStream;
import org.omg.CORBA_2_3.portable.InputStream;
/*
* Copy java file , exception handling
*/
public class CopyWenjian {
public static void main(String[] args) {
OutputStreamWriter osw = null;
InputStreamReader isr = null;
try{
osw = new OutputStreamWriter(
new FileOutputStream("E:\\EclipseCode\\file\\src\\fos.txt"));
isr = new InputStreamReader(
new FileInputStream("E:\\EclipseCode\\file\\fos.txt"));
int len;
char [] chs = new char[1024];
while((len=isr.read(chs))!=-1){
osw.write(chs);
}
}catch(Exception e){
e.printStackTrace();
}finally {
try{
if(isr!=null){
isr.close();
}
}catch(Exception e){
e.printStackTrace();
}
try{
osw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
The above code is JDK5 How to handle file exceptions before .
public static void main(String[] args) {
try(OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("E:\\EclipseCode\\file\\src\\fos.txt"));
InputStreamReader isr = new InputStreamReader(
new FileInputStream("E:\\EclipseCode\\file\\fos.txt"));)
{
int len;
char [] chs = new char[1024];
while((len=isr.read(chs))!=-1){
osw.write(chs);
}
}catch(Exception e){
e.printStackTrace();
}
}
Above is JDK7 Exception handling scheme for .
private static void copyTxt() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("fos.txt"));
InputStreamReader isr = new InputStreamReader(
new FileInputStream("fisr.txt"));
try(osw;isr){
int len;
char [] chs = new char[1024];
while((len=isr.read(chs))!=-1){
osw.write(chs);
}
}catch(Exception e){
e.printStackTrace();
}
}
Above is JDK7 The following scheme for handling file exceptions .
边栏推荐
- PCL . VTK files and Mutual conversion of PCD
- 行测-图形推理-1-汉字类
- 行测-图形推理-5-一笔画类
- 0-5VAC转4-20mA交流电流隔离变送器/转换模块
- Class implementation of linear stack and linear queue (another binary tree pointer version)
- iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
- Leetcode interview question 02.07 Linked list intersection [double pointer]
- 软件测评中心▏自动化测试有哪些基本流程和注意事项?
- 2021-01-12
- ASP. Net core introduction V
猜你喜欢
Talk about DART's null safety feature
Line test - graphic reasoning - 3 - symmetric graphic class
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
【测试面试题】页面很卡的原因分析及解决方案
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
There is another problem just online... Warm
Sword finger offer 27 Image of binary tree
安踏DTC | 安踏转型,构建不只有FILA的增长飞轮
Visual studio 2019 installation
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
随机推荐
Txt file virus
Unity technical notes (I) inspector extension
“拧巴”的早教行业:万亿市场,难出巨头
Redis集群安装
行测-图形推理-2-黑白格类
[network] Introduction to C language
Micro service remote debug, nocalhost + rainbow micro service development second bullet
行测-图形推理-3-对称图形类
Basic knowledge of linked list
Microbial health network, how to restore microbial communities
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
Force deduction - question 561 - array splitting I - step by step parsing
PCL . VTK files and Mutual conversion of PCD
Circumvention Technology: Registry
30讲 线性代数 第五讲 特征值与特征向量
Line test - graphic reasoning - 4 - alphabetic class
Are the microorganisms in the intestines the same as those on the skin?
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
[untitled] reprint melting ice - track icedid server with a few simple steps
Redis cluster installation