今天学习的内容是字节流和字节流缓冲区
字节流与字符流的基本操作相同,注意字节流的输出没有用到缓冲区,下例使用FileOutputStream类和FileInputStream类实现对文本文件的读写(字节流不能直接处理字符内容,只能直接处理字节内容):
public class Test78 {
public static void main(String[] args) {
test_write();
test_read_1();
test_read_2();
}
private static void test_read_2() {
FileInputStream fis = null;
try {
fis = new FileInputStream("test.txt");
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
System.out.print(new String(buf, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
private static void test_read_1() {
FileInputStream fis = null;
try {
fis = new FileInputStream("test.txt");
int ch = 0;
while ((ch = fis.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
private static void test_write() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("test.txt");
fos.write("asdasd".getBytes());// 字节流操作的是字节
// 注意与字符流不同,字节流并没有用到缓冲区,不用flush()
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
}
与字符流一样,字节流也有对应的缓冲区--BufferedOutputStream类和BufferedInputStream类,下面分别使用基本方法和缓冲区实现图片和MP3的复制:
public class Test79 {
public static void main(String[] args) {
copy_picture_1();
copy_picture_2();
copy_mp3_1();
copy_mp3_2();
}
private static void copy_mp3_2() {
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream("萧敬腾 - Beautiful Love.mp3"));
out = new BufferedOutputStream(new FileOutputStream("萧敬腾 - Beautiful Love_copy_2.mp3"));
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
private static void copy_mp3_1() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("萧敬腾 - Beautiful Love.mp3");
fos = new FileOutputStream("萧敬腾 - Beautiful Love_copy_1.mp3");
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
private static void copy_picture_2() {
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream("IO.jpg"));
out = new BufferedOutputStream(new FileOutputStream("IO_copy_2.jpg"));
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
private static void copy_picture_1() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("IO.jpg");
fos = new FileOutputStream("IO_copy_1.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
}