另外参考文章:http://www.ibm.com/developerworks/cn/java/j-lo-javaio/

一. File类

file.createNewFile();file.delete();file.list();file.listFiles();file.isFile();file.isDirectory();file.mkdirs();

删除目录下所有文件:

public void deleteAllFiles(File file) {

  if (file.isFile() || file.list().length == 0) {

    file.delete();

  } else {

    File[] delFiles = file.listFiles();

    for (File delFile : delFiles) {

      deleteAllFiles(delFile);

      delFile.delete();

    }

  }

}

二. 流

流从结构上分为字节流(以字节为处理单位,可以处理二进制数据)和字符流(以字符为处理单位,无法处理二进制数据),它们的底层都是以字节流的方式来实现的

字节流的输入输出流基础是InputStream和OutputStream

字符流的输入输出流基础是Reader和Writer

InputStreamReader是字节流到字符流的桥梁。用于读文件,把文件中的字节读成字符。

OutputStreamWriter是字符流到字节流的桥梁。用于写文件,把字符以字节的形式写入文件。

Java.io体系结构:http://353588249-qq-com.iteye.com/blog/780343

读数据的流程:
open a stream
while more information
read information
close the stream

try {
  InputStream is = new FileInputStream(filePath + "//abc.txt");
  byte[] buff = new byte[200];

  try {
    // 标识每次实际读了多少字节
    int length;
    while (-1 != (length = is.read(buff, 0, 200))) {
      String str = new String(buff, 0, length);
      System.out.println(str);
    }
    is.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
} catch (FileNotFoundException e) {
  e.printStackTrace();
}

写数据的流程
open a stream
while more information
write information
close the stream

String content = "hello world";
// flag用于标识是否在文件内容后再添加内容还是覆盖原有内容,flag=true不覆盖,否则覆盖
OutputStream os = new FileOutputStream(filePath + "//abc.txt", boolean flag);
os.write(content.getBytes());
os.close();

OutputStream os = new FileOutputStream("file.txt");

Writer writer = new OutputStreamWriter(os);

BufferedWriter bw = new BufferedWriter(writer);

bw.write("www.baidu.com");

bw.write("\n");

bw.write("www.qq.com");

bw.close();

InputStream is = new FileInputStream("file.txt");

Reader r = new InputStreamReader(is);

BufferedReader br = new BufferedReader(r);

String str = br.readLine();

while (null != str) {

  System.out.println(str);

  str = br.readLine();

}

br.close();

FileReader = new InputStreamReader(FileInputStream);

FileWriter = new OutputStreamWriter(FileOutputStream);

Serializable

package io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class SerializableTest {

  public static void main(String[] args) throws Exception {
    Person p1 = new Person(10, "person1", 1.5);
    Person p2 = new Person(20, "person2", 1.6);
    Person p3 = new Person(30, "person3", 1.7);

    OutputStream os = new FileOutputStream("person.dat");
    ObjectOutputStream oos = new ObjectOutputStream(os);
    oos.writeObject(p1);
    oos.writeObject(p2);
    oos.writeObject(p3);

    oos.close();

    InputStream is = new FileInputStream("person.dat");
    ObjectInputStream ois = new ObjectInputStream(is);
    Person p;
    for (int i = 0; i < 3; i++) {
      p = (Person) ois.readObject();
      System.out.println(p.name + ", " + p.age + ", " + p.height);
    }

    ois.close();
  }
}

class Person implements Serializable {
  private static final long serialVersionUID = -6460642520310614043L;

  int age;
  String name;
  double height;

  public Person(int age, String name, double height) {
    this.age = age;
    this.name = name;
    this.height = height;
  }
}

Java NIO:参考:http://tutorials.jenkov.com/java-nio/index.html

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class CopyFile {
  public static void main(String[] args) throws Exception {
    String infile = "CopyFile.java";
    String outfile = "CopyFile.txt";
    FileInputStream fin = new FileInputStream(infile);
    FileOutputStream fout = new FileOutputStream(outfile);

    // read data from a channel into a buffer
    FileChannel inChannel = fin.getChannel();
    // write data from a buffer into a channel(Read data out of the Buffer)
    FileChannel outChannel = fout.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(1024);
    while (true) {
      buffer.clear();
      // read data into buffer
      int r = inChannel.read(buffer);
      if (-1 == r) {
        break;
      }
      buffer.flip();
      // write data from buffer
      outChannel.write(buffer);
    }
    inChannel.close();
    outChannel.close();
    fin.close();
    fout.close();
  }
}

JavaIO 总结-装饰者模式的更多相关文章

  1. JavaIO模型--装饰者模式

    JavaIO体现出装饰者的设计模式 今天在学SparkRDD之前,听了一堂复习JavaIO的课,觉得讲得不错 Java的IO一直让我觉得一层一层的很麻烦,刚接触的时候,理不太清楚 只知道要分解为输入输 ...

  2. HeadFirst设计模式之装饰者模式

    一. 1.The Decorator Pattern attaches additional responsibilities to an object dynamically.Decorators ...

  3. IO流与装饰者模式

    java使用IO流来处理不同设备之间数据的交互;所有的IO操作实际上都是对 Stream 的操作 从功能上划分: 输入流: 当数据从源进入的编写的程序时,称它为输入流; 输出流: 从程序输出回另一个源 ...

  4. javaIO系统----再看装饰者模式

    javaIO系统拥有各种各样的类,尤其是每次要进行读写操作时,总会一层套一层的new,以前不明白为什么要这样做,不过学习了适配器模式后,对于这种做法立刻了解了:动态扩展IO的功能,使之符合使用者的习惯 ...

  5. java IO之 字符流 (字符流 = 字节流 + 编码表) 装饰器模式

    字符流 计算机并不区分二进制文件与文本文件.所有的文件都是以二进制形式来存储的,因此, 从本质上说,所有的文件都是二进制文件.所以字符流是建立在字节流之上的,它能够提供字符 层次的编码和解码.列如,在 ...

  6. 由装饰者模式来深入理解Java I/O整体框架

    前言 Java里面的I/O这一部分看过很多遍,每次看完之后特别混乱,又是输入流,又是输出流,又是字符流,又是字节流,还有什么过滤流,缓冲流.每次看得我如入云里雾里,直到后面看了设计模式这一块,才算真正 ...

  7. 装饰者模式 Decoration

    1.什么是装饰者模式 动态给对象增加功能,从一个对象的外部来给对象添加功能,相当于改变了对象的外观,比用继承的方式更加的灵活.当使用装饰后,从外部系统的角度看,就不再是原来的那个对象了,而是使用一系列 ...

  8. JAVA装饰者模式(从现实生活角度理解代码原理)

    装饰者模式可以动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活. 该模式的适用环境为: (1)在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职 ...

  9. 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)

    在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...

