字节输入/输出流

读写方法
void writeObject(Object obj)---写对象
Object readObject()---读对象

注意:对象串行化(Serializable)的问题

(1)保证对象是串行化(实现Serializable接口)

(2)不希望串行化的对象要用关键字transient修饰。

Reader类和Writer类用来专门处理字符流。
这两个类都是抽象类,从他们派生出了许多子类,增强功能、提高效率,实现各种不同要求的字符输入/输出流的处理。

InputStreamReader/OutputStreamWriter

InputStreamReader
public InputStreamReader(InputStream in)
public InputStreamReader(InputStream in,String enc)
int read()
String getEncoding()
void close();

OutputStreamWriter
public OutputStreamWriter(OutputStream in)

public OutputStreamWriter(OutputStream in,String enc);
write();
String getEncoding();
void close();

BufferedReader/BufferedWriter

BufferedReader
public BufferedReader(Reader in)

public BufferedReader(Reader in, int sz)

read();
readLine()

BufferedWriter
public BufferedWriter(Writer in)

public BufferedWriter(Writer in, int sz)

newLine();
write() ;

文件类(File

一个File类的对象,表示磁盘上的文件和目录。它提供了与平台无关的方法来对磁盘上的文件或目录进行操作,包括:更改文件名、删除文件、列出目录下的文件以及文件对象属性的描述信息等。

构造方法

public File(String path);

public File(String path,Stringname);

public File(File parent,String chile);

静态常量

由于不同操作系统的文件分隔符以及路径分隔符不同,因此,如果要获取当前系统的文件和路径的分隔符,可使用以下两个静态常量。

public static final char separatorChar;

public static final char pathSeparatorChar;

public static final char separator;

文件的随机访问类

RandomAccessFile类可以对文件进行随机读写操作。

创建一个RandomAccessFile对象

RandomAccessFile(File file,String mode)

RandomAccessFile(String name,String mode)

例如:File filenew File(“d:\\a.txt”);

RandomAccessFile rf=new RandomAccessFile( file,”rw”) ;

RandomAccessFile rf=new RandomAccessFile(“d:\\a.txt”,”rw”);

读写数据的常用方法

读、写基本数据类型: readInt();writeInt(int n)等;

读、写UTF字符串: readUTF();writeUTF(String str);

读取文件中的一行:readLine();

文件随机读写流的读取指针控制

long getFilePointer() ---得到当前的文件读取指针。

void seek(long pos) ---把指针从开始移动到pos位置。

long length() ---得到文件的长度(有多少个字节)

void setLength(long newLength)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值