"Java" reads the contents of a file in a variety of ways, including reading the contents of the file by byte, character, and by unit of behavior __java

Source: Internet
Author: User
Main function: Read the contents of the file in various ways
* <p> includes reading file contents by byte, reading file contents by character, and reading file contents by rows.
* <ul>
* <li> read files in bytes, often used to read binary files, such as pictures, sounds, images, and other files.
* <li> reads files in characters, often used to read text, numbers, and other types of files. To get the standard input byte stream,
* then convert to character stream through bridging, and then buffer to the standard input flow with buffering.

* <li> read files in behavior units, often used to read line-oriented format files.


Package Com.zf.s10.io;
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.Reader;
 /** * @ filename Textreadfile.java * @ Feature reads file contents in various ways * <p> includes reading file contents by byte, reading file contents by character, reading file contents by line.
 * <ul> * <li> read files in bytes, often used to read binary files, such as pictures, sounds, images, and other files. * <li> reads files in characters, often used to read text, numbers, and other types of files.
 To get the standard input byte stream, the * is then converted to character streams via bridging and then buffered for standard input streams with buffering.
 * <li> read files in behavior units, often used to read line-oriented format files.
	 * </ul> * </p> * @ author * @ Date 2010-4-28 * @ Version M1.0 * Description Java Tools class/public class Textreadfile {/**
	 * Read files in bytes, often used to read binary files, such as pictures, sounds, images, and other files. * * @param filename File Absolute path */public static void Readfilebybytes (String filename) {File File = new file (fileName)
		;//Create file InputStream in = null;
			try {System.out.println ("①. Read the contents of the file in bytes, one byte at a time:");
			in = new FileInputStream (file);//Put files into the file input stream int tempbyte; while (tempbyte = In.read ())!=-1 {//read one byte at a time, loop the content read out System.out.write (tempbyte);
			In.close ();//close file input stream} catch (IOException e) {//catch exception 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];//declares a byte array of length 100 int byteread = 0; in = new FileInputStream (filename);//Put the file into the file input stream textreadfile.showavailablebytes (in);///Display the number of bytes left in the input stream/read multiple bytes to byte Array, byteread the number of bytes read in a while ((Byteread = In.read (tempbytes))!=-1) {//Read multiple bytes at a time, looping content read System.out.write (tempbytes
			, 0, Byteread);
		} catch (Exception E1) {//catch exception e1.printstacktrace (); finally {//content total execution if (in!= null) {try {in.close ();//ensure file input stream closes} catch (IOException E1) {E1.pri
				Ntstacktrace ();
	 /** * Reads files in characters, often used to read text, numbers, and other types of files. * * @param filename File Absolute path */public static void Readfilebychars (String filename) {File File = new file (fileName) ;//Create file Reader read = Null
			try {System.out.println ("①. Read the contents of the file in characters, one byte at a time:");
			Read one character at a time = new InputStreamReader (new FileInputStream (file));
			int Tempchar;
				while ((Tempchar = Read.read ())!=-1) {//For Windows, the RN two characters together denote a newline.
				However, if the two characters are displayed separately, the lines will be changed two times. So, shielding off R, or shielding N.
				Otherwise, there will be a lot more empty lines.
				if ((char) tempchar)!= ' R ') {//Read the System.out.print ((char) Tempchar) as long as the line is not wrapped;
		} read.close ();
		catch (Exception e) {e.printstacktrace ();

			try {System.out.println ("②. Read the contents of the file in characters, read multiple bytes at a time:");
			char[] Tempchars = new CHAR[30];
			int charread = 0; Read = new InputStreamReader (new FileInputStream (FileName))//create file read stream while (Charread = Read.read (tempchars))!=-1) {//Once read multiple characters//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;//stops executing the current iteration and then returns the loopAt the beginning} else {System.out.print (tempchars[i]);
		catch (Exception E1) {//catch exception e1.printstacktrace ();
		finally {//content total execution if (read!= null) {try {read.close ();/Ensure Close} catch (IOException e1) {}}
	 }/** * Reads files in a behavior unit and is often used to read line-oriented format files. * * @param filename File Absolute path */public static void Readfilebylines (String filename) {File File = new file (fileName)
		;
			BufferedReader reader = null;//Create cache read try {System.out.println ("①." Read the contents of the file in the behavior unit, read one whole line at a time: ");
			reader = new BufferedReader (new FileReader (file))//place file in cache read 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 ("lines" +
				Line + ":" + tempstring);
			line++;
		} reader.close ();
		catch (IOException e) {//catch exception e.printstacktrace (); finally {//content total execution if (reader!= null) {try {reader.close ();/close Cache read} catch (ioexceptIon E1) {}}}/** * Displays the number of bytes left in the input stream * * @param in */private static void Showavailablebytes (in
		Putstream in) {try {System.out.println (the number of bytes in the current byte input stream is: + in.available ());
		catch (IOException e) {//catch exception e.printstacktrace ();
		} public static void Main (string[] args) {String fileName = ' e:/poem.txt ';
		System.out.println ("1. Read files in bytes:");
		Readfilebybytes (FileName);
		System.out.println ("2. read files by character:");
		Readfilebychars (FileName);
		System.out.println ("3. Read file by unit of conduct:");
	Readfilebylines (FileName);
 }
	
}


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.