There are several common ways for java to read file content

① Randomly read the contents of the file

②Read files in line units, often used to read line-oriented formatted files

③ Read files in units of characters, often used to read text, numbers and other types of files

④Read files in bytes, often used to read binary files, such as pictures, sounds, images and other files

package com.control;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;

public class ReadFromFile {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:/objectUser.txt";
// readFileByBytes(fileName);
// readFileByChars(fileName);
//readFileByLines(fileName);
// readFileByRandomAccess(fileName);
}

/**
* Randomly read file content
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("Randomly read a file content: ");
// Open a random access file stream, Read-only
randomFile = new RandomAccessFile(fileName, "r");
// file length, number of bytes
long fileLength = randomFile.length();
// starting position of read file
int beginIndex = (fileLength > 4) ? 0 : 0;
// Move the starting position of the read file to the beginIndex position.
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// Read 10 bytes at a time, if the file content is less than 10 bytes, read the remaining bytes.
// assign the number of bytes read at a time to 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) {
}
}
}
}
/**
* Read files in line units, commonly used to read line-oriented format files
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("Read the file content in line units, one whole line at a time: ");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// read one line at a time until null is read as end of file
while ((tempString = reader.readLine()) != null) {
// display line number
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

/**
* in characters Read files, 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("Character Read the file contents in units, one byte at a time: ");
// read one character at a time
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// For Windows, when \r\n these two characters are together, Indicates a newline.
// But if the two characters are displayed separately, it will wrap twice.
// So mask out \r, or mask out \n. Otherwise, there will be a lot of blank lines.
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("\nRead the file content in character units, read multiple bytes at a time: ");
// Read multiple characters at a time
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// Read multiple characters into the character array, charread is the number of characters read at a time
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
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) {
}
}
}
}
/**
* Read files in bytes, often used to read binary files, such as pictures, sounds, images and other files.
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("Read the file contents in bytes, one byte at a time: ");
// read one byte at a time
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read())!=-1) {
System.out.println(tempbyte);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

try {
System.out.println("Read the file content in bytes, multiple bytes at a time: ");
// once Read multiple bytes
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// Read multiple bytes into a byte array, byteread is the number of bytes read at a time
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);//Good method, the first parameter is the array, the second parameter is the start position, and the third parameter is the length
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}

/**
* show what is left in the input stream Number of bytes
*/
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();
}
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325037657&siteId=291194637
Recommended