Java提供的流类
Java的输入输出流主要由4个抽象类提供。这4个抽象类分别是InputStream类、OutputStream类、Reader类和Writer类。它们是数据流的抽象类,具体针对某个数据源或输出目标的流由它们的子类去实现。例如文件流、字节流、音频流、过滤器流等。
1.InputStream类是字节输入流的抽象类,它是所有字节输入流的父类
◇ InputStream、OutputStream
◇ FileInputStream、FileOutputStream
◇ PipedInputStream、PipedOutputStream
◇ ByteArrayInputStream、ByteArrayOutputStream
◇ FilterInputStream、FilterOutputStream
◇ DataInputStream、DataOutputStream
◇ BufferedInputStream、BufferedOutputStream
◇ 从流中读取数据:
int read( ); //读取一个字节,返回值为所读的字节
int read( byte b[ ] ); //读取多个字节,放置到字节数组b中,通常
//读取的字节数量为b的长度,返回值为实际
//读取的字节的数量
int read( byte b[ ], int off, int len ); //读取len个字节,放置
//到以下标off开始字节
//数组b中,返回值为实
//际读取的字节的数量
int available( ); //返回值为流中尚未读取的字节的数量
long skip( long n ); //读指针跳过n个字节不读,返回值为实际
//跳过的字节数量
◇ 关闭流:
close( ); //流操作完毕后必须关闭
◇ 使用输入流中的标记:
void mark( int readlimit ); //记录当前读指针所在位置,readlimit
//表示读指针读出readlimit个字节后
//所标记的指针位置才失效
void reset( ); //把读指针重新指向用mark方法所记录的位置
boolean markSupported( ); //当前的流是否支持读指针的记录功能
public static void main(String[] args)
{
try
{
InputStream is=System.in;
byte[] bs=new byte [50];
int i=is.read(bs);
System.out.println(new String(bs).trim());
is.close();
}catch(Exception e){ }
}
2.
OutputStream类是字节输出流的抽象类,是所有字节输出流的父类
◇ 输出数据:
void write( int b ); //往流中写一个字节b
void write( byte b[ ] ); //往流中写一个字节数组b
void write( byte b[ ], int off, int len ); //把字节数组b中从
//下标off开始,长度为len的字节写入流中
◇ flush( ) //刷空输出流,并输出所有被缓存的字节
由于某些流支持缓存功能,该方法将把缓存中所有内容强制输出到流中。
◇ 关闭流:
close( ); //流操作完毕后必须关闭
public static void main(String[] args)
{
try
{
OutputStream out=Systm.out;
byte[] bs="OutputStream输出流".getBytes();
out.write(bs);
out.close();
}catch(Exception e){ }
}
3.
◇ Reader、Writer
◇ InputStreamReader、OutputStreamWriter
◇ FileReader、FileWriter
◇ CharArrayReader、CharArrayWriter
◇ PipedReader、PipedWriter
◇ FilterReader、FilterWriter
◇ BufferedReader、BufferedWriter
◇ StringReader、StringWriter
◇ 读取字符
public int read() throws IOException; //读取一个字符,返回值为读取的字符
public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/
public abstract int read(char cbuf[],int off,int len) throws IOException;
/*读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现*/
◇ 标记流
public boolean markSupported(); //判断当前流是否支持做标记
public void mark(int readAheadLimit) throws IOException;
//给当前流作标记,最多支持readAheadLimit个字符的回溯。
public void reset() throws IOException; //将当前流重置到做标记处
◇ 关闭流
public abstract void close() throws IOException;
2. Writer类是处理所有字符流输出类的父类。
◇ 向输出流写入字符
public void write(int c) throws IOException;
//将整型值c的低16位写入输出流
public void write(char cbuf[]) throws IOException;
//将字符数组cbuf[]写入输出流
public abstract void write(char cbuf[],int off,int len) throws IOException;
//将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流
public void write(String str) throws IOException;
//将字符串str中的字符写入输出流
public void write(String str,int off,int len) throws IOException;
//将字符串str 中从索引off开始处的len个字符写入输出流
◇ flush( )
刷空输出流,并输出所有被缓存的字节。
◇ 关闭流
public abstract void close() throws IOException;
public static void main(String[] args)
{
try
{
InputStreamReader in=new InputStreamReader(System.in);
char[] bs=new char[60];
in.read(bs);
String s=new String(bs);
System.out.println(s.trim());
in.close();
}catch(Exception e){ }
4. Writer类是字符输出流的抽象类,所有字符输出流的实现都是它的子类
5.File类的常用方法
public static void main(String[] args) {
String filePath = "src/com/lzw/GetFileInfo.java"; // 根据读者的开发环境指定文件路径
File file = new File(filePath); // 创建文件对象
System.out.println("文件名称:"+file.getName()); // 输出文件属性
System.out.println("文件是否存在:"+file.exists());
System.out.println("文件的相对路径:"+file.getPath());
System.out.println("文件的绝对路径:"+file.getAbsolutePath());
System.out.println("是否可执行文件:"+file.canExecute());
System.out.println("文件可以读取:"+file.canRead());
System.out.println("文件可以写入:"+file.canWrite());
System.out.println("文件上级路径:"+file.getParent());
System.out.println("文件大小:"+file.length()+"B");
System.out.println("文件最后修改时间:"+new Date(file.lastModified()));
System.out.println("是否文件类型:"+file.isFile());
System.out.println("是否文件夹类型:"+file.isDirectory());
}
◇ 文件或目录的生成
public File(String path);/*如果path是实际存在的路径,则该File对象
/*表示的是目录;如果path是文件名,则该File对象表示的是文件。*/
public File(String path,String name);//path是路径名,name是文件名
public File(File dir,String name);//dir是路径名,name是文件名
◇ 文件名的处理
String getName( ); //得到一个文件的名称(不包括路径)
String getPath( ); //得到一个文件的路径名
String getAbsolutePath( );//得到一个文件的绝对路径名
String getParent( ); //得到一个文件的上一级目录名
String renameTo(File newName); //将当前文件名更名为给定文件的
完整路径
◇ 普通文件信息和工具
long lastModified( );//得到文件最近一次修改的时间
long length( ); //得到文件的长度,以字节为单位
boolean delete( ); //删除当前文件
◇ 目录操作
boolean mkdir( ); //根据当前对象生成一个由该对象指定的路径
String list( ); //列出当前目录下的文件
对象流
◇ ObjectInputStream、ObjectOutputStream
2591

被折叠的 条评论
为什么被折叠?



