package net.csdn.waterdrop.io;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author 水滴
* @date 2022/7/9 0009
*/
public class IO_02_FileInputStream {
public static void main(String[] args) throws IOException {
// read1();
// read2();
// read3();
// read4();
// read5();
read6();
}
/**
* 一次读取一个字节
*
* @throws IOException
*/
public static void read1() throws IOException {
// 通过文件路径创建 FileInputStream 实例
FileInputStream fis = new FileInputStream("c:/io/01.txt");
// 可读取的字节数
System.out.println("可读取的字节数: " + fis.available());
// 读取一个字节
int read = fis.read();
System.out.println("可读取的字节数: " + fis.available() + "\t读取内容: " + read);
// 读取一个字节
read = fis.read();
System.out.println("可读取的字节数: " + fis.available() + "\t读取内容: " + read);
// 读取一个字节
read = fis.read();
System.out.println("可读取的字节数: " + fis.available() + "\t读取内容: " + read);
// 读取一个字节
read = fis.read();
System.out.println("可读取的字节数: " + fis.available() + "\t读取内容: " + read);
// 关闭输入流
fis.close();
}
/**
* 通过循环判断可读取字节数,一次读取一个字节
*
* @throws IOException
*/
public static void read2() throws IOException {
// 通过文件路径创建 FileInputStream 实例
FileInputStream fis = new FileInputStream("c:/io/01.txt");
// 循环判断可读取字节数
while (fis.available() > 0) {
// 读取一个字节
int read = fis.read();
System.out.println("可读取的字节数: " + fis.available() + "\t读取内容: " + read);
}
// 关闭输入流
fis.close();
}
/**
* 通过循环判断是否读到末尾,一次读取一个字节
*
* @throws IOException
*/
public static void read3() throws IOException {
// 通过文件路径创建 FileInputStream 实例
FileInputStream fis = new FileInputStream("c:/io/01.txt");
int read;
// 循环读取一个字节,每次判断是否到达末尾
while ((read = fis.read()) != -1) {
System.out.println("可读取的字节数: " + fis.available() + "\t读取内容: " + (char) read);
}
// 关闭输入流
fis.close();
}
/**
* 一次读取多个字节
*
* @throws IOException
*/
public static void read4() throws IOException {
// 通过文件路径创建 FileInputStream 实例
FileInputStream fis = new FileInputStream("c:/io/01.txt");
// 可读取的字节数
System.out.println("可读取的字节数: " + fis.available());
// 字节数组,缓存读取的内容
byte[] bytes = new byte[2];
// 一次读取多个字节
int len = fis.read(bytes);
System.out.println("可读取的字节数: " + fis.available() + "\t读取的字节数: " + len + "\t读取内容: " + new String(bytes));
// 一次读取多个字节
len = fis.read(bytes);
System.out.println("可读取的字节数: " + fis.available() + "\t读取的字节数: " + len + "\t读取内容: " + new String(bytes));
// 一次读取多个字节
len = fis.read(bytes);
System.out.println("可读取的字节数: " + fis.available() + "\t读取的字节数: " + len + "\t读取内容: " + new String(bytes));
// 关闭输入流
fis.close();
}
/**
* 通过循环判断可读取字节数,一次读取多个字节
*
* @throws IOException
*/
public static void read5() throws IOException {
// 通过文件路径创建 FileInputStream 实例
FileInputStream fis = new FileInputStream("c:/io/01.txt");
// 字节数组,缓存读取的内容
byte[] bytes = new byte[2];
while (fis.available() > 0) {
// 一次读取多个字节
int len = fis.read(bytes);
System.out.println("可读取的字节数: " + fis.available() + "\t读取的字节数: " + len + "\t读取内容: " + new String(bytes, 0, len));
}
// 关闭输入流
fis.close();
}
/**
* 通过循环判断是否读到末尾,一次读取多个字节
*
* @throws IOException
*/
public static void read6() throws IOException {
// 通过文件路径创建 FileInputStream 实例
FileInputStream fis = new FileInputStream("c:/io/01.txt");
// 字节数组,缓存读取的内容
byte[] bytes = new byte[2];
int len;
// 循环读取一个字节,每次判断是否到达末尾
while ((len = fis.read(bytes)) != -1) {
System.out.println("可读取的字节数: " + fis.available() + "\t读取的字节数: " + len + "\t读取内容: " + new String(bytes, 0, len));
}
// 关闭输入流
fis.close();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
io.zip (9个子文件)
IO_01_File.java 4KB
IO_07_BufferedOutputStream.java 979B
IO_08_BufferedReader.java 1KB
IO_02_FileInputStream.java 5KB
IO_03_FileOutputStream.java 2KB
IO_05_FileWriter.java 4KB
IO_04_FileReader.java 3KB
IO_06_BufferedInputStream.java 1KB
IO_09_BufferedWriter.java 927B
共 9 条
- 1
水滴技术
- 粉丝: 1w+
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1