OutputStream 和 Writer
-
OutputStream类(直接操作byte数组)
该类是字节输出流的抽象类,定义了输出流的各种操作方法。如下图是OutputStream的层次结构:
- ByteArrayOutputStream:字节数组流,可以捕获内存缓冲区的数据,转换为字节数组。该类有两个构造方法:
new ByteArrayOutputStream();
new ByteArrayOutputStream(int size); //size表示初始化字节数组缓冲区的大小
ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write('q'); bos.write('a'); //将字节写入该字符数组 bos.reset(); //重置该字节数组,即将如上写入的'q' 'a'字节清空 byte[] b = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n'}; bos.write(b, 1, 7); //从b数组的第一个下标连续写入长度为7个字符 try { FileOutputStream fs = new FileOutputStream("SourceFile/employee"); bos.writeTo(fs); //将字符数组写入文档 fs.close(); bos.flush(); bos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- FileOutputStream:以字节流的方式将二进制数据或者字符数据输出到文件中,该类有5个构造方法,我们在代码中介绍了2和4的用法:
- new FileOutputStream(File);
- new FileOutputStream(FileDescriptor); //FileDescriptor.out将内容输出到控制台
- new FileOutputStream(String); //String为文件路径
- new FileOutputStream(File, boolean); //boolean为true时,则不覆盖文件,在文件的末尾添加内容,false则覆盖文件
- new FileOutputStream(String, boolean); //同上
try { FileOutputStream fs1 = new FileOutputStream(FileDescriptor.out); FileOutputStream fs2 = new FileOutputStream(new File("SourceFile/employee"), true); //在该文件的末尾添加内容 fs1.write("https://www.cnblogs.com/zhanglei93/".getBytes()); //write()方法可以写入byte数组、int fs1.close(); fs2.write("https://www.cnblogs.com/zhanglei93/".getBytes()); fs2.flush(); //清空缓存里的数据,并通知底层去进行实际的写操作 fs2.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- BufferedOutputStream是一个缓冲数据输出流接口,类中有一个byte数组,调用write()函数时,首先向这个数组中写入数据,然后当某些时刻(数组写满等)会将这些数组写入到流之中,该类有两个构造方法:
new BufferedOutputStream(OutputStream)
new BufferedOutputStream(OutputStream,int) //int的值规定了byte数组的大小
try { FileOutputStream fs = new FileOutputStream("SourceFile/employee"); BufferedOutputStream bos = new BufferedOutputStream(fs); bos.write("https://www.cnblogs.com/zhanglei93/".getBytes()); //write()方法可以写入byte数组、int fs.close(); bos.flush(); bos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- PrintStream可以方便的输出各种类型的数据,该类主要用于操作字节流,且该类的方法不抛出IOException。该类有8个构造方法:
new PrintStream(File);
new PrintStream(OutputStream);
new PrintStream(String); //文件路径及名称
new PrintStream(File, String); //String 编码格式
new PrintStream(OutputStream, boolean); //是否自动刷新
new PrintStream(OutputStream, boolean, String); //是否自动刷新、编码格式
new PrintStream(String, String); //文件路径及名称、编码格式
关于该类的详细说明见:
-
Writer类(首先进行decode、encode)
该类是字符输出流的抽象类,定义了输出流的各种操作方法。如下图是Writer的层次结构:
- BufferedWriter通过创建缓冲数组,将写入内容先存入缓存,该类有2个构造函数:
new BufferedWriter(Writer)
new BufferedWriter(Writer, int) //int大小为默认数组的大小
try { BufferedWriter bw = new BufferedWriter(new FileWriter("SourceFile/employee")); bw.write("http://www.cnblogs.com/zhanglei93/".toCharArray()); //写入char数组 bw.write("http://www.cnblogs.com/zhanglei93/"); //写入String,还可以写入int CharSequence csq = "http://www.cnblogs.com/zhanglei93/p/5846592.html"; bw.append(csq, 0, 34); bw.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
- CharArrayWriter创建char缓冲数组,也有两个构造函数:
new CharArrayWriter();
new CharArrayWriter(int);
CharArrayWriter cw = new CharArrayWriter(5); for(Employee e : employees){ try { cw.write(e.getName()); cw.append(e.getSalary() + ""); cw.write(e.getDate().toString() + "\r\n"); FileWriter fw = new FileWriter("SourceFile/employee"); cw.writeTo(fw); fw.close(); cw.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
- FileWriter该类包含5个构造方法:
new FileWriter(File)
new FileWriter(FileDescriptor)
new FileWriter(String)
new FileWriter(File, boolean)
new FileWriter(String, boolean)
具体的使用方法见:
- PrintWriter该类有8种构造方法:
具体见:
PrintWriter pw = null; /** * PrintWriter(String fileName, String csn) * 创建具有指定文件名称和字符集且不带自动行刷新的新 PrintWriter。如不执行pw.close()则不刷新文件内容 * @param name * @param code * @param employees */ public void writeData(String name, String code, Employee[] employees){ try { pw = new PrintWriter(name, code); writeToFile(pw, employees); pw.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * PrintWriter(Writer out, boolean autoFlush) * 创建新 PrintWriter, flag = true表示能自动刷新,即不执行pw.close()也会自动刷新内容到文件 * @param write * @param flag * @param employees */ public void writeData(Writer write, boolean flag, Employee[] employees){ pw = new PrintWriter(write, flag); writeToFile(pw, employees); pw.close(); } private void writeToFile(PrintWriter pw, Employee[] employees){ pw.println(employees.length); for(Employee e : employees) e.writeEmployee(pw); }