In the Java language IO programming, reading a file is two steps: 1, the data in the file is converted to a stream, 2, the data inside the stream is read. The first step is completed by the system, only need to create the corresponding stream object, the object is created after the completion of Step 1, the second step is implemented using the Read method in the input stream object.
When programming with an input stream, the code is generally divided into 3 parts: 1, creating a Stream object, 2, reading the data inside the stream object, 3, closing the stream object. The following code example reads a file:
Import java.io.*;
/**
* Read files using FileInputStream
*/
public class ReadFile1 {
public static void Main (string[] args) {
FileInputStream FIS = null; Declaring a Stream object
try{
FIS = new FileInputStream ("E:\\a.txt"); Creating a Stream object
Reads the data and stores the read data in an array
byte[] data = new byte[1024]; Array of data stores
int i = 0; Current subscript
Reads the first byte of data in a stream
int n = fis.read ();
Read the subsequent data sequentially
while (n! =-1) {//does not reach the end of the stream
To store valid data in an array
Data[i] = (byte) n;
Subscript Increase
i++;
Read the next byte of data
n = fis.read ();
}
Parsing data
string s = new string (data,0,i);
Output string
System.out.println (s);
}catch (Exception e) {
E.printstacktrace ();
}finally{
try{
Close the stream and release the resource
Fis.close ();
}catch (Exception e) {}
}
}
}
The following example code is code that reads with another read method:
Import Java.io.FileInputStream;
/**
* Read files using FileInputStream
*/
public class ReadFile2 {
public static void Main (string[] args) {
Declaring a Stream object
FileInputStream FIS = null;
try{
Creating a Stream object
FIS = new FileInputStream ("E:\\a.txt");
Reads the data and stores the read data in an array
byte[] data = new byte[1024]; Array of data stores
int i = fis.read (data);
Parsing data
string s = new string (data,0,i);
Output string
System.out.println (s);
}catch (Exception e) {
E.printstacktrace ();
}finally{
try{
Close the stream and release the resource
Fis.close ();
}catch (Exception e) {}
}
}
}
The steps for programming with the output stream are:
1. Set up the output stream
The corresponding output stream object is established, that is, the conversion between the stream object and the external data source is completed.
2. Write data to the stream
The data that needs to be output is called, and the corresponding write method is written to the stream object.
3. Turn off the output stream
After the write is completed, the Close method of the call flow object closes the output stream and frees the resource.
The use of the output stream is illustrated below with FileOutputStream as an example. The sample code is as follows:
Import java.io.*;
/**
* Example of using FileOutputStream to write files
*/
public class WriteFile1 {
public static void Main (string[] args) {
String s = "Java language";
int n = 100;
Declaring a Stream object
FileOutputStream fos = null;
try{
Creating a Stream object
FOS = new FileOutputStream ("E:\\out.txt");
Convert to byte array
Byte[] B1 = S.getbytes ();
Line break
byte[] B2 = "\ r \ n". GetBytes ();
byte[] B3 = string.valueof (n). GetBytes ();
Write to file in sequence
Fos.write (B1);
Fos.write (B2);
Fos.write (B3);
} catch (Exception e) {
E.printstacktrace ();
}finally{
try{
Fos.close ();
}catch (Exception e) {}
}
}
}
Java file and stream read