Java read file method daqo

  • 2020-04-01 03:35:17
  • OfStack

1. Read the contents of the file in bytes


public class ReadFromFile {

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, one byte at a time: ");
//Read one byte at a time
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, read more than one byte at a time: ");
//Read more than one byte at a time
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 once
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) {
}
}
}
}

2. Read the contents of the file by character


public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println(" Read the contents of the file in characters, 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, rn represents a newline when the two characters are together.
//But if the two characters are displayed separately, the lines are changed twice.
//So, block out r, or block out 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(" Read the contents of the file in characters, read more than one byte at a time: ");
//Read more than one character at a time
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
//Read more than one character into a character array, charread is the number of characters read at once
while ((charread = reader.read(tempchars)) != -1) {
//Also block out r not 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) {
}
}
}
}

3. Read the contents of the file by line


public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println(" Read the contents of the file in action units, one line at a time: ");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//Read in one line at a time until null is read in for the end of the 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) {
}
}
}
}

4. Read the contents of the file randomly


public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println(" Read a section of file contents randomly: ");
//Open a random access file stream, read only
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 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, and if the file contents are less than 10 bytes, read the remaining bytes.
//Assign the number of bytes read at once 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) {
}
}
}
}

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);
}
}

Append the content to the end of the file


public class AppendToFile {

public static void appendMethodA(String fileName, String content) {
try {
//Open a random access file stream, read and write
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
//File length, number of bytes
long fileLength = randomFile.length();
//Moves the write file pointer to the end of the file.
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}


public static void appendMethodB(String fileName, String content) {
try {
//Opens a filer. The second parameter in the constructor, true, indicates that the file is written as an append
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
String content = "new append!";
//Append the file as method A
AppendToFile.appendMethodA(fileName, content);
AppendToFile.appendMethodA(fileName, "append end. n");
//Display file contents
ReadFromFile.readFileByLines(fileName);
//Append the file as method B
AppendToFile.appendMethodB(fileName, content);
AppendToFile.appendMethodB(fileName, "append end. n");
//Display file contents
ReadFromFile.readFileByLines(fileName);
}
}

Related articles: