[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Coder994/JavaGuide/main/docs/java/io/io-basis.md [Back]  [Original]

---
title:  Java IO
category: Java
tag:
  - Java IO
  - Java
---

## IO 

IO  `Input/Output` IO IO  Java 

Java IO  40  4 

- `InputStream`/`Reader`: 
- `OutputStream`/`Writer`: 

## 

### InputStream

`InputStream``java.io.InputStream`

`InputStream`  

- `read()`  0  255  `-1` 
- `read(byte b[ ])` :  `b`  `b`  `-1` `b.length`   `read(b, 0, b.length)`
- `read(byte b[], int off, int len)` `read(byte b[ ])`  `off`  `len` 
- `skip(long n)`  n  ,
- `available()` 
- `close()` 

 Java 9 `InputStream` 

- `readAllBytes()` 
- `readNBytes(byte[] b, int off, int len)`  `len` 
- `transferTo(OutputStream out)`  

`FileInputStream` 

`FileInputStream` 

```java
try (InputStream fis = new FileInputStream("input.txt")) {
    System.out.println("Number of remaining bytes:"
            + fis.available());
    int content;
    long skip = fis.skip(2);
    System.out.println("The actual number of bytes skipped:" + skip);
    System.out.print("The content read from file:");
    while ((content = fis.read()) != -1) {
        System.out.print((char) content);
    }
} catch (IOException e) {
    e.printStackTrace();
}
```

`input.txt` 

![](https://guide-blog-images.oss-cn-shenzhen.aliyuncs.com/github/javaguide/java/image-20220419155214614.png)



```
Number of remaining bytes:11
The actual number of bytes skipped:2
The content read from file:JavaGuide
```

 `FileInputStream`  `BufferedInputStream`

 `readAllBytes()`  `String` 

```java
//  BufferedInputStream 
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("input.txt"));
//  String 
String result = new String(bufferedInputStream.readAllBytes());
System.out.println(result);
```

`DataInputStream`  `FileInputStream` 

```java
FileInputStream fileInputStream = new FileInputStream("input.txt");
//fileInputStream
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
//
dataInputStream.readBoolean();
dataInputStream.readInt();
dataInputStream.readUTF();
```

`ObjectInputStream`  Java `ObjectOutputStream` ()

```java
ObjectInputStream input = new ObjectInputStream(new FileInputStream("object.data"));
MyClass object = (MyClass) input.readObject();
input.close();
```

 `Serializable`  `transient` 

### OutputStream

`OutputStream``java.io.OutputStream`

`OutputStream`  

- `write(int b)` 
- `write(byte b[ ])` : `b`  `write(b, 0, b.length)` 
- `write(byte[] b, int off, int len)` : `write(byte b[ ])`  `off`  `len` 
- `flush()` 
- `close()` 

`FileOutputStream` 

`FileOutputStream` 

```java
try (FileOutputStream output = new FileOutputStream("output.txt")) {
    byte[] array = "JavaGuide".getBytes();
    output.write(array);
} catch (IOException e) {
    e.printStackTrace();
}
```



![](https://guide-blog-images.oss-cn-shenzhen.aliyuncs.com/github/javaguide/java/image-20220419155514392.png)

 `FileInputStream``FileOutputStream`  `BufferedOutputStream`

```java
FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)
```

**`DataOutputStream`**  `FileOutputStream`

```java
// 
FileOutputStream fileOutputStream = new FileOutputStream("out.txt");
DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
// 
dataOutputStream.writeBoolean(true);
dataOutputStream.writeByte(1);
```

`ObjectInputStream`  Java `ObjectInputStream`,`ObjectOutputStream`(`ObjectOutputStream`)

```java
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("file.txt")
Person person = new Person("Guide", "JavaGuide");
output.writeObject(person);
```

## 

 ** I/O **



-  Java 
- 

 `FileInputStream`  `input.txt` 

![](https://guide-blog-images.oss-cn-shenzhen.aliyuncs.com/github/javaguide/java/image-20220419154632551.png)



```java
Number of remaining bytes:9
The actual number of bytes skipped:2
The content read from file:
```



I/O 

 `Unicode` `utf8` : 1  3 `unicode` 2 `gbk` 1  2 

### Reader

`Reader``java.io.Reader`

`Reader`  `InputStream` 

`Reader`  

- `read()` : 
- `read(char[] cbuf)` :  `cbuf` `read(cbuf, 0, cbuf.length)` 
- `read(char[] cbuf, int off, int len)` `read(char[] cbuf)`  `off`  `len` 
- `skip(long n)`  n  ,
- `close()` : 

`InputStreamReader`  `FileReader` 

```java
// 
public class InputStreamReader extends Reader {
}
// 
public class FileReader extends InputStreamReader {
}
```

`FileReader` 

```java
try (FileReader fileReader = new FileReader("input.txt");) {
    int content;
    long skip = fileReader.skip(3);
    System.out.println("The actual number of bytes skipped:" + skip);
    System.out.print("The content read from file:");
    while ((content = fileReader.read()) != -1) {
        System.out.print((char) content);
    }
} catch (IOException e) {
    e.printStackTrace();
}
```

`input.txt` 

![](https://guide-blog-images.oss-cn-shenzhen.aliyuncs.com/github/javaguide/java/image-20220419154632551.png)



```
The actual number of bytes skipped:3
The content read from file:Guide
```

### Writer

`Writer``java.io.Writer`

`Writer`  

- `write(int c)` : 
- `write(char[] cbuf)`  `cbuf``write(cbuf, 0, cbuf.length)`
- `write(char[] cbuf, int off, int len)` `write(char[] cbuf)`  `off`  `len` 
- `write(String str)`  `write(str, 0, str.length())` 
- `write(String str, int off, int len)` `write(String str)`  `off`  `len` 
- `append(CharSequence csq)`  `Writer`  `Writer` 
- `append(char c)`  `Writer`  `Writer` 
- `flush()` 
- `close()`:

`OutputStreamWriter`  `FileWriter` 

```java
// 
public class OutputStreamWriter extends Writer {
}
// 
public class FileWriter extends OutputStreamWriter {
}
```

`FileWriter` 

```java
try (Writer output = new FileWriter("output.txt")) {
    output.write("Guide");
} catch (IOException e) {
    e.printStackTrace();
}
```



![](https://guide-blog-images.oss-cn-shenzhen.aliyuncs.com/github/javaguide/java/image-20220419155802288.png)

## 

IO / IO 

 `InputStream` `OutputStream`

 `BufferedInputStream` `FileInputStream` 

```java
//  BufferedInputStream 
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("input.txt"));
```

 `write(int b)`  `read()`  IO 

 `write(int b)`  `read()`  `524.9 mb`  PDF 

```
PDF:15428 
PDF:2555062 
```

 1/165

:

```java
@Test
void copy_pdf_to_another_pdf_buffer_stream() {
    // 
    long start = System.currentTimeMillis();
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(".pdf"));
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("-.pdf"))) {
        int content;
        while ((content = bis.read()) != -1) {
            bos.write(content);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 
    long end = System.currentTimeMillis();
    System.out.println("PDF:" + (end - start) + " ");
}

@Test
void copy_pdf_to_another_pdf_stream() {
    // 
    long start = System.currentTimeMillis();
    try (FileInputStream fis = new FileInputStream(".pdf");
         FileOutputStream fos = new FileOutputStream("-.pdf")) {
        int content;
        while ((content = fis.read()) != -1) {
            fos.write(content);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 
    long end = System.currentTimeMillis();
    System.out.println("PDF:" + (end - start) + " ");
}
```

 `read(byte b[])`  `write(byte b[], int off, int len)` 

 `read(byte b[])`  `write(byte b[], int off, int len)`  524.9 mb  PDF 

```
PDF:695 
PDF:989 
```





```java
@Test
void copy_pdf_to_another_pdf_with_byte_array_buffer_stream() {
    // 
    long start = System.currentTimeMillis();
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(".pdf"));
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("-.pdf"))) {
        int len;
        byte[] bytes = new byte[4 * 1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 
    long end = System.currentTimeMillis();
    System.out.println("PDF:" + (end - start) + " ");
}

@Test
void copy_pdf_to_another_pdf_with_byte_array_stream() {
    // 
    long start = System.currentTimeMillis();
    try (FileInputStream fis = new FileInputStream(".pdf");
         FileOutputStream fos = new FileOutputStream("-.pdf")) {
        int len;
        byte[] bytes = new byte[4 * 1024];
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 
    long end = System.currentTimeMillis();
    System.out.println("PDF:" + (end - start) + " ");
}
```

### BufferedInputStream

`BufferedInputStream`  IO 

`BufferedInputStream`  `BufferedInputStream` 

```java
public
class BufferedInputStream extends FilterInputStream {
    // 
    protected volatile byte buf[];
    // 
    private static int DEFAULT_BUFFER_SIZE = 8192;
    // 
    public BufferedInputStream(InputStream in) {
        this(in, DEFAULT_BUFFER_SIZE);
    }
    // 
    public BufferedInputStream(InputStream in, int size) {
        super(in);
        if (size 

Web Proxy Viewer  |  New URL  |  Original Page