Summary of several methods for reading file (binary character) content in JAVA

  • 2020-06-07 04:29:52
  • OfStack

There are many methods to read file contents in JAVA, such as reading file contents according to bytes, reading file contents according to characters, reading file contents according to lines, reading file contents randomly, etc. This paper gives the code for the concrete implementation of the above methods, which can be directly copied and used if needed


public class ReadFromFile {
 /**
  *  Reads a file in bytes, often used for reads 2 Base files, such as pictures, sounds, videos, etc. 
  */
 public static void readFileByBytes(String fileName) {
  File file = new File(fileName);
  InputStream in = null;
  try {
   System.out.println(" Read the contents of the file in bytes, 1 Time to read 1 Bytes: ");
   // 1 Time to read 1 bytes 
   in = new FileInputStream(file);
   int tempbyte;
   while ((tempbyte = in.read()) != -1) {
    System.out.write(tempbyte);
   }
   in.close();
  } catch (IOException e) {
   e.printStackTrace();
   return;
  }
  try {
   System.out.println(" Read the contents of the file in bytes, 1 Multiple bytes read at a time: ");
   // 1 Read multiple bytes at a time 
   byte[] tempbytes = new byte[100];
   int byteread = 0;
   in = new FileInputStream(fileName);
   ReadFromFile.showAvailableBytes(in);
   //  Read multiple bytes into an array of bytes, byteread for 1 The number of bytes read per second 
   while ((byteread = in.read(tempbytes)) != -1) {
    System.out.write(tempbytes, 0, byteread);
   }
  } catch (Exception e1) {
   e1.printStackTrace();
  } finally {
   if (in != null) {
    try {
     in.close();
    } catch (IOException e1) {
    }
   }
  }
 }

 /**
  *  To read a file as a character, often used to read text, Numbers, and other types of files 
  */
 public static void readFileByChars(String fileName) {
  File file = new File(fileName);
  Reader reader = null;
  try {
   System.out.println(" To read the contents of a file in characters, 1 Time to read 1 Bytes: ");
   // 1 Time to read 1 A character 
   reader = new InputStreamReader(new FileInputStream(file));
   int tempchar;
   while ((tempchar = reader.read()) != -1) {
    //  for windows Next, \r\n These two characters are in 1 Start, indicate 1 A line feed. 
    //  But if the two characters are displayed separately, the lines are broken twice. 
    //  So, screen it out \r Or shielding \n . Otherwise, there will be many more blank lines. 
    if (((char) tempchar) != '\r') {
     System.out.print((char) tempchar);
    }
   }
   reader.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  try {
   System.out.println(" To read the contents of a file in characters, 1 Multiple bytes read at a time: ");
   // 1 Read multiple characters at a time 
   char[] tempchars = new char[30];
   int charread = 0;
   reader = new InputStreamReader(new FileInputStream(fileName));
   //  Read multiple characters into a character array, charread for 1 Number of characters read per time 
   while ((charread = reader.read(tempchars)) != -1) {
    //  Again, screen it out \r Don't show 
    if ((charread == tempchars.length)
      && (tempchars[tempchars.length - 1] != '\r')) {
     System.out.print(tempchars);
    } else {
     for (int i = 0; i < charread; i++) {
      if (tempchars[i] == '\r') {
       continue;
      } else {
       System.out.print(tempchars[i]);
      }
     }
    }
   }

  } catch (Exception e1) {
   e1.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e1) {
    }
   }
  }
 }

 /**
  *  Reads files in action units, often used to read line-oriented formatting files 
  */
 public static void readFileByLines(String fileName) {
  File file = new File(fileName);
  BufferedReader reader = null;
  try {
   System.out.println(" Read the contents of the file as an action unit, 1 Time to read 1 The whole line: ");
   reader = new BufferedReader(new FileReader(file));
   String tempString = null;
   int line = 1;
   // 1 Time to read 1 Ok, until read in null End of file 
   while ((tempString = reader.readLine()) != null) {
    //  According to the line Numbers 
    System.out.println("line " + line + ": " + tempString);
    line++;
   }
   reader.close();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e1) {
    }
   }
  }
 }

 /**
  *  Randomly read file contents 
  */
 public static void readFileByRandomAccess(String fileName) {
  RandomAccessFile randomFile = null;
  try {
   System.out.println(" Random reads 1 Section file content: ");
   //  Open the 1 Six randomly accessed file streams in read-only mode 
   randomFile = new RandomAccessFile(fileName, "r");
   //  File length, number of bytes 
   long fileLength = randomFile.length();
   //  Read the starting position of the file 
   int beginIndex = (fileLength > 4) ? 4 : 0;
   //  Move the start position of the read file to beginIndex Position. 
   randomFile.seek(beginIndex);
   byte[] bytes = new byte[10];
   int byteread = 0;
   // 1 Time to read 10 Two bytes if file content is insufficient 10 Reads the remaining bytes. 
   //  will 1 Number of bytes per read byteread
   while ((byteread = randomFile.read(bytes)) != -1) {
    System.out.write(bytes, 0, byteread);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (randomFile != null) {
    try {
     randomFile.close();
    } catch (IOException e1) {
    }
   }
  }
 }

 /**
  *  Displays the number of bytes left in the input stream 
  */
 private static void showAvailableBytes(InputStream in) {
  try {
   System.out.println(" The number of bytes in the current byte input stream is :" + in.available());
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  String fileName = "C:/temp/newTemp.txt";
  ReadFromFile.readFileByBytes(fileName);
  ReadFromFile.readFileByChars(fileName);
  ReadFromFile.readFileByLines(fileName);
  ReadFromFile.readFileByRandomAccess(fileName);
 }
}


Related articles: