Java IO

创建: 2020/11/22

完成: 2020/11/22 

https://docs.oracle.com/javase/7/docs/api/java/io/package-summary.html

 

File  
 构造函数  
File(String path)  
File(String directoryPath, String filename)  
File(File directory, String filename)  
File(URI uri)
  • path为空抛出NullPointerException
 静态变量  

static String pathSeparator

static char pathSeparatorChar

路径分隔符

static String separator

static char separatorChar

文件分割符
实例方法  
判断
boolean canExecute() 存在且运行
boolean canRead()  
boolean canWrite()  
boolean exists()  
boolean isAbsolute() path是否是绝对路径
boolean isDirectory()  
boolean isFile()  
boolean isHidden()  
   
   
   
获取
String getAbsolutePath() 绝对路径
String getCanonicalPath() throws IOException 标准路径
long getFreeSpace()

文件系统剩余的容量

单位: 字节(byte)

long getTotalSpace()  
long getUsedSpace()  
String getName() 文件名
String getParent()  
String getPath()  
long lastModified() 1970/1/1GMT到最后更改直接的毫秒
long length()

文件大小

单位: byte

String[] list()

String[] list(FilenameFilter filter)

返回文件夹内的文件

File[] list()

File[] list(FilenameFilter filter)

 
   
设置
boolean setExecutable(boolean flag, boolean owner)
      • owner: true 只有onwer
            false All
      • 成功返回true
boolean setReadable(boolean flag, boolean owner)  
boolean setWritable(boolean flag, boolean owner)  
boolean setLastModified(long t)  
URI toURI() 返回 file:// 开头的URI
   
   
   

boolean createNewFile()

throws IOException

创建新的空文件, 成功返回true
boolean delete() 删除文件。成功返回true
boolean mkdir()

以此文件名创建新文件夹

  • 上层文件夹必须都存在
boolean mkdirs()

以此文件名创建新文件夹

  • 上层文件夹不存在就一并创建
boolean renameTo(File newName) 成功返回true
   
   
   
   
   
   
   
   
   
   
   
   
字符流
简介
  • 两种流, 字符流(charactor stream), 字节流(byte stream)
字符流类关系图 

 继承关系图

    BufferdReader  
  Reader ->    
    InputStreamReader -> FileReader
Object ->      
    BufferdWriter  
  Writer -> OutputStreamWriter -> FileWriter
    PrintWriter  

 

 

 

Writer  
 

 

这是抽象类

 

构造函数
Writer()  
Writer(Object obj)  
实例函数
append
Writer append(char c) throws IOException  
Writer append(CharSequence cs) throws IOException  
Writer append(CharSequence cs, int strat, int end) throws IOException

写入cs里指定范围的部分到流

write
void write(int c) throws IOException  
void write(char buffer[]) throws IOException  

abstract void write(char buffer[], int index, int size)

throws IOExceptio 

  • index写入流起始位置(流的)
  • size写入的字符数
void write(String s) throws IOException  
void write(String s, int index, int size) throws IOException  

abstract void close()

throws IOException

 

abstract void flush()

throws IOException

写入终端
   
   
   
OutputStreamWriter  
简介

Object -> Writer -> OutputStreamWriter

字符流转换为字节流输出到终端

构造函数
OutputStreamWriter(OutputStream os) 不指定encoding则使用系统默认
OutputStreamWriter(OutputStream os, String encoding)  
实例方法
String getEncoding()  
   
   
 FileWriter  
简介

Object -> Writer -> OutputStreamWriter -> FileWriter

向文件输出

构造函数
FileWriter(String filepath) throws IOException  
FileWriter(String filepath, boolean append) throws IOException  
FileWriter(File fileObj) throws IOException  
FIleWriter(File fileObj, boolean append) throws IOException append: true则在末尾添加, false则删除原内容
  没有自己的函数, 全是继承的
   
   
   
   
Reader  

 

 

 

