java中的IO:节点流的使用 博客分类: java基础 javaio节点流FileInputStreamFileOutputStream
程序员文章站
2024-03-24 20:51:28
...
目录
1.文件字节流
2.文件字符流
3.字节数组流
4.管道流
1.文件字节流
1.1FileIputStream输入流
read() | 从此输入流读取一个字节,返回读取的数据字节,达到文件末尾则返回-1. |
read(Byte[] b) | 从此输入流读取b.length个字节存入b中,返回读取到的有效字节个数,达到文件末尾则返回-1. |
close() | 关闭此输入流并释放资源 |
1.2FileOutputSteam输出流
write(int b) | 将指定字节写入到此输出流 |
write(byte[] b, int off, int len) | 将指定byte数组中从off开始的len个字节写入到此输出流 |
flush() | 刷新此输出流并强制写出所有缓冲的输出字节 |
close() | 关闭此输出流并释放资源 |
1.3实现文件复制
public class FileStreamDemo { // 每次读写一个字节 public static void copy1(File src, File dest) throws IOException { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dest); int value = fis.read(); while (value != -1) { fos.write(value); value = fis.read(); } fos.close(); fis.close(); } // 每次读写字节数组 public static void copy2(File src, File dest) throws IOException { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dest); byte[] temp = new byte[1 << 15]; int value = fis.read(temp); while (value != -1) { fos.write(temp, 0, value); value = fis.read(temp); } fos.close(); fis.close(); } public static void main(String[] args) throws IOException { copy1(new File("c:\\a1.txt"), new File("d:\\b1.txt")); copy2(new File("c:\\a2.jpg"), new File("d:\\b2.jpg")); } }
2.文件字符流
字符流与字节流的用法基本相同,是为了更便捷的读写文字,但是对于其他资源或文件读写会发生错误.利用字符流实现文件复制:
public class File_Reader_Writer { // 通过字符流复制文本文件 public static void copy3(File src, File dest) throws IOException { FileReader fr = new FileReader(src); FileWriter fw = new FileWriter(dest); // 通过char类型数组读写 char[] temp = new char[1 << 15]; int value = fr.read(temp); while (value != -1) { fw.write(temp, 0, value); value = fr.read(temp); } fw.close(); fr.close(); } public static void main(String[] args) throws IOException { copy3(new File("c:\\a3.txt"), new File("d:\\b3.txt")); } }
3.字节数组流
3.1字节数组输入流ByteArrayInputStream
面向数组,读方法与文件流相似.例:
public class ByteArrayInputStreamDemo { public static void main(String[] args) throws IOException { byte[] b = new byte[] { 23, 4, 54, 46, 78, 46, 78, 34, 84, 4 }; ByteArrayInputStream bis = new ByteArrayInputStream(b); int temp = 0; while ((temp = bis.read()) != -1) { System.out.println(temp); } } }
3.2字节数组输入流ByteArrayOutputStream
写方法除了write()以外,还提供了toByteArray()的方法,将输出流中的有效内容复制到该数组中.例:
public class ByteArrayOutputStreamDemo { public static void main(String[] args) throws IOException { final Random r = new Random(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); for (int i = 0; i < 10; i++) { bos.write(r.nextInt(100)); } byte[] b = bos.toByteArray(); System.out.println(Arrays.toString(b)); } }
4.管道流
管道流常用于线程,并且输入流和输出流需要建立联系.例:
public class PipedStreamDemo { public static void main(String[] args) throws Exception { // 创建线程任务 Send s = new Send(); Receive r = new Receive(); // 连接两个管道流 s.getOut().connect(r.getIn()); // 启动线程 new Thread(s).start(); new Thread(r).start(); } } class Send implements Runnable { private PipedOutputStream out = new PipedOutputStream(); public PipedOutputStream getOut() { return out; } @Override public void run() { String message = "This is a PipedStream"; try { out.write(message.getBytes()); } catch (IOException e) { e.printStackTrace(); } } } class Receive implements Runnable { private PipedInputStream in = new PipedInputStream(); public PipedInputStream getIn() { return in; } @Override public void run() { byte[] b = new byte[1 << 10]; int length = 0; try { length = in.read(b); } catch (IOException e) { e.printStackTrace(); } System.out.println(new String(b, 0, length)); } }
运行结果:This is a PipedStream