package demo01;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @qvthor liuwenzheng
* @date 2021/5/13 17:32
*/
public class Demo01 {
public static void main(String[] args) throws Exception{
FileInputStream fi = null;
FileOutputStream fo = null ;
try {
//1 准备输入输出流
fi = new FileInputStream("D:\\WeChatSetup.exe");
fo = new FileOutputStream("C:\\des\\eChatSetup.exe");
int len = 0;
byte[] b = new byte[1024];
while ((len = fi.read(b)) != -1) {
fo.write(b, 0, len);
}
}catch (IOException e){
System.out.println("文件路径不存在");
}finally {
fi.close();
fo.close();
}
}
}
Demo02
package demo01;
import java.io.*;
/**
* @qvthor liuwenzheng
* @date 2021/5/13 18:15
*/
public class Demo02 {
public static void main(String[] args) throws IOException {
BufferedInputStream fi = null;
BufferedOutputStream fo = null ;
try {
//1 准备输入输出流
fi = new BufferedInputStream(new FileInputStream("D:\\WeChatSetup.exe"));
fo = new BufferedOutputStream (new FileOutputStream("C:\\des\\eChatSetup.exe"));
int len = 0;
byte[] b = new byte[1024];
while ((len = fi.read(b)) != -1) {
;
}
}catch (IOException e){
System.out.println("文件路径不存在");
}finally {
fi.close();
fo.close();
}
}
}
Demo03
package demo01;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @qvthor liuwenzheng
* @date 2021/5/13 18:24
*/
public class Demo03 {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("D:\\WeChatSetup.exe");
FileWriter writer = new FileWriter("C:\\des\\eChatSetup.exe");
int len = 0 ;
char[] buffer = new char[1024];
while (((len = reader.read(buffer))) != 1){
writer.write(buffer,0,len);
}
reader.close();
writer.close();
}
}
Main
package demo01;
import java.io.File;
public class Main {
public static void main(String[] args) {
// 展示文件树
File f = new File("d:/demo01");
printTree(f, 0);
}
public static void printTree(File f, int level) {
for (int i = 0; i < level; i++) {
System.out.println("\t");
}
System.out.println(f.getAbsolutePath());
if (f.isDirectory()) {
level++;
File[] strs = f.listFiles();
for (int i = 0; i < strs.length; i++) {
File f0 = strs[i];
printTree(f0, level + 1);
}
}
}
}