Java中输入输出流方法总结

来源:互联网 发布:万网域名解析修改端口 编辑:程序博客网 时间:2024/06/05 10:51

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( );//
得到文件最近一次修改的时间

字串1

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

 

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)

 

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()
;返回文件名的哈希码

 

0 0
原创粉丝点击