Io Stream and Propertiesio stream
An IO stream is an interface between a computer and the outside world or between a program and the rest of the computer. It is critical for any computer system,
Thus the body of all I/O is actually built into the operating system. Separate programs generally allow the system to do most of the work for them.
In Java programming, I/O is always done using streams. All I/O are treated as a single byte of movement,
Moves one byte at a time through an object called Stream. Flow I/O is used to contact the outside world. It is also used internally,
Used to convert an object to a byte and then back to the object. Input is also called read data, output is also called contrived to write data.
Overview
- The IO stream is the transmission of data between devices.
- IO stream input and output (based on the program's angle, not the file-based angle)
Classification of IO Streams
Data flow:
- Input stream: File to Program
- Output stream: Program to File
Data types (data stores within a computer are stored in bytes):
- BYTE stream: All files can be manipulated using a byte stream
- Character stream: When a text file is read, we need to convert the bytes to characters.
How many bytes represent a character? Not necessarily.
Base class
Data flow:
- Input stream:
- Output stream:
Data type:
- BYTE stream:
- byte input stream (InputStream): Outputs data in memory to a specified device
- Byte output stream (OutputStream): Output data on the device to the device
- Character Stream:
- Character output stream
- Character input stream
FileOutputStream
The construction method (if the specified file does not exist, the object is created; exists, and the data in the file is emptied (unless you use the latter two construction methods, the character stream is the same)):
- Public FileOutputStream: Creates a file output stream to write files represented by the specified file object.
- Public FileOutputStream (String name): Creates a file output stream to write to the file with the specified name (name indicates the path to the action file).
- FileOutputStream (File File, Boolean append): Creates a file output stream to write the file represented by the specified file object, and the Boolean type indicates whether the content is appended.
- FileOutputStream (String name, Boolean append): Creates a file output stream that writes to a file with the specified name, and a Boolean type that indicates whether the content is appended.
Common methods:
- void write (int b): Writes the specified bytes out to the file (a).
- void Write (byte[] b): Writes the bytes in the byte array to the file.
- void Write (byte[] b, int off, int len): Off indicates the index position of the start of the byte array, the output Len bytes length to the file
- void Close (): Closes this file output stream and frees any system resources associated with this stream.
Steps to use:
- Create a Stream
- Write stream
Close the stream
public static void main(String[] args) throws Exception { // 使用构造方法创建FileOutputStream对象 FileOutputStream fos = new FileOutputStream("F:\\bbb\\wangzhi.txt"); // FileOutputStream fos2 = new FileOutputStream(new File("F:\\bbb\\wangzhi.txt")); // 调用write方法向文件中输出内容 // int字节,查询ASICC表 fos.write(97); fos.write(65); fos.write(98); fos.write(66); fos.write(99); fos.write(new byte[]{67,100,68}); fos.close(); }
FileInputStream
Construction method (file must exist, no throw exception exists):
- FileInputStream (File file): Creates a fileinputstream by opening a connection to the actual file, which is named by the file object files in the filesystem.
- FileInputStream (String name): Creates a fileinputstream by opening a connection to the actual file, which is named by the pathname name in the file system.
Common methods:
- int read (): reads a single byte of data from the input stream.
- int read (byte[] b): reads up to b.length bytes of data from the input stream into a byte array (returns the number of bytes read).
void Close (): Closes this file input stream and frees any system resources associated with the stream.
// 使用字节流复制图片 public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("D:\\美女.jpg"); FileOutputStream fos = new FileOutputStream("F:\\bbb\\美女.jpg"); /*long start = System.currentTimeMillis(); byte[] arr = new byte[1024 * 4]; int len; while((len = fis.read(arr)) != -1){ fos.write(arr,0,len); } long end = System.currentTimeMillis(); System.out.println(end - start);*/ byte[] arr1 = new byte[1024 * 4]; int len1; StringBuilder sb = new StringBuilder(); while((len1 = fis.read(arr1)) != -1){ sb.append(new String(arr1,0,len1)); } fos.write(sb.toString().getBytes()); /*System.out.println(System.currentTimeMillis() - end);*/ fos.close(); fis.close(); }
Character Stream
Using byte stream to read Chinese (a Chinese occupies 3 bytes) may be garbled.
- Writer: stream of writing data
- FileWriter
- Reader: stream of Read data
- FileReader
FileReader
Construction Method:
- FileReader (String fileName)
Creates a new FileReader, given the name of the file to be read.
- FileReader (File file)
Creates a new FileReader that gives the file read.
Common methods:
- int read ()
Read a character
- int read (char[] cbuf)
Reads a character into an array.
- void Close ()
Closes the stream and frees any system resources associated with it.
FileWriter
Construction Method:
- FileWriter (File file)
Constructs a FileWriter object for a file object.
- FileWriter (File File, Boolean append)
Constructs a FileWriter object for a file object.
- FileWriter (String fileName)
Constructs a FileWriter object for a given file name.
- FileWriter (String fileName, Boolean append)
Constructs a FileWriter object that gives a file name with a Boolean value indicating whether to append the written data.
Common methods:
- void Write (char[] cbuf)
Writes an array of characters.
- void write (int c)
Write a character
- void write (String str)
Write a string
- void Write (String str, int off, int len)
Write a part of a string.
- void Flush ()
Refreshes the stream.
void Close ()
Close the stream and refresh it first.
// 复制txt文件 public static void main(String[] args) throws IOException { // 创建字符输入流 FileReader fr = new FileReader("wangzhi.txt"); // 创建字符输出流 FileWriter fw = new FileWriter("wangzhi2.txt"); // 读取资源 char[] arr = new char[1024]; int len; StringBuilder sb = new StringBuilder(); while((len = fr.read(arr)) != -1){ sb.append(arr); } fw.write(sb.toString()); // 关闭流 fw.close(); fr.close(); }
Java IO Stream (byte stream, character stream)