这是抽象类
构造函数
Reader()  
Reader(Object obj)  
实力函数
abstract void close() throws IOException 关闭读取流, 再尝试读取则抛出IOException
void mark(int numChars) throws IOException ???
void reset() throws IOExceptio   
int skip(long numChars) throws IOException 返回实际跳过的数量
read
int read() throws IOException 读取的是一个字符
int read(char buffer[]) throws IOException  
abstract int read(char buffer[], int offset, int numChars) throws IOException  
判断
boolean markSupported() 此流是否支持mark()/reset()
boolean ready() throws IOException read不在读取返回true
   
   
   
InputStreamReader  
简介

Object -> Reader -> InputStreamReader

字节流读取为字符流

构造函数
InputStreamReader(InputStream is)  
InputSreamReader(IntputSream is, String encoding)  
实例函数
String getEncoding()  
   
FileReader  
简介 Object -> Reader -> InputStreamReader -> FileReader
构造函数
FileReader(String filepath) throws FileNotFoundException  
FileReader(File fileObj) throws FileNotFoundException  
  没有自己的函数, 全是继承的
   
带buffer字符流
 简介

 写入和读取都存到buffer里

 好处: 减少直接写入和读取的次数, 提高performance

BufferedWriter  
简介 Objcet -> Writer -> BufferedWriter
构造函数

BuffferedWriter(Writer w)

 
BufferedWriter(Writer w, int bufSize)  
实例函数

void newLine() throws IOException()

 写入一个换行(另起一行)
BufferedReader   
简介 Object -> Reader -> BufferedReader
构造函数
BufferedReader(Reader r)  
BufferedReader(Reader r, int bufSize)  
实例函数
String readLine() throws IOException  
   
   
   
   
   
   
PrintWriter
   Object -> Writer -> PrintWriter
构造函数  
PrintWriter(File fileObj) throws FileNotFoundException  
PrintWriter(OutputStream os)  
PrintWriter(OutputStream os, boolean flushOnNewline)  
PrintWriter(Writer writer)  
PrintWriter(Writer writer, boolean flushOnNewline)  
实例方法  
print
void pirnt(boolean b)  
void print(char c)  
void print(char cs[])  
void print(double d)  
void print(float f)  
void print(int i)  
void print(long l)  
void print(Object o)  
void print(String s)  
   
println
void println()  
其他9个和print一样  
printf

void printf(String s, Object... args)

void format(String s, Object... args)

 

void printf(Locale l, String s, Object... args)

void format(Locale l, String s, Object... args)

 
   
   
   
字节流
字节流类关系图 

继承关系图

    FileInputStream  
  InputStream ->   BufferedInputStream
    FilterInputStream -> DataInputStream
       
Object ->      
    FileOutputStream  
  OutputStream ->   BufferedOutputStream
    FilterOutputStream -> DataOutputStream
      PrintStream
OutputStream   这是抽象类
简介 Object -> OutputStream
构造函数 OutoutStream()
实例方法
void close() throws IOException  
void flush() throws IOException  
write
abstract void write(int i) throws IOException  
void write(byte buffer[]) throws IOException  
void write(byte buffer[], int index int size) throws IOException 把size数量的buffer字符写入index处
FileOutputStream  
简介 Object -> OutputStream -> FileOutputStream
构造函数
FileOutputStream(String filepath) throws FileNotFoundException  
FIleOutputStream(String filepath, boolean append) throws FileNotFoundException  
FileOutputStream(File fileObj) throws FileNotFoundException  
FileOutputStream(File fileObj, boolean append) throws FileNotFoundException  
FilterOutpuStream  
简介

Object -> OutputStream -> FilterOutputStream

用filter过滤后再输出

构造函数
FilterOutputStream(OutputStream os)  
   
BufferedOutputStream  
简介 Object -> OutputStream -> FilterOutputStream -> BufferedOutputStream
构造函数
BufferedOutputStream(OutputStream os)  
BufferedOutputStream(OutputStream os, int bufSize)  
DataOutputStream  
简介

Object -> OutputStream -> FilterOutputStream -> DataOutputStream

能把基本数据类型直接写入的

构造函数
DataOutputStream(OutputStream os)  
   
实例函数
write
void write(int i) throws IOException  
void witer(byte buffer[]) throws IOException  
void write(byte buffer[], int index, int size) throws IOException  
…………………分割线………………  
void writeBoolean(boolean b) throws IOException  

