目录
1.文件字节流
2.文件字符流
3.字节数组流
4.管道流
?
class="java">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")); } }
?
字符流与字节流的用法基本相同,是为了更便捷的读写文字,但是对于其他资源或文件读写会发生错误.利用字符流实现文件复制:
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")); } }
?
面向数组,读方法与文件流相似.例:
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); } } }
写方法除了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)); } }
?
管道流常用于线程,并且输入流和输出流需要建立联系.例:
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