Java中输入输出流方法总结

1. InputStream OutputStream
read()
:从流中读入数据
数组

skip():跳过流中若干字节数
available()
:返回流中可用字节数
mark()
:在流中标记一个位置
reset()
:返回标记过得位置
markSupport()
:是否支持标记和复位操做
close()
:关闭流
int read()
从输入流中读一个字节,造成一个0255之间的整数返回(是一个抽象方法)。
int read(byte b[])
l
读多个字节到数组中。
int read(byte b[], int off, int len)
write(int b)
将一个整数输出到流中(只输出低位字节,抽象)
write(byte b[])
将字节数组中的数据输出到流中
write(byte b[], int off, int len)
将数组b中从off指定的位置开始,长度为len的数据输出到流中
flush()
:刷空输出流,并将缓冲区中的数据强制送出
close()
:关闭流
从输入流中读取长度为len的数据,写入数组b中从索引off开始的位置,并返回读取得字节数。进行I/O操做时可能会产生I/O例外,属于非运行时例外,应该在程序中处理。如:型FileNotFoundException, EOFException, IOException
工具

 

2. 文件操做
File

File
类声明以下:
public class File ectends Object implements Serializable,Comparable
构造方法:
public File(String pathname)
public File(File patent,String chile)
public File(String patent,String child)
文件名的处理
String getName( )
//获得一个文件的名称(不包括路径)
String getPath( )
//获得一个文件的路径名
String getAbsolutePath( )
//获得一个文件的绝对路径名
String getParent( )
//获得一个文件的上一级目录名
String renameTo(File newName); //
将当前文件名改名为给定文件的完整路径
文件属性测试
boolean exists( )
//测试当前File对象所指示的文件是否存在
boolean canWrite( )
//测试当前文件是否可写
boolean canRead( )
//测试当前文件是否可读
boolean isFile( )
//测试当前文件是不是文件(不是目录)
boolean isDirectory( )
//测试当前文件是不是目录
普通文件信息和工具
long lastModified( );//
获得文件最近一次修改的时间
测试

字串1spa

long length( ); //获得文件的长度,以字节为单位
boolean delete( ); //
删除当前文件
目录操做
boolean mkdir( ); //
根据当前对象生成一个由该对象指定的路径
String list( ); //
列出当前目录下的文件
orm

 

3. 字符流对象

Reader
• void close()
• void mark(int readAheadLimit)
• boolean markSupported()

• int read()
• int read(char[] cbuf)
• int read(char[] cbuf, int off, int len)
• boolean ready()
• void reset()
• long skip(long n)
Writer

• void close()
• void flush()
• void write(char[] cbuf)
• void write(char[] cbuf, int off, int len)
索引

字串7
• void write(int c)
• void write(String str)
• void write(String str, int off, int len)
ip

 

4. 若是你建立了一个文件对象,你可使用下面的方法来得到文件的相关信息,对文件进行操做。
1)
文件名的操做
public String getName()
;返回文件对象名字符串,串空时返回null
public String toString()
;返回文件名字符串

public String getParent()
;返回文件对象父路径字符串,不存在时返回null
public File getPath()
;转换相对路径名字符串

public String getAbsolutePath()
;返回绝对路径名字符串,若是为空返回当前使用目录,也可使用系统指定目录
public String getCanonicalPath()throws IOException
;返回规范的路径名串
public File getCanonicalFile()throws IOException
;返回文件(含相对路径名)规范形式
public File getAbsoluteFile()
;返回相对路径的绝对路径名字符串
public boolean renameTo(File dest)
;重命名指定的文件
public static Fiel createTempFile(String prifix,String suffix,File directory)throws IOException
;在指定目录创建指定先后缀空文件
public static Fiel createTempFile(String prifix,String suffix)throws IOException
;在指定目录创建指定先后缀文件
public boolean createNewFile()throws IOException
;当指定文件不存在时,创建一个空文件
2)
文件属性测试
public boolean canRead()
;测试应用程序是否能读指定的文件
public boolean canWrite()
;测试应用程序是否能修改指定的文件
public boolean exists()
;测试指定的文件是否存在
public boolean isDirectory()
;测试指定文件是不是目录
public boolean isAbsolute()
;测试路径名是否为绝对路径
public boolean isFile()
;测试指定的是不是通常文件
public boolean isHidden()
;测试只的是不是隐藏文件
3)
通常文件信息和工具
public long lastModified()
;返回只的文件最后被修改的时间
public long length()
;返回指定文件的字节长度
public boolean delete()
;删除指定的文件
public void deleteOnExit()
;当虚拟机执行结束时请求删除指定的文件或目录
4)
目录操做
public boolean mkdir()
;建立指定的目录,正常创建时返回true,不然返回false
public boolean mkdirs()
;常见指定的目录,包含任何不存在的父目录

public String[]list()
;返回指定目录下的文件(存入数组)
public String[]list(FilenameFilter filter)
;返回指定目录下知足指定文件过滤器的文件
public File[]listFiels()
;返回指定目录下的文件
public File[]listFiles(FilenameFilter filter)
;返回指定目录下知足指定文件过滤器的文件
public File[]listFiles(FileFilter filter)
;返回指定目录下知足指定文件过滤器的文件(返回路径名应知足文件过滤器)
public static File[]listRoots()
;列出可用文件系统的根目录结构
5)
文件属性设置
public boolean setLastModified(long time)
;设置指定文件或目录的最后修改时间,操做成功返回true,不然返回false
public boolean setReadOnly()
;标记指定的文件或目录为只读属性,操做成功返回true,不然返回
false
6)
其余

public URL toURL()throws MalformedURLException
;把相对路径名存入URL文件
public int compareTo(OBject o)
;与另外一个对性比较名字
public boolean equals(Object obj)
;与另外一个对象比较对象名
public int hashCode()
;返回文件名的哈希码
字符串

相关文章
相关标签/搜索