“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

JAVA operation files are often used, this article summarizes some JAVA operation files read common tool classes, I hope to help you. Go straight to the code.

First, read the file into bytes

To convert the contents of a file into bytes, use the FileInputStream byte input stream, enter the file into the file byte input stream, use the FileInputStream available() method to get the number of bytes associated with the file, and then use the read() method to read the data. Finally, turn off the file byte stream.

// Read files as byte arrays
  public static byte[] file2byte(String path){
        try {
            FileInputStream in =new FileInputStream(new File(path));
            byte[] data=new byte[in.available()];
            in.read(data);
            in.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null; }}Copy the code

Write bytes to a file

Similar to reading a file into bytes in 1, writing bytes to a file uses the FileOutputStream stream to write bytes to a file. Call FileOutputStream’s write () method, write data, and then close the stream.

 // Write the byte array to the file
  public static void byte2file(String path,byte[] data) {
        try {
            FileOutputStream outputStream  =new FileOutputStream(new File(path));
            outputStream.write(data);
            outputStream.close();
        } catch(Exception e) { e.printStackTrace(); }}Copy the code

Read the file as a list by row

It is common to have to output text in a document as a line, which we can handle using the BufferedReader and InputStreamReader streams. The specific code is as follows.

 // Read the file as a list by line
  public static ArrayList<String> file2list(String path,String encoder) {
        ArrayList<String> alline=new ArrayList<String> ();try {
            BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
            String str=new String(a);while ((str=in.readLine())! =null) {
                alline.add(str);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alline;
    }

Copy the code

Print list to file

 // Print the list to the file
  public static void list2file(String path,ArrayList<String> data,String encoder) {
        try {
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
            for (String str:data) {
                out.write(str);
                out.newLine();
            }
            out.flush();
            out.close();
        } catch(Exception e) { e.printStackTrace(); }}Copy the code

Read from standard input

  // Read from standard input
  public static String system2str() throws IOException{
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
        return stdin.readLine();
    }

Copy the code

Read the file as a string

// Read the file as a string
  public static String file2str(String path,String encoder){
        StringBuilder sb=new StringBuilder();
        try {
            BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
            String str=new String(a);while ((str=in.readLine())! =null) {
                sb.append(str);
            }
            in.close(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

Copy the code

7. Output string to file

 // Outputs a string to a file
  public static void str2file(String path,String data,String encoder){
        try {
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
            out.write(data);
            out.flush();
            out.close();
        } catch(Exception e) { e.printStackTrace(); }}Copy the code

Eight, read the file into the data matrix

// Read file into data matrix
  public static ArrayList<Double> file2matrix(String path){
        ArrayList<Double> alldata=new ArrayList<Double>();
        try {
            DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
            // Use DataInputStream to read data
            while(true)
            {
                alldata.add(in.readDouble()); }}catch (Exception e) {
            e.printStackTrace();
        }
        return alldata;
    }
Copy the code

conclusion

There are many ways to operate files. This article uses only a basic reference example. Welcome to learn and exchange. Thanks for reading.