随机推荐

  1. ajax常用知识

     同源地址:任意两个地址中的协议,域名,端口相同,称为同源地址 同源策略:  是浏览器的一种基本安全策略                       不允许对非同源地址进行请求(ajax)       ...

  2. HDU1061 - Rightmost Digit

    Given a positive integer N, you should output the most right digit of N^N. Input The input contains ...

  3. LeetCode 856 递归思路详解

    题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) 得 2 * A 分,其中 A 是平衡括 ...

  4. jQuery 简单介绍

    jQuery  简单介绍 jQuery的定义 jQuery是一个快速,小巧,功能丰富的JavaScript库.它通过易于使用的API在大量浏览器中运行,使得   HTML文档遍历和操作,事件处理,动画 ...

  5. Vue基础知识点

    基础知识: vue的生命周期: beforeCreate/created.beforeMount/mounted.beforeUpdate/updated.beforeDestory/destorye ...

  6. 监控aps.net计数器

  7. nutch+hadoop 配置使用

    nutch+hadoop 配置使用 配置nutch+hadoop 1,下载nutch.如果不需要特别开发hadoop,则不需要下载hadoop.因为nutch里面带了hadoop core包以及相关配 ...

  8. Tarjan缩点【模板】

    #include <algorithm> #include <cstdio> #include <map> using namespace std; ); map& ...

  9. [ReactVR] Add Shapes Using 3D Primitives in React VR

    React VR ships with a handful of 3D primitives. We'll importprimitives like <Sphere/>, <Box ...

  10. 混合高斯模型的EM求解(Mixtures of Gaussians)及Python实现源代码

    今天为大家带来混合高斯模型的EM推导求解过程. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVhbnl1YW5zZW4=/font/5a6L5L2T/ ...