Byte stream
FileOutStream
A constructor
FileOutputStream(File File) Creates a File output stream to write to a File represented by the specified File object.
FileOutputStream(FileDescriptor fdObj) creates a FileOutputStream to write to the specified FileDescriptor, which represents an existing connection to the actual file in the file system. FileOutputStream(File File, Boolean Append) Creates a File output stream to write to a File represented by the specified File object. FileOutputStream(String name) creates a FileOutputStream to write to a file with the specified name. FileOutputStream(String name, Boolean append) creates a FileOutputStream to write to a file with the specified name.
Commonly used method
Void close() closes this file output stream and frees all system resources associated with this stream.
FileChannel getChannel() returns the unique FileChannel object associated with the output stream of this file. FileDescriptor getFD() returns the FileDescriptor associated with the stream. Void write(byte[] b) Writes b.length bytes from the specified byte array to the file output stream. Void write(byte[] b, int off, int len) writes len bytes from the specified byte array starting at offset off to the file output stream. Void write(int b) writes the specified byte to the file output stream.
The specific implementation
package work.february.two.daily;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/ * * *@Author: a small sea *@Description:
* @Date Created in 2021-02-02 17:32
* @Modified By:
*/
public class Demo4 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream =new FileOutputStream("D://a.txt");
/ / int type
fileOutputStream.write(65);
/ / byte [] type
byte [] bytes ={65.66.67.68};
fileOutputStream.write(bytes);
fileOutputStream.write(bytes,1.2);
System.out.println("Already written");
fileOutputStream.close();
// Append when not the same object. True is append
FileOutputStream fileOutputStream1 =new FileOutputStream("D://a.txt".true);
fileOutputStream1.write(100); fileOutputStream1.close(); }}Copy the code
FileInputStream
The constructor
FileInputStream(File File) Creates a FileInputStream by opening a connection to an actual File named File, the File object in the File system. FileInputStream(FileDescriptor fdObj) FileInputStream is created using the FileDescriptor fdObj, which represents an existing connection to the actual file in the file system. FileInputStream(String Name) Creates a FileInputStream by opening a connection to the actual file named by the path name in the file system.
Commonly used method
Int available() returns an estimate of the number of bytes left that can be read (or skipped) from this input stream without being blocked by the next method that calls this input stream.
Void close() closes the file input stream and frees all system resources associated with the stream. FileChannel getChannel() returns the unique FileChannel object associated with this file’s input stream. FileDescriptor getFD() returns an FileDescriptor object that represents a connection to the actual file in the file system that this FileInputStream is using. Int read() reads one byte of data from the input stream. Int read(byte[] b) Reads a B. string byte array of the most b. string bytes of data from this input stream. Int read(byte[] b, int off, int len) Reads up to len bytes of data from the input stream into a byte array. Long skip(long n) Skips and dismisses n bytes of data from the input stream.
Implementation method:
package work.february.two.daily;
import java.io.FileInputStream;
import java.io.IOException;
/ * * *@Author: a small sea *@Description:
* @Date Created in 2021-02-02 18:25
* @Modified By:
*/
public class Demo5 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream =new FileInputStream("D:\\a.txt");
// Read byte by byte until the end is -1
while (true) {byte b = (byte) fileInputStream.read();
if(b == -1) {break;
}else {
System.out.println((char)b); } } fileInputStream.close(); }}Copy the code
A group reading is more reliable
package work.february.two.daily;
import java.io.FileInputStream;
import java.io.IOException;
/ * * *@Author: a small sea *@Description:
* @Date Created in 2021-02-02 19:27
* @Modified By:
*/
public class Demo6 {
public static void main(String[] args) throws IOException {
// A set of bytes to read
FileInputStream fileInputStream =new FileInputStream("D://a.txt");
byte [] bytes =new byte[5];
int len =fileInputStream.read(bytes);
System.out.println(new String(bytes,0,len));
len =fileInputStream.read(bytes);
System.out.println(new String(bytes,0,len)); fileInputStream.close(); }}Copy the code
Time to talk:
If the guest officer feel edible appropriate can give a free praise! Thanks, thanks! Go slow, guest officer! I suggest you pack it and come back next time. Shopkeeper QQ: 309021573, welcome harassment!