How to read the contents of a file by using the Java O stream
This article mainly introduces how to use Java to read the contents of the file, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The details are as follows:
In order to read the contents of a file using the I _ Band O stream, you must first master the architecture of InputStream.
In this system, FileInputStream and BufferedInputStream must be mastered because of the high frequency of use.
InputStream method: InputStream is located under the java.io package.
The method of OutputStream:
Read the file (code):
Package com.jredu.oopch21;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;/** * the concept of a stream of data that flows to a sequence of data from an object and reaches that object. * input stream: the process of data source data flowing to computer memory * output stream: the process of moving data from a program to a target data source * @ author Administrator * / public class Ch01 {/ * read file contents * @ param args * / public static void main (String [] args) {/ / InputStream: is an abstract class / /\: is a transfer character / / representation Two representations of the disk path: 1. \\ 2, / try {/ / read the contents from the file address into the program / / 1, establish a connection InputStream is = new FileInputStream ("E:/iodemo/ch01.txt") / / 2. Start reading information / * / method 1: read only one System.out.println (is.read ()) at a time; / / read byte type: 49 System.out.println ((byte) is.read ()); / / 50 * / / method 2: define an array, loop read / / first define a byte array to store data byte [] b = new byte [5] / / read all the data into this byte / / declare an int store to read data int I = 0; / / define a variable int index = 0 for a record index; / / read each data while ((i=is.read ())! =-1) {/ / put the read data into I b [index] = (byte) I; index++ } / / convert the byte array to a string System.out.println (new String (b)); / / close the stream is.close ();} catch (FileNotFoundException e) {/ / TODO Auto-generated catch block / / the problem forced by the system: file did not find e.printStackTrace ();} catch (IOException e) {/ / file read / write exception / / TODO Auto-generated catch block e.printStackTrace ();}
Package com.jredu.oopch21;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Arrays;/** * the concept of a stream of data that flows to a sequence of data from an object and reaches that object. * input stream: the process of data source data flowing to computer memory * output stream: the process of moving data from a program to a target data source * @ author Administrator * / public class Ch02 {/ * read file contents * @ param args * / public static void main (String [] args) {/ / InputStream: is an abstract class / /\: is a transfer character / / representation Two representations of the disk path: 1. \\ 2, / try {/ / read the contents from the file address into the program / / 1, establish a connection InputStream is = new FileInputStream ("E:/iodemo/ch01.txt") / / 2. Start reading information / / first define a byte array to store data byte [] b = new byte [5]; / / read all data into this byte / / read a file is.read (b) completely; / / read: return the file size read / / the maximum does not exceed b.length, and return the actual number of bytes read System.out.println (Arrays.toString (b)) / / read the byte array / / convert the byte array to the string System.out.println (new String (b)); / / close the stream is.close ();} catch (FileNotFoundException e) {/ / TODO Auto-generated catch block / / the problem forced by the system: the file did not find e.printStackTrace ();} catch (IOException e) {/ / file read / / TODO Auto-generated catch block e.printStackTrace ();}
Package com.jredu.oopch21;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Arrays;/** * the concept of a stream of data that flows to a sequence of data from an object and reaches that object. * input stream: the process of data source data flowing to computer memory * output stream: the process of moving data from a program to a target data source * @ author Administrator * / public class Ch03 {/ * read file contents * @ param args * / public static void main (String [] args) {/ / InputStream: is an abstract class / /\: is a transfer character / / representation Two representations of the disk path: 1. \\ 2, / try {/ / read the contents from the file address into the program / / 1, establish a connection InputStream is = new FileInputStream ("E:/iodemo/ch01.txt") / / 2. Start reading information / / first define a byte array to store data byte [] b = new byte [is.available ()]; / / read all data into this byte / / is.available (): return the size of the file / / while (is.available () = 0); / / stop cycling / / complete reading of a file when it is not equal to 0; int le = 2 While (is.read (bquotation offpence2)! =-1) {off+=1;} is.read (brecoveroffpai2); / / read: returns the file size read / / the maximum size does not exceed b.length, and the actual number of bytes read System.out.println (Arrays.toString (b)); / / reads the byte array / / converts the byte array to the string System.out.println (new String (b)) / / close stream is.close ();} catch (FileNotFoundException e) {/ / TODO Auto-generated catch block / / problems forced by the system: file not found e.printStackTrace ();} catch (IOException e) {/ / file read / write exception / / TODO Auto-generated catch block e.printStackTrace ();}
Package com.jredu.oopch21;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Arrays;public class Ch04 {/ * File for reading Chinese characters * @ param args * / public static void main (String [] args) {/ / TODO Auto-generated method stub try {/ * FileInputStream fis = new FileInputStream ("E:/iodemo/ch04.txt"); / / wrapper stream BufferedInputStream bis = new BufferedInputStream (fis) * / / wrapper stream BufferedInputStream bis = new BufferedInputStream (new FileInputStream ("E:/iodemo/ch04.txt")); / / read the contents of the file byte [] b = new byte [bis.available ()]; bis.read (b); / * char [] c = new char [b.length]; for (int I = 0; I < c. Stories; istreams +) {c [I] = (char) b [I];} System.out.println (Arrays.toString (c)) / / garbled * / System.out.println (Arrays.toString (b)); / / the byte / / String (byte []) converts the byte array into the string System.out.println (new String (b)); / / you can get the Chinese bis.close (); / / close the stream (just close the bis)} catch (FileNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace () } catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}}
Package com.jredu.oopch21;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class Ch05 {public static void main (String [] args) {/ / TODO Auto-generated method stub try {/ / read the file FileInputStream fis = new FileInputStream ("E:/iodemo/ch01.txt"); / / fis.available (): the length of the file byte [] b=new byte [fis.available ()]; / / skip: skip n bytes before reading fis.skip (5) / / Skip the first five fis.read (b); System.out.println (new String (b)); fis.close ();} catch (FileNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}
Package com.jredu.oopch21;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException The public class Ch06 {/ * read process is paused to mark the current. Next time read * @ param args * / public static void main (String [] args) {/ / TODO Auto-generated method stub / / pause / / give the current tag / / next time read try {BufferedInputStream bis= new BufferedInputStream (new FileInputStream ("E:/iodemo/ch06.txt")) from the mark location Byte [] b = new byte [bis.available ()]; / / bis.read (b, 0, b.length/2); / / set breakpoint bis.mark (bis.read (b, 0, b.length/2)); / / position is the length of the read System.out.println (new String (b)); System.out.println ("pause read...."); Thread.sleep (2000) / / dormant 2s / / continue to read System.out.println after hibernation ("continue reading..."); / / reset: set the current reset position to the position marked by the last call to mark bis.reset (); bis.read (b); System.out.println (new String (b)); bis.close ();} catch (FileNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace () } catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}
Package com.jredu.oopch21;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.SequenceInputStream;/** * sequence stream (collection stream) * read n streams together * @ author Administrator * * / public class Ch07 {public static void main (String [] args) {try {/ / the first file stream FileInputStream fis1=new FileInputStream ("E:/iodemo/ch01.txt") / / second file stream FileInputStream fis2=new FileInputStream ("E:/iodemo/ch04.txt"); / / merge SequenceInputStream sis=new SequenceInputStream (fis1, fis2) into the sequence stream; / / Mode 1: / / the array of temporary data storage / / int len = fis1.available () + fis2.available (); / / byte [] b=new byte [2*len+1]; / store the temporary data read each time as in sb / StringBuffer sb=new StringBuffer () / read all content at one time / / int off=0;// int item0 all at once / while ((i=sis.read (bgrad offpceLen)! =-1) {/ sb.append (); / / off+=i;//} / / System.out.println (new String (b)); / / Mode 2 byte [] b=new byte [fis1.available ()]; / / StringBuffer sb=new StringBuffer (); / / int item0 While (sis.read (b)! =-1) {System.out.println (new String (b)); / / sb.append (new String (b));} / / System.out.println (sb.toString ()); sis.close ();} catch (FileNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}
Package com.jredu.oopch21;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.SequenceInputStream;import java.util.Vector;public class Ch08 {public static void main (String [] args) {/ / TODO Auto-generated method stub try {/ / three file streams FileInputStream fis1 = new FileInputStream ("E:/iodemo/a.txt"); FileInputStream fis2 = new FileInputStream ("E:/iodemo/b.txt"); FileInputStream fis3 = new FileInputStream ("E:/iodemo/c.txt") / / add three streams to the collection Vector vector = new Vector (); vector.add (fis1); vector.add (fis2); vector.add (fis3); / / vector.elements (); / / method returns Enumeration / / merge into one sequence stream SequenceInputStream sis = new SequenceInputStream (vector.elements ()); byte [] b = new byte [fis1.available () + fis2.available () + fis3.available ()]; / / read int off=0 / / vector.get (I) .available (): the length of a file for (int I = 0; I < vector.size (); iTunes +) {/ / off: the location of the starting subscript where the data is stored in the array off+=sis.read (b, off, vector.get (I). Available ()); / / the length of one file read at a time} System.out.println (new String (b)); sis.close () } catch (FileNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}
Thank you for reading this article carefully. I hope the article "how to use the Java O stream to read the file content" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!