Java流与文件操作的总结 .

数据流的基本概念

数据流分为输入流和输出流。

输入流只能读不能写,而输出流只能写不能读。

使用数据流的目的是使程序的输入输出操作独立于相关设备,增强程序的可移植性。

 

 

字符流

Reader类和Writer类

字符输入流Reader类是所有面向字符的输入流的超类声明为java.io中的抽象类。

Public abstract class Reader extends Object

字符输出流Writer类是所有面向字符的输出流的超类声明为java.io中的抽象类。

Public abstract class Writer extends Object

Writer的新增方法:

Public void writer(Srtring str) throwsIOException:将字符串写入输出流

Public abstract void flush() throwsIOException:将缓冲区内容写入输出流

输入\输出流对象.close是关闭输入\输出流

 

字符文件流FileReader和FileWriter类

FileReader和FileWriter类分别是InputStreamReader和OutputStreamWriter的子类。

FileReader从父类中继承了read()、close()等方法,FileWriter从父类中继承了write()、close()等方法。

 

字符缓冲流BufferedReader和BufferedWriter类

字符缓冲输入流BufferedReader类  父类是Reader

构造方法:

Public BufferedReader(Reader in):创建一个默认大小的缓冲字符输入流

Public BufferedReader(Reader in,int sz):创建一个指定大小的缓冲字符的输入流

实例方法:

public String readLine() throws IOException:读取一个文件行

 

字符缓冲输出流BufferedWriter类  父类是Writer

构造方法:

Public BufferedWriter(Writer in):创建一个默认大小的缓冲字符输出流

Public BufferedWriter (Writer in,int sz):创建一个指定大小的缓冲字符的输出流

实例方法:

public String newLine() throws IOException:写入一个行分隔符

 

字节流

字节流InputStream和OutputStream类

字节输入流InputStream类   父类是Object

InputStream类的常用的方法:

read():从流中读入数据

skip():跳过流中若干字节数

available():返回流中可用字节数

mark():在流中标记一个位置

reset():返回标记过的位置

markSupport():是否支持标记和复位操作

close():关闭流

字节输入流OutputStream类   父类是Object

OutputStream类的常用的方法:

write(int b):将一个整数输出到流

write(byte b[]):讲数组中的数据输出到流中

write(byte b[],int off,int len):将数组b中从off指定的位置开始len长度的数据输出到流中

flush():将缓冲区中的数据强制送出

close():关闭流

 

文件字节输入\输出流类FileInputStream和FileOutputStream

文件字节输入流类FileInputStream

构造方法:

Public FileInputStream(String name) throwsFileNotFoundException

Public FileInputStream(File file) throwsFileNotFoundException

例如:

FileInputStream f=new FileInputStream(“date.txt”);

读写字节的方法:

Public int read() throws IOException

Public int read(byte[] b) throwsIOException

Public int read(byte[] b,int off,int len)throws IOException

关闭输入流:

Public void close() throws IOException

文件字节输出流类FileOutputStream

构造方法:

Public FileOutputStream (String name)throws FileNotFoundException

Public FileOutputStream (File file) throwsFileNotFoundException

Public FileOutputStream (String name,Booleanappend) throws FileNotFoundException

例如:

FileOutputStreamf=new FileOutputStream (“date.txt”,true);

读写字节的方法:

Public int write() throws IOException

Public int write(byte[] b) throwsIOException

Public int write(byte[] b,int off,int len)throws IOException

关闭输入流:

Public void close() throws IOException

 

文件操作

File类

构造方法:

Public File(String pathname)

Public File(File parent,String child)

Public File(String parent,String child)

 

Public File(URI uri)

Fiel类的方法:

访问文件的对象:

Public String getName():返回文件对象名,不包含路径名

Public String getPath():返回相对路径名,包含文件名

Public String getAbsolutePath():返回绝对路径名,包含文件名

Public String getParent():返回父类文件对象的路径名

Public File getParentFile():返回父文件对象

获得文件属性:

Public long length():返回指定文件的字节长度

Public Boolean exists():测试指定的文件是否存在

Public long lastModified():返回指定文件最后被修改的时间

文件操作:

Public Boolean renameTo(Filedest):文件重命名

Public Boolean delete():删除空目录

目录操作:

Public Boolean mkdir():创建指定目录,正常建立时返回true

Public String[] list():返回目录中的所有文件名字符串

Public File[] listFile():返回指定目录中的所有文件对象

文件过滤器

FileFilter和FilenameFilter接口

具有上面的方法

如果过滤出指定的后缀:可以用endsWith()方法

随机文件操作

RandomAccessFile类

对象序列化

ObjectInputStream

ObjectOutputStream

 

 

 

Java的标准数据流和输入/输出流

标准输入System.in

标准输出System.out

标准的错误输出System.err

 

流操作的基本规律:

 

 

1.  明确数据源和数据目的,

     其实就是在明确是要使用输入流还是输出流。

 

2.  明确操作的数据是否是纯文本数据。

       如果是使用字符流。

       如果不是使用字节流。

 

                   InputStream OutputStream

                   Reader  Writer

 

源设备:键盘System.in,硬盘File,   内存。

目的设备:控制台System.out,硬盘File,   内存。

posted @ 2012-05-16 23:11  hibernate3例子  阅读(323)  评论(0编辑  收藏  举报