SoFunction
Updated on 2025-03-02

Java byte stream, character stream and conversion stream process

IO Stream Overview

IO stream:Input/Output

  • A stream is a set of sequential, start and end points bytes, which is a general term or abstraction of data transmission. That is, the transmission of data between two devices is called streaming
  • The essence of a stream is data transmission. According to the data transmission characteristics, the stream is abstracted into various categories, which is convenient and more intuitive to perform data operations.

Classification of IO streams

  • According to the processing data type, it is divided into:Character streams and byte streams
  • According to the data flow direction, it is divided into:Input Stream(Read the file) andOutput Stream(Write a file)

Byte Stream

Byte output stream

OutputStream class definition

public abstract class OutputStream extends Object implements Closeable,Flushable

This abstract class is a superclass representing all classes that output a stream of bytes. The output stream accepts the output bytes and sends these bytes to the InputSream class. A receiver wants to output to the file. Use the FileOutputStream class.

Byte input stream

InputSream class definition

public abstract class InputStream extends Object implements Closeable

This abstract class is the superclass of all classes of the byte input stream.

FileInputStream gets input bytes from a file in the file system

Example code:

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class ByteStreamDemo {
	//Read the file	public static void In() {
		//Determine the target file to be written		File file = new File("D:\\test\\");
		//Build a file input stream object		try {
			InputStream in = new FileInputStream(file);
			
			//Read the file content into the program			byte[] bytes = new byte[1024];
			StringBuilder buf = new StringBuilder();
			int len = -1;//Indicate the byte length of each read			//Read the data into the array and return the number of bytes read. When it is not equal to -1, it means that the data has been read. Equally -1 means that the file has been read.			while((len=(bytes))!=-1) {
				//Convert to string content based on the read byte array and add to StringBuilder				(new String(bytes));
			}	
			//Test Print			("File read successfully:"+buf);
			//Close the flow			();
			
		} catch (FileNotFoundException e) {
			();
		} catch (IOException e) {
			();
		}
	}
	//Write a file	public static void Out() {
		//Determine the target file to be output		File file = new File("D:\\test\\");
		
		//Build a file output stream object		try {
			OutputStream out = new FileOutputStream(file,true);//true is not written as a replacement			//The output content			String info = "Small bridge and flowing water house\r\t";
			//Write the content into the file			(());
			//Close the flow			();
			("File writing was successful!");
		} catch (FileNotFoundException e) {
			();
		} catch (IOException e) {
			();
		}
	}
	public static void main(String[] args) {
		Out();
		In();		
	}
}
/**
 Running results:
 File writing was successful!
 File reading successfully: Xiaoqiao Liushui Home
	 Small bridge and flowing water house
 */

principle:

  • Input/output byte stream operation principle, only one byte will be operated at a time (read or written from a file)
  • Byte operation stream: By default, data will be written directly to the file every time you perform a write operation.

Character stream

Writer

  • An abstract class that writes to a character stream. The only methods that subclasses must implement are write(char[],int,int), flush() and close(). However, most subclasses will override some of the methods defined here to provide greater efficiency and/or other functionality.
  • Like OutputString, the operation of files is done using the FileWrite class.

Reader

  • An abstract class for reading character streams. The only methods that subclasses must implement are read(char[], int, int) and close(). However, most subclasses will override some of the methods defined here to provide greater efficiency and/or other functionality.
  • Use the FileReader class to perform instantiation operations.

The file character stream will come with its own cache, the default size is 1024 bytes. After the cache is full, the cache will be refreshed manually, or the stream will be closed and the data will be written to the file.

How to choose to use a byte stream or a character stream:

  • Generally, when operating non-text files, use byte streams, and use character streams for text files.
  • Internal implementation of character stream or byte stream

Example code:

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class CharStreamtDemo {

	public static void main(String[] args) {
		Out();
		In();
	}
	private static void In() {
		File file = new File("D:\\test\\");
		try {
			Reader in = new FileReader(file);
			
			char[] cs = new char[1];
			StringBuilder sbl = new StringBuilder();
			int len = -1;
			while((len=(cs))!=-1) {
				(cs, 0, );
			}
			(sbl);
			();
		} catch (FileNotFoundException e) {
			();
		} catch (IOException e) {
			();
		}
	}
	private static void Out() {
		File file = new File("D:\\test\\");
		try {
			Writer out = new FileWriter(file,true);
			(",The ancient road, the west wind is thin horse");
			();
		} catch (IOException e) {
			();
		}
	}
}
/**
 Running results:
  Small bridge and flowing water, ancient road and west wind to thin horse
 */

Code example:

Specify a file under the drive letter and copy the file to the specified directory

Copy of files: read data from an input stream and then write to the target location via the output stream

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class CopyFileDemo {
	public static void main(String[] args) {
		copy("D:\\test\\","D:\\test\\my\\");
	}
	private static void copy(String src,String target) {
		File srcFile = new File(src);
		File targetFile = new File(target);
		InputStream in =null;
		OutputStream out =null;
		try {
			in =new FileInputStream(srcFile);
			out =new FileOutputStream(targetFile);
			
			byte[] bytes = new byte[1024];
			int len = -1;
			while((len=(bytes))!=-1) {
				(bytes, 0, len);
			}			
		} catch (FileNotFoundException e) {
			();
		} catch (IOException e) {
			();
		}
		finally {
			try {
				if(in!=null)();
				if(out!=null)();
			} catch (IOException e) {
				();
			}
		}
	}
}

Byte character conversion stream

Convert streams, which can convert a byte stream to a character stream, or convert a character stream to a byte stream

  • OutputStreamWriter:The output character stream can be converted into the output form of a byte stream
  • InputStreamReader:Convert the input byte stream into a character stream input form

Example code:

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class ChangerStringDemo {

	public static void main(String[] args) throws FileNotFoundException {
		//InputStream in = new FileInputStream("D:\\test\\");
		//read(in);
		OutputStream out = new FileOutputStream("D:\\test\\");
		writer(out);
	}
	private static void read(InputStream in) {
		Reader reader = new InputStreamReader(in,());
		
		char[] cs = new char[1024];
		int len = -1;
		try {
			while((len=(cs))!=-1) {
				(new String(cs,0,len));
			}
			();
		} catch (IOException e) {
			();
		}
	}
	private static void writer(OutputStream out) {
		Writer writ = new OutputStreamWriter(out,());
		try {
			("The sunset is setting, and the heartbroken person is at the end of the world");
			();
		} catch (IOException e) {
			();
		}
	}
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.