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;
/** * Java read files in several ways.
* @author Haicheng.cao * */public class ReadFromFile {/** * read files in bytes, often used to read binary files, such as pictures, sounds, images, and other documents.
*/public static void Readfilebybytes (String filename) {File File = new file (fileName);
InputStream in = null;
try {System.out.println (reads 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 multiple bytes at a time: "); Read multiple bytes at a time byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream (fileName);
Readfromfile.showavailablebytes (in); Reads multiple bytes into a byte array, byteread the number of bytes read at a while ((Byteread = In.read (tempbytes))!=-1) {SYSTEM.OUT.W
Rite (tempbytes, 0, Byteread);
} catch (Exception E1) {e1.printstacktrace ();
Finally {if (in!= null) {try {in.close ();
The catch (IOException E1) {}}}/** * reads files in characters, 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 (reads 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, \ r \ n These two characters together indicate a newline.
However, if the two characters are displayed separately, the lines will be changed two times. So, block \ r, or block \ n.
Otherwise, there will be a lot more empty 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 multiple bytes at a time: ");
Read multiple characters at a time char[] Tempchars = new CHAR[30];
int charread = 0;
reader = new InputStreamReader (fileName) (new FileInputStream); Reads more than one character into an array of characters, charread as the number of read characters while (Charread = Reader.read (tempchars))!=-1) {//the same screen off \ R does not display 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 ();
The catch (IOException E1) {}}}/** * Reads a file in a behavior unit and is often used to read a line-oriented format file.
public static void Readfilebylines (String filename) {File File = new file (fileName);
BufferedReader reader = null;
try {System.out.println ("reads the contents of the file in a unit of behavior, reads one whole line at a time:");
reader = new BufferedReader (new FileReader (file));
String tempstring = null;
int line = 1; Read one row at a time until you read null for file end while ((tempstring = Reader.readline ())!= null) {//Show 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) {}}}}/** * Random Read file content */Public STA
tic void Readfilebyrandomaccess (String fileName) {randomaccessfile randomfile = null;
try {System.out.println ("read the contents of a file randomly:");
Open a random Access file stream, read-only randomfile = new Randomaccessfile (FileName, "R");
File length, byte number long filelength = Randomfile.length (); Read the starting position of the file int beginindex = (Filelength > 4)?
4:0; Will read the file openThe start position moves to the beginindex position.
Randomfile.seek (Beginindex);
byte[] bytes = new BYTE[10];
int byteread = 0;
Read 10 bytes at a time and read the remaining bytes if the file content is less than 10 bytes. Assigns a read number of bytes to Byteread while ((byteread = randomfile.read (bytes))!=-1) {System.out.write by
TES, 0, Byteread);
} catch (IOException e) {e.printstacktrace ();
finally {if (randomfile!= null) {try {randomfile.close ();
The 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); }
}