void writeByte(int i) throws IOException

void writeChar(int i) throws IOException

 

void writeBytes(Strings) throws IOException

void writeChars(String s) throws IOException

 
void writeDouble(double d) throws IOException  
void writeFloat(float f) throws IOException  
void writeInt(int i) throws IOException  
void writeLong(long l) throws IOException  
void writeShort(short s) throws IOException  
void writeUTF(String s) throws IOException 把字符串转换从Unicode转为UTF-8再写入
   
   
PrintStream
简介 Object -> OutputStream -> FilterOutputStream -> PrintStream
构造函数
PrintStream(File fileObj) throws FileNotFoundException  
PrintStream(String filepath) throws FileNotFoundException  
PrintStream(OutputStream os)  
PrintStream(OutputStream os, boolean flushOnNewLine)  
实例方法
printf

printf(String format, Object... args)

format(String format, Object... args)

 

printf(Locale l, String format, Object... args)

format(Locale l, String format, Object... args)

 
   
   
 InputStream  这是抽象类
构造函数 InputStream()
实例方法  
int available() throws IOException 返回现在读取可用的字节数byte
void close() throws IOexception  
void mark(int numBytes)  
void reset() throws IOException  
int skip(long numBytes) throws IOException 跳过指定byte数, 返回实际跳过的byte数
boolean markSupported() 是否支持mark()/reset()
read
int read() throws IOException 读1byte
int read(byte buffer[]) throws IOException 读buffer.length长度

int read(byte buffer[], int offset, int numBytes)

throws IOException

  • 返回实际读取数
  • offset: 存入的起始位置
  • numBytes: 尝试读取数
   
FileInputStream  
简介 Object -> InputStream -> FileInputStream
构造函数
FileInputStream(String filepath) throws FileNotFountException  
FileInputStream(File fileObj) throws FileNotFoundException  
FilterInputStream   
简介 Object -> InputStream -> FilterInputStream
构造函数
FilterInputStream(InputStream is)  
   
BufferedInputStream
简介 Object -> InputStream -> FilterInputStream -> BufferedInputStream
构造函数

 

BufferedInputStream(InputStream is)  
BufferedInputStream(InputStream is, int bufSize)  

 

DataInputStream
简介 Object -> InputStream -> FilterInputStream -> DataInputStream
构造函数
DataInputStream(InputStream is)  
实例函数
read  
int skipBytes(int n) throws IOException 跳过n字节, 返回实际跳过的
随机接入文件
简介

可以在文件内随意读取写入

Object -> RandomAccessFile

https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html

构造函数  

RandomAccessFile(String filepath, String mode) throws FileNotFoundException

RandomAccessFile(File fileobj, String mode) throws FileNotFoundException

  • mode: r, rw, rws, rwd
实例方法  
void close() throws IOException  
long getFilePointer() throws IOException  
long length() throws IOException 返回文件的byte数
read
int read() throws IOException 读取一字节
int read(byte buffer[], int index, int size) throws IOException 返回实际读取的字节数
int read(byte buffer[]) throws IOException  
void seek(long n) throws IOException 指针指向开头起n的位置
void setLength(long size) throws IOException  
int skipBytes(int n) throws IOException  
   
   
StreamTokenizer
构造函数   
StreamTokenizer(Reader r)  
实例函数   
void commentChar(int ch) 指定ch是单行注释的开始
void eollsSignificant(boolean flag)

true: 行末作为token

flase: 作为空白

int lineno()  
void lowerCaseMode(boolean flag) true: 把文字自动转为小写
int nextToken() throws IOException

数字返回TT_NUMBER

单词返回TT_WORD

其他则返回相同的字符

ordinaryChar
void ordinaryChar(int ch)  
void ordinaryChar(int c1, int c2)  
void parseNumbers()  
void pushBacK()  
void quoteChar(int ch)  
void resetSyntax()  
void slashSlashComments(boolean flag)  
void slashStraComments(boolean flag)  
String toString()  
void whiteSpaceChars(int c1, int c2)  
void wordChars(int c1, int c2)  
   
posted @ 2020-11-22 17:44  懒虫哥哥  阅读(86)  评论(0编辑  收藏  举报