Java reads file content in several common ways

Source: Internet
Author: User

① Random Read of file contents

② read files in behavioral units, often used to read line-oriented format files

③ read files in characters, commonly used for reading text, numbers and other types of files

④ read files in bytes, commonly used for reading binary files, slices, 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);
}

/**
* Random reading of file contents
*/
public static void Readfilebyrandomaccess (String fileName) {
Randomaccessfile randomfile = null;
try {
System.out.println ("Random reading of a file content:");
Open a random Access file stream as read-only
Randomfile = new Randomaccessfile (FileName, "R");
File length, number of bytes
Long filelength = Randomfile.length ();
The starting position of the read file
int beginindex = (Filelength > 4)? 0:0;
Moves the start position of the read file to the Beginindex location.
Randomfile.seek (Beginindex);
byte[] bytes = new BYTE[10];
int byteread = 0;
Read 10 bytes at a time, or read the remaining bytes if the contents of the file are less than 10 bytes.
Assigns the number of bytes read 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 behavioral units, often 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 contents of the file in the Act unit, read a 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 to the end of the file
while ((tempstring = Reader.readline ()) = null) {
Show 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) {
}
}
}
}

/**
* Read files in characters, commonly used for reading 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 ("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, \ r \ n These two characters are together, representing a newline.
However, if the two characters are displayed separately, the lines are changed two times.
Therefore, block \ r, or block \ n. Otherwise, there will be a lot more empty rows.
if ((char) tempchar)! = ' \ r ') {
System.out.print ((char) tempchar);
}
}
Reader.close ();
} catch (Exception e) {
E.printstacktrace ();
}
try {
System.out.println ("\ n reads the contents of the file in characters, reads multiple bytes at a time:");
Read multiple characters at once
char[] Tempchars = new CHAR[30];
int charread = 0;
reader = new InputStreamReader (new FileInputStream (FileName));
Reads multiple characters into a character array, charread the number of characters to read at a time
while ((Charread = Reader.read (tempchars))! =-1) {
Also shield off \ R does 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) {
}
}
}
}
/**
* Read files in bytes, often used to read binary files, slices, 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 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.println (Tempbyte);
}
} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}

try {
System.out.println ("read the contents of the file in bytes, read more than one byte 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 in multiple bytes into a byte array, Byteread is the number of bytes read in at a time
while ((Byteread = In.read (tempbytes))! =-1) {
System.out.write (tempbytes, 0, Byteread);//Good method, the first parameter is an array, the second argument is the start position, the third argument is the length
}
} catch (Exception E1) {
E1.printstacktrace ();
} finally {
if (in = null) {
try {
In.close ();
} catch (IOException E1) {
}
}
}
}

/**
* Shows the number of bytes remaining 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 ();
}
}

}

Java reads file contents in several common ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.