标签:date() exception 一个 说明 acl string 文件内容 AC ace
该类是字节输出流的抽象类,定义了输出流的各种操作方法。如下图是OutputStream的层次结构:
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(); }
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(); }
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(); }
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); //文件路径及名称、编码格式
关于该类的详细说明见:http://www.cnblogs.com/skywang12345/p/io_16.html
该类是字符输出流的抽象类,定义了输出流的各种操作方法。如下图是Writer的层次结构:
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(); }
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(); } }
new FileWriter(File)
new FileWriter(FileDescriptor)
new FileWriter(String)
new FileWriter(File, boolean)
new FileWriter(String, boolean)
具体的使用方法见:http://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html
具体见:http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html
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); }
标签:date() exception 一个 说明 acl string 文件内容 AC ace
原文地址:https://www.cnblogs.com/cxhfuujust/p/9129635.html