[Learn JAVA from scratch | Article 35] IO flow comprehensive exercise

Table of contents

Foreword:

1. Copy files (including sub-files)

Ideas:

2. File encryption

Ideas:

3. Modify the data in the file:

Ideas:

Summarize:


Foreword:

        In the front we introduced the FILE class and IO class for you. In this article, we will practice some examples of comprehensive use to consolidate our own knowledge.

1. Copy files (including sub-files)

Ideas:

Create a stream for reading files to read files, and a stream for writing files to create files. The overall establishment framework is: if it is a folder, create a folder and continue to access it, and if it is a file, copy it.

import java.io.*;

public class file3 {
    public static void main(String[] args) throws IOException {
        //1.创建对象表示数据源
        File src = new File("D:\\aaa\\bbb");

        //2.创建对象表示目的地
        File dest = new File("D:\\aaa\\dest");

        //3.开始拷贝
        copydir(src,dest);
    }

    private static void copydir(File src, File dest) throws IOException {
        dest.mkdirs();
            //递归
        //1.进入数据源
        File[] files = src.listFiles();
        //依次便利数组
        for (File file : files) {
            if(file.isFile())
            {
                //如果是文件就拷贝
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()));
                byte[]bytes = new byte[1024];
               int len;
                while((len=fis.read(bytes))!=-1)
                {
                    fos.write(bytes,0,len);
                }
                fos.close();
                fis.close();
            }
            else {
                //如果是文件夹就递归
                copydir(file,new File(dest,file.getName()));
            }
        }
    }
}

2. File encryption

Ideas:

Encryption principle: Change every byte of data in the original file, and then store the changed data into a new file.

Decryption principle: read the decrypted file, reverse the operation according to the encryption rules, and turn it into the original file.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class file4 {
    public static void main(String[] args) throws IOException {
        //1.创建对象关联原始文件
        FileInputStream fis = new FileInputStream("c.txt");

        //创建对象关联加密文件
        FileOutputStream fos = new FileOutputStream("jiami.txt");

        //进行加密
        int b;
        while((b=fis.read())!=-1);
        {
            fos.write(b^2);
        }
        fos.close();
        fis.close();


        //进行解密
        //1.创建对象关联原始文件
        FileInputStream fis1 = new FileInputStream("jiami.txt");

        //创建对象关联加密文件
        FileOutputStream fos1 = new FileOutputStream("jiemi.txt");

        //进行解密
        int b1;
        while((b1=fis1.read())!=-1);
        {
            fos1.write(b1^2);
        }
        fos1.close();
        fis1.close();
    }
}

It should be noted that we generally use bit operations to change the encryption method of changing byte data, which can avoid overflow and error reporting caused by arithmetic operations on byte data.

3. Modify the data in the file:

The text file has the following data:

2-1-9-4-7-8

Sorting and outputting the data in the text becomes the following data:

1-2-4-7-8-9

Ideas:

First cut the string, then get the numbers and sort them, and finally create a new file for input.

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class file5 {
    public static void main(String[] args) throws IOException {

        //1.读取数据并拼接成为一个字符串进行操作
        FileReader fr = new FileReader("c.txt");

        StringBuilder sb = new StringBuilder();

        int ch;
        while((ch=fr.read())!=-1)
        {
            sb.append((char) ch);
        }

        fr.close();

        //2.排序

          String s= sb.toString();
        String[] arrstr = s.split("-");

        ArrayList<Integer> list = new ArrayList<>();
        for (String s1 : arrstr) {
            int i = Integer.parseInt(s1);
            list.add(i);

        }

        Collections.sort(list);

        //向新文件写入数据
        FileWriter fw = new FileWriter("b.txt");

        for(int i=0;i<list.size();i++)
        {
            if(i==list.size()-1)
            {
                fw.write(list.get(i)+"");
            }
            else {
                fw.write(list.get(i)+"-");
            }
        }
        fw.close();
    }
}

In fact, we can optimize in the sorting stage, using the knowledge of Stream we learned before.

   Integer[] array = Arrays.stream(sb.toString()
                            .split("-"))
                            .map(Integer::parseInt)
                            .sorted()
                            .toArray(Integer[]::new);

Summarize:

        The exercises about IO stream and File class are not difficult. The key is that we must master the various subclasses of IO stream and File proficiently. The more proficient the better.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

 

Guess you like

Origin blog.csdn.net/fckbb/article/details/132052763