| 方法名及返回值类型 | 说明 |
|---|---|
| int read() |
从输入流中读取一个 8 位的字节,并把它转换为 0~255 的整数,最后返回整数。 如果返回 -1,则表示已经到了输入流的末尾。为了提高 I/O 操作的效率,建议尽量 使用 read() 方法的另外两种形式 |
| int read(byte[] b) |
从输入流中读取若干字节,并把它们保存到参数 b 指定的字节数组中。 该方法返回 读取的字节数。如果返回 -1,则表示已经到了输入流的末尾 |
| int read(byte[] b, int off, int len) |
从输入流中读取若干字节,并把它们保存到参数 b 指定的字节数组中。其中,off 指 定在字节数组中开始保存数据的起始下标;len 指定读取的字节数。该方法返回实际 读取的字节数。如果返回 -1,则表示已经到了输入流的末尾 |
| void close() |
关闭输入流。在读操作完成后,应该关闭输入流,系统将会释放与这个输入流相关 的资源。注意,InputStream 类本身的 close() 方法不执行任何操作,但是它的许多 子类重写了 close() 方法 |
| int available() | 返回可以从输入流中读取的字节数 |
| long skip(long n) | 从输入流中跳过参数 n 指定数目的字节。该方法返回跳过的字节数 |
| void mark(int readLimit) |
在输入流的当前位置开始设置标记,参数 readLimit 则指定了最多被设置标记的字 节数 |
| boolean markSupported() | 判断当前输入流是否允许设置标记,是则返回 true,否则返回 false |
| void reset() | 将输入流的指针返回到设置标记的起始处 |
| 方法名及返回值类型 | 说明 |
|---|---|
| void write(int b) |
向输出流写入一个字节。这里的参数是 int 类型,但是它允许使用表达式, 而不用强制转换成 byte 类型。为了提高 I/O 操作的效率,建议尽量使用 write() 方法的另外两种形式 |
| void write(byte[] b) | 把参数 b 指定的字节数组中的所有字节写到输出流中 |
| void write(byte[] b,int off,int len) |
把参数 b 指定的字节数组中的若干字节写到输出流中。其中,off 指定字节 数组中的起始下标,len 表示元素个数 |
| void close() |
关闭输出流。写操作完成后,应该关闭输出流。系统将会释放与这个输出 流相关的资源。注意,OutputStream 类本身的 close() 方法不执行任何操 作,但是它的许多子类重写了 close() 方法 |
| void flush() |
为了提高效率,在向输出流中写入数据时,数据一般会先保存到内存缓冲 区中,只有当缓冲区中的数据达到一定程度时,缓冲区中的数据才会被写 入输出流中。使用 flush() 方法则可以强制将缓冲区中的数据写入输出流, 并清空缓冲区 |
public class test08 {
public static void main(String[] args) {
byte[] b = new byte[] { 1, -1, 25, -22, -5, 23 }; // 创建数组
ByteArrayInputStream bais = new ByteArrayInputStream(b, 0, 6); // 创建字节数组输入流
int i = bais.read(); // 从输入流中读取下一个字节,并转换成int型数据
while (i != -1) { // 如果不返回-1,则表示没有到输入流的末尾
System.out.println("原值=" + (byte) i + "\t\t\t转换为int类型=" + i);
i = bais.read(); // 读取下一个
}
}
}
在该示例中,字节输入流 bais 从字节数组 b 的第一个元素开始读取 4 字节元素,并将这 4 字节转换为 int 类型数据,最后返回。原值=1 转换为int类型=1 原值=-1 转换为int类型=255 原值=25 转换为int类型=25 原值=-22 转换为int类型=234 原值=-5 转换为int类型=251 原值=23 转换为int类型=23从上述的运行结果可以看出,字节类型的数据 -1 和 -22 转换成 int 类型的数据后变成了 255 和 234,对这种结果的解释如下:
public class Test09 {
public static void main(String[] args) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[] { 1, -1, 25, -22, -5, 23 }; // 创建数组
baos.write(b, 0, 6); // 将字节数组b中的前4个字节元素写到输出流中
System.out.println("数组中一共包含:" + baos.size() + "字节"); // 输出缓冲区中的字节数
byte[] newByteArray = baos.toByteArray(); // 将输出流中的当前内容转换成字节数组
System.out.println(Arrays.toString(newByteArray)); // 输出数组中的内容
}
}
该程序的输出结果如下:
数组中一共包含:6字节 [1, -1, 25, -22, -5, 23]
try {
// 以File对象作为参数创建FileInputStream对象
FileInputStream fis1 = new FileInputStream(new File("F:/mxl.txt"));
// 以字符串值作为参数创建FilelnputStream对象
FileInputStream fis2 = new FileInputStream("F:/mxl.txt");
} catch(FileNotFoundException e) {
System.out.println("指定的文件找不到!");
}
D:\myJava\HelloJava.java 文件,下面使用 FileInputStream 类读取并输出该文件的内容。具体代码如下:
public class Test10 {
public static void main(String[] args) {
File f = new File("D:/myJava/HelloJava.java");
FileInputStream fis = null;
try {
// 因为File没有读写的能力,所以需要有个InputStream
fis = new FileInputStream(f);
// 定义一个字节数组
byte[] bytes = new byte[1024];
int n = 0; // 得到实际读取到的字节数
System.out.println("D:\\myJava\\HelloJava.java文件内容如下:");
// 循环读取
while ((n = fis.read(bytes)) != -1) {
String s = new String(bytes, 0, n); // 将数组中从下标0到n的内容给s
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如上述代码,在 FileInputDemo 类的 main() 方法中首先创建了一个 File 对象 f,该对象指向 D:\myJava\HelloJava.java 文件。接着使用 FileInputStream 类的构造方法创建了一个 FileInputStream 对象 fis,并声明一个长度为 1024 的 byte 类型的数组,然后使用 FileInputStream 类中的 read() 方法将 HelloJava.java 文件中的数据读取到字节数组 bytes 中,并输出该数据。最后在 finally 语句中关闭 FileInputStream 输入流。
D:\myJava\HelloJava.java文件内容如下:
/*
*第一个java程序
*/
public class HelloJava {
// 这里是程序入口
public static void main(String[] args) {
// 输出字符串
System.out.println("你好 Java");
}
}
public class Test11 {
public static void main(String[] args) {
FileInputStream fis = null; // 声明FileInputStream对象fis
FileOutputStream fos = null; // 声明FileOutputStream对象fos
try {
File srcFile = new File("D:/myJava/HelloJava.java");
fis = new FileInputStream(srcFile); // 实例化FileInputStream对象
File targetFile = new File("D:/myJava/HelloJava.txt"); // 创建目标文件对象,该文件不存在
fos = new FileOutputStream(targetFile); // 实例化FileOutputStream对象
byte[] bytes = new byte[1024]; // 每次读取1024字节
int i = fis.read(bytes);
while (i != -1) {
fos.write(bytes, 0, i); // 向D:\HelloJava.txt文件中写入内容
i = fis.read(bytes);
}
System.out.println("写入结束!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close(); // 关闭FileInputStream对象
fos.close(); // 关闭FileOutputStream对象
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如上述代码,将 D:\myJava\HelloJava.java 文件中的内容通过文件输入/输出流写入到了 D:\myJava\HelloJava.txt 文件中。由于 HelloJava.txt 文件并不存在,所以在执行程序时将新建此文件,并写入相应内容。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有