欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java中的IO流

程序员文章站 2024-03-04 15:36:47
...

Java中的IO流

IO流体系图

Java中的IO流

通过捕捉获取来修改异常

public class MyTest {
    public static void main(String[] args) {
        FileOutputStream out=null;
        try {
          out = new FileOutputStream("a.txt");
            out.write(100);

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

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

用字节流复制单层文件

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("MyTest.txt");
        FileOutputStream out = new FileOutputStream("B.txt");
        int len=0;
        byte[] bytes = new byte[1024];
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
        in.close();
        out.close();
    }
}

高效字节流复制文件,通过写入字节缓冲区,在将缓冲区的数据一次性写入文件,可以提高读写效率

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("E:\\领悟.mp3"));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("领悟.mp3"));
        int len=0;
        byte[] bytes = new byte[1024 * 8];
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }
}

用字符流高效流复制文本文件

public class MyTest2 {
    public static void main(String[] args)  {
        BufferedReader in=null;
        BufferedWriter writer=null;
        try {
            in= new BufferedReader(new FileReader("MyTest.txt"));
            writer= new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\MyTest.java"));
            int len=0;
            char[] chars = new char[1024];
            while ((len=in.read(chars))!=-1){
                writer.write(chars,0,len);
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                    if (writer!=null){
                        writer.close();
                    }
                    } catch(IOException e){
                        e.printStackTrace();
                    }
                }
        }
    }

把Arrlist集合中的文件存入到文本文件中

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        BufferedWriter writer = new BufferedWriter(new FileWriter("C.txt"));
        list.add("乔布斯");
        list.add("库克");
        list.add("比尔科茨");
        list.add("乔丹");
        for (String s : list) {
                writer.write(s);
                writer.newLine();
                writer.flush();
            }
        writer.close();
    }
    }

遍历集合,取出数据,写入文本文件,writer.newline()重新写入一行

利用集合和字符流编一个点名系统

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("C.txt"));
        ArrayList<String> list = new ArrayList<>();
        String str=null;
        while ((str=reader.readLine())!=null){
            list.add(str);
        }
        Random random = new Random();
        int i = random.nextInt(list.size());
        System.out.println(list.get(i));
    }
    }

复制多级文件夹

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\MyTest");
        File file1 = new File("E:\\MyTest2");
        if (!file1.exists()) {
            file1.mkdirs();
        }
        copyFolder(file, file1);
    }

    private static void copyFolder(File file, File file1) throws IOException {
        File[] files = file.listFiles();
        for (File file2 : files) {
            if (file2.isFile()) {
                copyFiles(file2, file1);
            } else {
                File file3 = new File(file1, file2.getName());
                if (!file3.exists()) {
                    file3.mkdirs();
                }
                copyFolder(file2, file3);
            }
        }
    }
    private static void copyFiles(File file2, File file1) throws IOException {
        FileInputStream in = new FileInputStream(file2);
        FileOutputStream out = new FileOutputStream(new File(file1,file2.getName()));
        int len = 0;
        byte[] bytes = new byte[1024 * 8];
        while ((len = in.read(bytes)) != -1) {
            out.write(bytes, 0, len);
            out.flush();
        }
        in.close();
        out.close();
    }
}

将多首歌合并成一首歌,

public class MyTest3 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("E:\\领悟.mp3");
        FileInputStream in1 = new FileInputStream("E:\\许巍-曾经的你.mp3");
        FileOutputStream out = new FileOutputStream("E:\\大合唱.mp3");
        ArrayList<FileInputStream> list = new ArrayList<>();//创建集合,把两首歌放到一个集合中
        list.add(in);
        list.add(in1);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        int len=0;
        byte[] bytes1 = new byte[1024 * 8];
        for (FileInputStream fileInputStream : list) {//遍历集合,把数据放到内存操作流中
            while ((len=fileInputStream.read(bytes1))!=-1){
                bytes.write(bytes1,0,len);
            }
        }
        in.close();
        in1.close();
        byte[] bytes2 = bytes.toByteArray();//取出数据,转换成字节数组
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes2);
        int len2=0;
        byte[] bytes3 = new byte[1024 * 8];
        while ((len2=byteArrayInputStream.read(bytes3))!=-1){
            out.write(bytes3,0,len2);
        }
        out.close();
    }
}

利用Scanner和字节打印流复制文本文件

public class MyTest4 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileInputStream("MyTest.txt"));
        PrintWriter printWriter = new PrintWriter("MyTest2.txt");
        while (scanner.hasNextLine()){
            String string = scanner.nextLine();
            printWriter.println(string);
        }
scanner.close();
        printWriter.close();
    }
}

键盘的第二种录入方式

public class MyTest4 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //InputStream in = System.in;
        while (true){
            System.out.println("请输入数据");
            String string = reader.readLine();
            System.out.println(string);
            if ("bye".equals(string)){
                break;
            }
        }
    }
}