[Bi sett in progress industrial big data, data mining] Java reads text data and converts it into a two-dimensional array

Before the text

One day fishing nine days sun net graduation project. I feel that the mid-term inspection is coming soon. So I was a little flustered these two days. I wrote part of the code (most of it was copied, and a small part of it was changed by myself). So in order to survive my Jane book, I have to put up one thing I wrote today.

text

In fact, there is nothing to say. It is a text reading class. Of course, in order to show the effect, I also left a test data in it. If you have data, delete the test data. This is basically universal. It can be used in many places.

import java.io.*;

public class ReadData {
    public Object[][] ReadData() throws IOException {
        Object[][] rawData = new Object [][]{
                { "<30  ", "High  ", "No ", "Fair     ", "0" },
                { "<30  ", "High  ", "No ", "Excellent", "0" },
                { "30-40", "High  ", "No ", "Fair     ", "1" },
                { ">40  ", "Medium", "No ", "Fair     ", "1" },
                { ">40  ", "Low   ", "Yes", "Fair     ", "1" },
                { ">40  ", "Low   ", "Yes", "Excellent", "0" },
                { "30-40", "Low   ", "Yes", "Excellent", "1" },
                { "<30  ", "Medium", "No ", "Fair     ", "0" },
                { "<30  ", "Low   ", "Yes", "Fair     ", "1" },
                { ">40  ", "Medium", "Yes", "Fair     ", "1" },
                { "<30  ", "Medium", "Yes", "Excellent", "1" },
                { "30-40", "Medium", "No ", "Excellent", "1" },
                { "30-40", "High  ", "Yes", "Fair     ", "1" },
                { ">40  ", "Medium", "No ", "Excellent", "0" }
        };

        File file = new File("/Users/zhangzhaobo/IdeaProjects/Graduation_Design/src/data.txt");  //File for storing array data

        FileWriter DataToTXT = new FileWriter(file);  //File write stream
        int row=0;
        //Writes data from the array to a file. TAB interval between data in each row
        for(int i=0;i<rawData.length;i++){
            for(int j=0;j<rawData[0].length;j++){
                DataToTXT.write(rawData[i][j]+"\t");
            }
            row++;
            DataToTXT.write("\r\n");
        }
        DataToTXT.close();

        BufferedReader in = new BufferedReader(new FileReader(file));
        String line;  //One row of data
        //Read line by line and put each array into the array
        Object[][] DataToOut = new Object[row][5];
        int readrow=0;
        while((line = in.readLine()) != null){
            String[] temp = line.split("\t");
            for(int j=0;j<temp.length;j++){
                DataToOut[readrow][j] = temp[j];
            }
            readrow++;
        }
        in.close();

        //Show read out arrays
        for(int i=0;i<DataToOut.length;i++){
            for(int j=0;j<DataToOut[0].length;j++){
                System.out.print(DataToOut[i][j]+"\t");
            }
            System.out.println();
        }
        return DataToOut;
    }
}

The previous paragraph is to write the data of that two-dimensional array to the specified text. Later, the data is read from the text and parsed into the original two-dimensional array. If we actually have some data, we can directly change the size of the array and the file address.

After the text

In recent days, I have been busy planning graduation activities. And then it's the graduation project. I feel that if I don't finish it, I will gg. Don't fail to graduate then. Everything is empty! My great future is gone!

Tags: Java

Posted on Tue, 31 Mar 2020 02:20:39 -0700 by acidglitter