java io流复制文件简单实例
程序员文章站
2024-03-04 16:37:05
...
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//20191014.txt");
BufferedInputStream input = null;
File file2 = new File("D://QQQ//20191014.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 block
e.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,"20191014.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//20191014.txt");
writer = new FileWriter("D://QQQ//20191014.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();
}
}
}
我是与其终,一个风起云涌的女孩子。
很高兴你能看到我的博客,希望能对你有所帮助。
下一篇: 图像数据处理