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;
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();
}
}
}
}
以上代码是JDK5之前处理文件异常方式。
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();
}
}
以上是JDK7的处理异常方案。
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();
}
}
以上是JDK7之后的处理文件异常方案。



