Java file and stream reading

In io programming of Java language, reading a file is divided into two steps: 1. Convert the data in the file into a stream; 2. Read the data inside the stream. The first step is completed by the system. You only need to create the corresponding flow object. After the object is created, step 1 is completed. The second step can be realized by using the read method in the input flow object.

When programming with input stream, the code is generally divided into three parts: 1. Create a stream object, 2. Read the data inside the stream object, and 3. Close the stream object. Here is a code example to read a file:

import java. io.*;

/**

*Reading files using FileInputStream

*/

public class ReadFile1 {

public static void main(String[] args) {

FileInputStream fis = null; // Declare stream object

try{

fis = new FileInputStream("e:\\a.txt"); // Create flow object

//Read the data and store the read data in the array

byte[] data = new byte[1024]; // Array of data stores

int i = 0;// Current subscript

//Read the first byte of data in the stream

int n = fis. read();

//Read the subsequent data in turn

While (n! = - 1) {/ / the end of the stream is not reached

//Store valid data in an array

data[i] = (byte)n;

//Subscript increase

i++;

//Read the next byte of data

n = fis. read();

}

//Parse data

String s = new String(data,i);

//Output string

System. out. println(s);

}catch(Exception e){

e.printStackTrace();

}finally{

try{

//Close the flow and free up resources

fis. close();

}catch(Exception e){}

}

}

}

The following example code is the code that uses another read method to read:

import java. io. FileInputStream;

/**

*Reading files using FileInputStream

*/

public class ReadFile2 {

public static void main(String[] args) {

//Declare stream object

FileInputStream fis = null;

try{

//Create flow object

fis = new FileInputStream("e:\\a.txt");

//Read the data and store the read data in the array

byte[] data = new byte[1024]; // Array of data stores

int i = fis. read(data);

//Parse data

String s = new String(data,i);

//Output string

System. out. println(s);

}catch(Exception e){

e.printStackTrace();

}finally{

try{

//Close the flow and free up resources

fis. close();

}catch(Exception e){}

}

}

}

The steps for programming using the output stream are:

1. Create output stream

Establish the corresponding output stream object, that is, complete the conversion from the stream object to the external data source.

2. Write data to stream

Call the corresponding write method to write the data to be output into the stream object.

3. Close output stream

After writing, the close method that calls the stream object closes the output stream and frees the resource.

The following takes fileoutputstream as an example to illustrate the use of output stream. The example code is as follows:

import java. io.*;

/**

*Example of writing a file using fileoutputstream

*/

public class WriteFile1 {

public static void main(String[] args) {

String s = "Java language";

int n = 100;

//Declare stream object

FileOutputStream fos = null;

try{

//Create flow object

fos = new FileOutputStream("e:\\out.txt");

//Convert to byte array

byte[] b1 = s.getBytes();

//Newline character

byte[] b2 = "\r\n". getBytes();

byte[] b3 = String. valueOf(n). getBytes();

//Write files in sequence

fos. write(b1);

fos. write(b2);

fos. write(b3);

} catch (Exception e) {

e.printStackTrace();

}finally{

try{

fos. close();

}catch(Exception e){}

}

}

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>