字节流
- 字节流(byte stream)类以字节为单位处理数据;
- 字节流不会对数据进行任何转换,因此可以用来处理二进制数据;
1. 输入流
- 字节输入流的作用是从数据输入源(例如磁盘、网络等)获取字节数据到应用程序(内存)中;
- InputStream 是一个定义了 Java 流式字节输入模式的抽象类,该类的所有方法在出错时都会引发一个 IOException 异常;
a. InputStream 的常用方法
类型 |
方法 |
说明 |
int |
available() |
返回当前可读的输入字节数 |
void |
close() |
关闭输入流,关闭后再读取会抛出 IOException 异常 |
void |
mark(int numBytes) |
在输入流的当前点设置一个标记,该流在读取 numBytes 个字节前都保持有效 |
boolean |
markSupported() |
如果调用的流支持 mark()/reset() 就返回 true |
int |
read() |
如果下一个字节可读则返回一个整型,遇到文件结尾时返回 -1 |
int |
read(byte buffer[]) |
尝试读取 buffer.length 个字节到 buffer 中,返回实际读取的字节数,遇到文件结尾时返回 -1 |
int |
read(byte buffer[], int offset, int numBytes) |
尝试读取 buffer[offset] 开始的 numBytes 个字节,返回实际读取的字节数,遇到文件结尾时返回 -1 |
void |
reset() |
重新设置输入指针到先前设置的标志处 |
long |
skip(long numBytes) |
忽略 numBytes 个输入字节,返回实际忽略的字节数 |
b. FileInputStream 类
构造方法 |
说明 |
FileInputStream(String filepath) |
文件的绝对路径,比较常用 |
FileInputStream(File fileObj) |
fileObj 是描述该文件的 File 对象,允许在把文件赋给输入流之前用 File 方法更进一步检查文件 |
c. 读取并显示文件
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
byte[] b = new byte[1024];
int n = -1;
System.out.println("请输入要读取的文件名:(例如:d:\\hello.txt)");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
try {
FileInputStream in = new FileInputStream(str);
while ((n = in.read(b, 0, 1024)) != -1) {
String s = new String(b, 0, n);
System.out.println(s);
}
in.close();
} catch (IOException e) {
System.out.println("文件读取失败");
}
}
}
2. 输出流
- 字节输出流的作用是将字节数据从应用程序(内存)中传送到输出目标,如外部设备、 网络等;
- OutputStream 是一个定义了 Java 流式字节输出模式的抽象类,该类的所有方法都会返回一个 void 值,并在出错时都会引发一个 IOException 异常;
a. OutputStream 的常用方法
类型 |
方法 |
说明 |
void |
close() |
关闭输出流,关闭后再操作会抛出 IOException 异常 |
void |
flush() |
刷新输出缓冲区 |
void |
write(int b) |
向输出流写入单个字节,参数是个整型数 |
void |
write(byte buffer[]) |
向一个输出流写一个完整的字节数组 |
void |
write(byte buffer[], int offset, int numBytes) |
写数组 buffer 以 buffer[offset] 为起点的 numBytes 个字节区域内的内容 |
b. FileOutputStream 类
构造方法 |
说明 |
FileOutputStream(String filepath) |
文件的绝对路径 |
FileOutputStream(File fileObj) |
fileObj 是描述该文件的 File 对象 |
FileOutputStream(String filePath, boolean append) |
如果 append 为 true,文件则以设置搜索路径模式打开 |
- FileOutputStream 的创建不依赖于文件是否存在,它会在打开输出文件后就创建它,如果尝试打开一个只读文件,则会引发 IOException 异常;
- 在完成操作过程中,系统会将数据暂存到缓冲区,缓冲区满了后再一次性写入到输出流中,执行 close() 方法时,不管缓冲区是否已满,都会把其中的数据写到输出流;
c. 写入数据
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String content;
byte[] b;
FileOutputStream out;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入文件名:(例如,d:\\hello.txt)");
String filename = scanner.nextLine();
File file = new File(filename);
if (!file.exists()) {
System.out.println("文件不存在,是否创建?(y/n)");
String f = scanner.nextLine();
if (f.equalsIgnoreCase("n"))
System.exit(0);
else {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("创建失败");
System.exit(0);
}
}
}
try {
content = "Hello";
b = content.getBytes();
out = new FileOutputStream(file);
out.write(b);
out.close();
System.out.println("文件写操作成功!");
} catch (IOException e) {
e.getMessage();
}
try {
System.out.println("请输入追加的内容:");
content = scanner.nextLine();
b = content.getBytes();
out = new FileOutputStream(file, true);
out.write(b);
out.close();
System.out.println("文件追加写操作成功!");
scanner.close();
} catch (IOException e) {
e.getMessage();
}
}
}
c. 写入并读出数据
import java.io.*;
public class Test {
public static void main(String args[]) {
File f = new File("C:\\Users\\80626\\Desktop\\1.txt");
OutputStream out = null;
try {
out = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte b[] = "Hello World!!!".getBytes();
try {
out.write(b);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
out.close();
} catch (IOException e2) {
e2.printStackTrace();
}
InputStream in = null;
try {
in = new FileInputStream(f);
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
byte b1[] = new byte[1024];
int i = 0;
try {
i = in.read(b1);
} catch (IOException e4) {
e4.printStackTrace();
}
try {
in.close();
} catch (IOException e5) {
e5.printStackTrace();
}
System.out.println(new String(b1, 0, i));
}
}