您的位置:首页 > 编程语言 > Java开发

梦入IBM之java基础第十六天

2012-05-16 21:02 225 查看
十六天了,现在我们来谈谈几个核心!

今天开始谈论最大的核心!

IO流!

1):关于IO流的先驱:File类!

1):File类对象表示的是磁盘上的一个文件或者是一个目录(即文件夹),File类中封装了对他们的操作。

2):File类描述了文件本省的属性。File类对象可以获取到文件的权限,时间等等

创建文件是:File file = new File(文件名);

file.createNewFile()即创建一个文件。

file.mkdir方法创建目录

2):关于IO的核心之一字节流(传输图片,视频等等必须用它!):1):流可分为输出流 字节流 节点流

输入流 字符流 过滤流(都要通过节点流获得,只有一个过滤流不需要,那就是FileReader和FileWriter)

文件的三大操作:1):创建读流的对象(即打开流)

2):写东西或读东西

3):关闭流

2):来看FileInputStream:

FileInputStream in = new FileInputStream("d:/abc.txt");

byte[] b = new byte[1000];

int length = 0;

while(-1!=(length=in.read(b,0,b.length)))

{

String str = new String(b,0,length);

System.out.println(str);

}

在这里read()是读一段(为b的length那么长,放到b中)

写的话:FileOutputStream

FileOutputStream out = new FileOutputStream("d:/abc.txt",true);

out.write("yinjian".getBytes());

3):BufferedInputStream是过滤流,所以要包装节点流,其他的和FileInputStream一样!

4):ByteArrayInputStream是将程序中byte数组作为输入的起点的流!

ByteArrayOutputStream是将程序中的byte输出到对象的byte缓冲区里面!通过它的toByteArray()可以拿出来

5):二进制的输入输出流DataOutStream与DataInputStream(过滤流

3):IO的有一大核心:字符流:

1):InputStreamReader和OutputStreamWtiter用来处理字节流和字符流的桥梁!用于包装字符流!

读:

FileInputStream in = new FileInputStream("d:/abc.txt");

InputStreamReader ins = new InputStreamReader(in);

BufferedReader bos = new BufferedReader(ins);

String str = null;

while((str=bos.readLine())!=null)

{

System.out.println(str);

}

写:

FileOutputStream out = new FileOutputStream("d:/abc.txt");

OutputStreamWriter ous = new OutputStreamWriter(out);

BufferedWriter bus = new BufferedWriter(ous);

bus.write("yinjiandfdfdfd");

bus.close();

还有一个是PrintWriter

4):对象的序列化:将对象的非静态成员变量保存到文件中!

1):必须实现Seralizable接口!而transient表示该成员变量不会被序列化!

2):通过调用ObjectOutputStream(过滤流)的writeObject方法来序列化。

思维导图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: