当前位置:网站首页>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 .
边栏推荐
- Class implementation of linear stack and linear queue (another binary tree pointer version)
- Some parameters of Haikang IPC
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- 【刷题记录】3. 无重复字符的最长子串
- CTF练习
- Unity technical notes (I) inspector extension
- 聊聊 Dart 的空安全 (null safety) 特性
- 定位到最底部[通俗易懂]
- CTF exercise
- What is fake sharing after filling the previous hole?
猜你喜欢
Talk about DART's null safety feature
Line test graph reasoning graph group class
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Common verification rules of form components -2 (continuously updating ~)
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
行测-图形推理-3-对称图形类
Unity and webgl love each other
消费品企业敏捷创新转型案例
Quick sort (diagram +c code)
The author of LinkedList said he didn't use LinkedList himself
随机推荐
How to operate DTC community?
行测-图形推理-3-对称图形类
不夸张地说,这是我见过最通俗易懂的,pytest入门基础教程
Debezium系列之: 支持在 KILL 命令中使用变量
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
What is ADC sampling rate (Hz) and how to calculate it
The author of LinkedList said he didn't use LinkedList himself
Line test - graphic reasoning -5- one stroke class
Leetcode1984. Minimum difference in student scores
DTC社群运营怎么做?
This time, let's clear up: synchronous, asynchronous, blocking, non blocking
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2
Sword finger offer 63 Maximum profit of stock
What does the model number of asemi rectifier bridge kbpc1510 represent
安踏DTC | 安踏转型,构建不只有FILA的增长飞轮
Digital transformation: five steps to promote enterprise progress
行测-图形推理-7-相异图形类
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
oc 可变參数传递
每日一题——PAT乙级1002题