⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileinputstreamdemo.java

📁 java2参考大全上的例子的源码和自己的理解.
💻 JAVA
字号:
package fileinputstream;

/**
 Total Available Bytes: 100
 First 2 bytes of the file one read() at a time
 he
 Still Available: 98
 Reading the next 2 with one read(b[])
 kk
 Still Available: 96
 Skipping half of remaining bytes with skip()
 Still Available: 48
 Reading 1 into the end of array
 ka
 Still Available: 47
 */

// Demonstrate FileInputStream.
import java.io.*;

class FileInputStreamDemo {
  public static void main(String args[]) throws Exception {
    int size;
    InputStream f =
        new FileInputStream("FileInputStreamDemo.java");

    System.out.println("Total Available Bytes: " +
                       (size = f.available()));
    int n = size / 40;
    System.out.println("First " + n +
                       " bytes of the file one read() at a time");
    for (int i = 0; i < n; i++) {
      System.out.print( (char) f.read());
    }
    System.out.println("\nStill Available: " + f.available());
    System.out.println("Reading the next " + n +
                       " with one read(b[])");
    byte b[] = new byte[n];
    if (f.read(b) != n) {
      System.err.println("couldn't read " + n + " bytes.");
    }

    System.out.println(new String(b, 0, n));
    System.out.println("\nStill Available: " + (size = f.available()));
    System.out.println("Skipping half of remaining bytes with skip()");
    f.skip(size / 2);
    System.out.println("Still Available: " + f.available());
    System.out.println("Reading " + n / 2 + " into the end of array");
    if (f.read(b, n / 2, n / 2) != n / 2) {
      System.err.println("couldn't read " + n / 2 + " bytes.");
    }
    System.out.println(new String(b, 0, b.length));
    System.out.println("\nStill Available: " + f.available());
    f.close();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -