IO练习题

package leif;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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.io.InputStreamReader;

import org.junit.Test;

public class IOExercises {
    public static final String KEYWORDS = "const, goto, public, protected, private, class, interface, abstract, implements, extends, new, import, package, byte, short, int, long, float, double, char, boolean, void, null, true, false, if, else, while, for, switch, case, default, do, break, continue, return, instanceof, static, final, super, this, native, strictfp, synchronized, transient, volatile, try, catch, finally, throw, throws, enum, assert";

    /*
     * 将一个.java文件转为.html文件,关键字和注释分别用粗体的红色和斜体的绿色表示。
     */
    @Test
    public void test1() {
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;

        try {
            fileInputStream = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\test1.txt"));
            inputStreamReader = new InputStreamReader((fileInputStream), "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            fileWriter = new FileWriter("C:\\Users\\Administrator\\Desktop\\test1.html");
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write("<!DOCTYPE html>\n<html>\n  <head>\n    <meta charset=\"UTF-8\">\n    <title>test1</title>\n  </head>\n  <body>\n    <pre>\n");
            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                for (String s : KEYWORDS.split(", ")) {
                    line = line.replace(s + " ", "<span style=\"color: red; font-weight: bolder;\">" + s + "</span> ");
                }

                line = line.replace("/*", "<span style=\"color: green; font-style: italic;\">/*").replace("*/", "*/</span>");
                bufferedWriter.write("      " + line + "\n");
            }

            bufferedWriter.write("    </pre>\n  </body>\n</html>");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*
     * 复制目录及文件
     */
    @Test
    public void test2() throws FileNotFoundException {
        copy("D:\\Java", "D:\\Java2");
    }

    public static void copy(String oldPath, String newPath) throws FileNotFoundException {
        File oldFile = new File(oldPath);

        if (!oldFile.exists()) {
            throw new FileNotFoundException("路径错误!");
        }

        for (File file : oldFile.listFiles()) {
            File newFile = new File(file.getPath().replace(oldPath, newPath));

            if (file.isDirectory()) {
                if (!newFile.exists()) {
                    newFile.mkdirs();
                }

                copy(file.getPath(), file.getPath().replace(oldPath, newPath));
            } else {
                FileInputStream fileInputStream = null;
                BufferedInputStream bufferedInputStream = null;
                FileOutputStream fileOutputStream = null;
                BufferedOutputStream bufferedOutputStream = null;

                try {
                    fileInputStream = new FileInputStream(file);
                    bufferedInputStream = new BufferedInputStream(fileInputStream);
                    fileOutputStream = new FileOutputStream(newFile);
                    bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    byte[] bs = new byte[1024];
                    int length = -1;

                    while ((length = bufferedInputStream.read(bs)) != -1) {
                        bufferedOutputStream.write(bs, 0, length);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (bufferedOutputStream != null) {
                        try {
                            bufferedOutputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if (bufferedInputStream != null) {
                        try {
                            bufferedInputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    /*
     * 创建一个D:\HelloWorld.txt目录,并判断它是文件还是文件夹,再创建一个D:\IOTest目录,并将HelloWorld.txt移动到IOTest目录中,然后遍历此目录
     */
    @Test
    public void test3() {
        File file1 = new File("D:\\HelloWorld.txt");

        if (!file1.exists()) {
            try {
                file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (file1.isDirectory()) {
            System.out.println("文件夹");
        } else {
            System.out.println("文件");
        }

        File file2 = new File("D:\\IOTest");

        if (!file2.exists()) {
            file2.mkdirs();
        }

        file1.renameTo(new File(file2.getPath() + File.separator + file1.getName()));

        for (File file : file2.listFiles()) {
            System.out.println(file);
        }
    }

    /*
     * 递归实现输入任意目录,列出所有文件夹及其所有文件
     */
    @Test
    public void test4() {
        show(".");
    }

    public static void show(String path) {
        for (File file : new File(path).listFiles()) {
            System.out.println(file);

            if (file.isDirectory()) {
                show(file.getPath());
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容