1 import java.io.File;
2
3 public class ShowFile {
4
5 public static void main(String[] args) {
6
7 File dir = new File("E:/aaa");
8 if(dir.exists()){
9
10 show(dir, dir);
11 } else {
12 System.out.println("该目录不存在");
13 }
14 }
15
16 /**
17 * show
18 *
19 * @param file1:
20 * 当前对象(目录或者文件)
21 * @param file2:
22 * 根目录
23 */
24
25 public static void show(File file1, File file2) {
26 if (file1.isDirectory()) {
27
28 File[] files = file1.listFiles();
29
30 for (int i = 0; i < files.length; i++) {
31
32 ShowFile sf = new ShowFile();
33 int n = sf.num(files[i], file2);
34
35 for (int j = 0; j < n; j++) {
36 System.out.print("\t");
37 }
38 System.out.println(files[i].getName());
39 //递归调用
40 show(files[i], file2);
41
42 }
43 }
44 }
45
46 int b = 0;
47 public int num(File file1, File file2) {
48
49 // 拿当前的文件和根目录来进行比较
50 //如果file1.getParentFile()和file2相等,则表示是同一个等级的文件夹
51 if (file1.getParentFile().equals(file2)) {
52 return b;
53 } else {
54 b++;
55 return num(file1.getParentFile(), file2);
56 }
57
58 }
59
60 }
1 public class Copy {
2 public static void main(String[] args) throws IOException {
3 String path = "E:"+File.separator+"sdf";
4
5 File file = new File(path ,"abc.rar" );
6
7 File file1 = new File(path,"cba.rar");
8
9 InputStream is = new FileInputStream(file);
10
11 OutputStream ops = new FileOutputStream(file1);
12
13 byte[] by = new byte[20*1024];
14
15 int n = 0;
16
17 while((n=is.read(by))!=-1){
18
19 ops.write(by);
20 System.out.println(n);
21
22 }
23
24 /**
25 *IO流中的read(File file),
26 *从此输入流中读取一个数据字节。如果没有输入可用,则此方法将阻塞。
27 返回:下一个数据字节;如果已到达文件末尾,则返回 -1。
28 read(File file)最终返回的是所读取目标文件的所拥有的字节总数
29 */
30
31 is.close();
32 ops.flush();
33 ops.close();
34
35 }
36
37 }
1 /**
2 *BufferedInputStream 为另一个输入流添加一些功能,即缓冲输入以及 支持 mark 和 reset 方法的能力。
3 *在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。
4 *在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。
5 *mark 操作记录输入流中的某个点,reset 操作使得在从包含的输入流中获取新字节之前,
6 *再次读取自最后一次 mark 操作后读取的所有字节。
7 */
8 public class BufferCopy {
9 public static void main(String[] args) throws IOException {
10 String path = "E:"+File.separator+"sdf"+File.separator+"abc.rar";
11
12 //判断在该路径下是否存在相同文件
13 if(!new File(new File(path).getParentFile(),"sdfd.rar").exists()){
14
15 //创建一个读取File对象的文件数据的输出流
16 InputStream is = new FileInputStream(new File(path));
17 //实现获取数据的输出流
18 BufferedInputStream bis = new BufferedInputStream(is);
19
20 //创建一个写入File对象的文件数据的输出流
21 OutputStream os = new FileOutputStream(new File((new File(path)).getParentFile(),"sdfd.rar"));
22 //实现写入数据的输出流
23 BufferedOutputStream bos = new BufferedOutputStream(os);
24 //创建一个byte[]的缓冲区 保证容量足够大
25 byte[] by = new byte[20*1024];
26 int n = 0;
27 while((n = is.read())!= -1){
28 os.write(by);
29 }
30
31 is.close();
32 os.flush();
33 os.close();
34 }
35 System.out.println("exists");
36
37 }
38
39 }