1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java io流复制文件简单实例

java io流复制文件简单实例

时间:2023-04-20 11:33:14

相关推荐

java io流复制文件简单实例

java io流复制文件简单实例

实例1:

package com.io;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.util.Scanner;import javax.xml.soap.SAAJResult;public class IoDemo2 {public static void main(String[] args) {File file = new File("E://ABCJava//ZYQ//1014.txt");BufferedInputStream input = null;File file2 = new File("D://QQQ//1014.txt");BufferedOutputStream output = null;try {input = new BufferedInputStream(new FileInputStream(file));output = new BufferedOutputStream(new FileOutputStream(file2));byte[] buffer = new byte[4096];int len = 0;if(file2.exists()){System.out.println("文件已存在,是否覆盖 Y|N");Scanner in = new Scanner(System.in);String x = in.next();if(x.equals("Y") ){System.out.println("已覆盖");}else{System.out.println("覆盖失败");}}else{while((len=input.read(buffer))!=-1){output.write(buffer,0,len);output.flush();}}output.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

实例2:

package com.date;import java.io.File;import java.io.IOException;public class IODemo {public static void main(String[] args){File file = new File("E://ABCJava//ZYQ");if(!file.exists()){file.mkdir();}else{System.out.println("目录文件已存在");}if(file.isDirectory()){System.out.println("是目录");}File file1 = new File(file,"1014.txt");if(!file1.exists()){try {file1.createNewFile();} catch (IOException e) {e.printStackTrace();}}else{System.out.println("文件已存在");}}}

实例3:

package com.date;import java.io.FileReader;import java.io.FileWriter;import java.io.Reader;import java.io.Writer;public class StreamDemo {public static void main(String[] args) {long startTime = System.currentTimeMillis();Reader reader = null;Writer writer = null;try {reader = new FileReader("E://ABCJava//ZYQ//1014.txt");writer = new FileWriter("D://QQQ//1014.txt");char[] buffer = new char[2048];int len = 0;while ((len = reader.read(buffer)) != -1) {writer.write(buffer, 0, len);}reader.close();writer.close();long endTime = System.currentTimeMillis();System.out.println("文件复制成功");System.out.println("耗时:" + (endTime - startTime));} catch (Exception e) {e.printStackTrace();}}}

我是与其终,一个风起云涌的女孩子。

以下是我的公众号,主要写的是有关于前段后台数据库,面试技巧,学习方法,职场心得等,感兴趣的可以关注一下哦。

很高兴你能看到我的博客,希望能对你有所帮助。

qq群:可直接扫码进qq群,或者输入群号670983672。

微信群:可加下方博主微信,备注“进群”即可。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。