Byte output, the input stream OutputStream, InputStream

@

Byte output stream OutputStream

void close ()
Closes this output stream and releases any system resources associated with this stream.

void flush ()
Flushes this output stream and forces any buffered output bytes to write effective for the character stream

void write (byte [] b)
The b.length bytes written to this output stream from the specified byte array.

void write (byte [] b, int off, int len)
specified byte array starting at offset off len bytes written to this output stream.

abstract void write (int b) Write a byte

    OutputStream os = new FileOutputStream("hello.txt");
    os.write("good".getBytes());
    byte[] bys = {97,98,99,100};
    os.write(bys, 1, 2);
    os.write(100);
    os.flush();
    os.close();

// 输出结果 hellowordbcd

FileOutputStream (File file)
to create a file for writing data file represented by the specified File object in the output stream.

FileOutputStream (File file, boolean append)
to create a file for writing data file represented by the specified File object in the output stream.

FileOutputStream (String name)
Creates a write data to a file with the specified name of the output file stream.

FileOutputStream (String name, boolean append)
creates a write data to the file with the specified name in the output file stream.

Binding IO streams written exception handling

    System.out.println("Start");
    try (OutputStream os = new FileOutputStream("Test.txt");
        Scanner input = new Scanner(System.in);
            ){
        os.write("HelloWorld".getBytes());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("End");

InputStream input stream of bytes

Method Summary

int available ()
estimates a stream of bytes back to this input method calls input stream without blocking read (or skipped) a.
void close ()
Closes this input stream and releases any system resources associated with the stream.
void mark (int readlimit)
this input stream Marks the current position.
boolean markSupported ()
Tests if this input stream supports the mark and reset methods.
abstract int read ()
Reads the next byte of data from the input stream.

  **特点:** 
      1.表示一次性读取一个字节
      2.每读取一个字节,那么返回的实际读取的内容
      3.读取一个字节完毕,会自动等待读取下一个字节,类似迭代器的 cursor
      4.读取到文件末尾返回 -1

int read (byte [] b)
reads a certain number of bytes from the input stream, and stores it in the buffer array b.
int read (byte [] b, int off, int len)
the input stream up to len bytes of data is read into the byte array.
void reset ()
this stream relocated to the last position of this input stream mark method was called.
long skip (long n)
skipped and discards the input data flow in n bytes.

File -> FileInputStream input stream memory
Memory -> file output stream PrintStream

Note:
1. If the file does not exist the output stream is created automatically, but the input stream file must exist, otherwise it will throw FileNotFoundException
2. read one byte, if the intermediate think each byte did cast It may result in garbled

int read(byte[] b)

A disposable read the contents of a byte array from the outside

2. Each read once, returns the length actually read

3. Read the file to the end of -1

4. Each time the read pointer is moved backward a byte array unit

note:

While expanding the range of each reading, but distortion still exists, so the byte stream read a byte or byte array, but GBK or UTF-8 encoding table representing a Chinese two or three bytes, certainly possible garbled, so it is not recommended byte stream processing Chinese.

    InputStream is = new FileInputStream("d:\\test.txt");
    byte[] bys = new byte[1024];
    int len = 0;
    while ((len = is.read(bys)) != -1) {
        System.out.print(new String(new String(bys, 0, len)));
    }

+ Byte input stream of bytes to the output stream, copied file

test.java --> test.txt

Data source: test.java

Destination: test.txt

Direction:

File -> Memory FileInputStream

Memory -> File FileOutputStream

Copy mode:

A disposable copy a byte

2. The disposable copy a byte array

A disposable copy a byte

/*
 * 功能: 一次性拷贝一个字节拷贝文件
 * 返回值类型 : void
 * 参数列表: String srcFileName, String descFileName
 * 方法名: copy
 */
public static void copy(String srcFileName, String descFileName) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(srcFileName);
        fos = new FileOutputStream(descFileName);

        int by = 0;
        while ((by = fis.read()) != -1) {
            fos.write(by);
            fos.flush();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

2. The disposable copy a byte array

/*
 * 功能: 一次性拷贝一个字节数组拷贝文件
 * 返回值类型 : void
 * 参数列表: File srcFile, File descFile
 * 方法名: copy
 */
public static void copy(File srcFile, File descFile, int capacity) {
    try (FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(descFile);) {
        byte[] bys = new byte[capacity];
        int len = 0;

        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
            fos.flush();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } 
} 

Guess you like

Origin www.cnblogs.com/zhiwenxi/p/11389291.html