小白学java-基础篇(IO包)

1、File类(操作文件本身):
a、public static final String pathSeparator表示路径的分隔符(window是:);
b、public static final String separator表示路径的分隔符(window是\);
c、public File(String pathname)创建File对象,传入完整路径;
d、public boolean creatNewFile() throws IOException创建新文件;
e、public boolean delete()删除文件;
f、 public boolean exists()判断文件是否存在;
g、public boolean isDirectory()判断给定的路径是否一个目录;
h、public long length()返回文件的大小;
i、public String[] list()列出指定目录的全部内容,只列出名称;
j、public File[] listFiles()列出指定目录的全部内容,会列出路径;
k、public boolean mkdir()创建一个目录;
l、public boolean renameTo(File dest)重命名;

2、RandomAccessFile类(操作文件内容):
a、public RandomAccessFile(File file,String mode) throws FileNotFoundException接收File对象,指定操作路径,设置需要设置权限;
b、public RandomAccessFile(String name,String mode) throws FileNotFoundException不再用File对象表示文件,而是路径;
c、public void close() throws IOException关闭操作;
d、public int read(byte[] b) throws IOException将内容读取到一个byte数组中;
e、public final byte readByte() throws IOException读取一个字节;
f、public final int readInt() throws IOException从文件中读取整形数据;
g、public void seek(long pos) throws IOException设置读指针的位置;
h、public final void writeBytes(String s) throws IOException将一个字符串写入到文件中,按字节方式处理;
i、public final void writeInt(int i) throws IOException将一个int型数据写入到文件中,长度为4位;
j、`public int skipBytes(int i) throws IOException指针跳过字节;

3、字节流和字符流:
字节流主要操作byte类型数据,主要有OutputStream类和InputStream类;
OutputStream是一个抽象类,需要通过子类向上转型实例化;
a、public void close() throws IOExcception关闭输出流;
b、public void flush() throws IOExcception刷新缓冲区;
c、public void write(byte[] b) throws IOException将一个byte数组写入数据流;
d、public void write(byte[] b,int off,int len) throws IOException将一个指定范围的byte数组写入数据流;
e、public abstract void write(int b) throws IOException将一个字节数据写入数据流;
与之相对应的是InputStream类:
a、public int available() throws IOException可以取得输入文件的大小;
b、public void close() throws IOException关闭输入流;
c、public abstract int read() throws IOException读取内容,以数字的方式读取;
d、public int read(byte[] b) throws IOException将内容读到byte数组,痛死返回读入的个数;
将文件内容读出,现将文件内容读入数组b[i]=(byte)input.read(),之后字符串输出new String(b,0,b.length),实例化数组是,指定数组空间为b.length个长度,这样不引起数组开辟空间的浪费,但是这里的b.length是已知的,如果是未知的,可以用是否读到文件末尾来判断:
while ((temp = input.read())!=-1){
//若temp不为-1,表示文件未读完
b[len]=(byte)temp;
len++
}

字符流也有输入输出类Reader和Writer,方法与字节流无大的差别,只不过字符流不用先转byte数组,然后再转字符串输出,可以操作String、char型数据;
字节流不会用到缓冲区,而字符流需要用到缓冲区,关闭程序是强制性把缓冲区的内容输出,否则不会输出;
字符流在不关闭程序也想输出内容时,可以用flush()方法完成。
!日后需要更深入的学习

4、内存操作流:
ByteArrayInputStream、ByteArrayOutputStream是针对内存的操作流,方法也与文件操作流程类似,一般用于临时性文件。

5、管道流:
PipedInputStream、PipedOutputStream是针对两个线程间的通信。

6、打印流:
是IO包最方便的输出类,主要有字节打印流(PrintStream)和字符打印流(PrintWriter);
PrintStream类:
a、public PrintStream(File file)throws FileNotFoundExcception通过一个File实例化PrintStream对象;
b、public PrintStream(OutputStream out)接收OutputStream对象,实例化PrintStream对象;
c、public PrintStream printf(Locale l,String format,Object.....args)根据指定的locale格式化输出;
d、public PrintStream printf(String format,Object.....args)根据本地环境格式化输出;

输入\输出重定向:
a、public static void setOut(PrintStream out)重定向输出流;
b、public static void setIn(PrintStream in)重定向输入流;

7、BufferedReader类:
用于从缓冲区读取内容,比较常用于键盘输入数据的标准格式;
a、public BufferedReader(Read in)接收一个Read类的实例;
b、public String readLine() throws IOException一次性从缓冲区全部读取数据;
构造方法只能接收字符输入流的实例,因此可能会用到InputStreamRead将字节输入流转换成字符输入流;

8、Scanner类:
主要用于对输入数据进行验证;
a、public Scanner(File source) throws FileNotFoundException从文件中接收内容;
b、public Scanner(InputStream source)从指定的字节输入流接收内容;
c、public boolean hasNext(Pattern pattern)判断输入的数据是否符合指定的正则表达式;
d、public boolean hasNextInt()判断输入的数据是否是整数;
e、public boolean hasNextFloat()判断输入的数据是否是小数;
f、public String next()接收内容;
g、public String next(Pattern pattern)接收内容,进行正则验证;
h、public int nextInt()接收整数;
i、public float nextFloat()接收小数;
j、public Scanner userDelimiter(String pattern)设置读取的分隔符;

9、数据操作流:
与平台无关,能够按一定格式输入输出(DataOutputStreamDataInputStream);
DataOutputStream类:
a、public DataOutputStream(OutputStream out)实例化对象;
b、public final void writeInt(int v)throws IOException将一个int值以4-byte形式写入基础输出流;
c、public final void writeDouble(double v)throws IOException将一个double值以8-byte形式写入基础输出流;
d、public final void writeChars(String s)throws IOException将一个字符串写入基础输出流;
e、public final void writeChar(String s)throws IOException将一个字符写入基础输出流;

DataInputStream类:
a、public DataInputStream(InputStream in)实例化对象;
b、public final int readInt()throws IOException从输入流中读取整数;
c、public final float readFloat()throws IOException从输入流中读取小数;
d、public final char readChar()throws IOException从输入流中读取字符;

10、合并流:
主要是用于将两个文件的内容合并成一个文件;
SequenceInputStream类:
a、public SequenceInputStream(InputStream s1,InputStream s2)使用两个输入流对象实例化本类对象;
b、public int avaiable()throws IOException返回文件大小;

11、压缩流:
主要用于压缩文件;
ZipOutputStream类(压缩输出):
a、public ZipOutputStream(OutputStream out)创建新的ZIP输出流;
b、public void putNextEntry(ZipEntry e)throws IOException设置每一个ZipEntry对象;
c、public void setComment(String comment) 设置ZIP的注释;

ZipFile类(表示被压缩的文件):
a、public ZipFile(File file)throws ZipException,IOException根据File类实例化ZipFile对象;
b、public ZipEntry getEntry(String name)根据名称找到对应的ZipFile;
c、public InputStream getinputStream(ZipEntry entry)throws IOException根据ZipEntry取得InputStream对象;
d、public String getName()得到压缩文件的路径名称;

ZipInputStream类(结合ZipEntry,用于被压缩文件多的的压缩文件):
a、public ZipInputStream(InputStream in)实例化ZipInputStream对象;
b、public ZipEntry getNextEntry()throws IOException取得下一个ZipEntry;

!!!其他格式如JAR、GZIP的操作参考JDK文档,与此ZIP都留日后深入学习

12、回退流:
PushbackInputStream类用于在输入流跳过不需要的字段,完全针对输入流;
a、public PushbackInputStream(InputStream in)将输入流放入到回退流中;
b、public int read()throws IOException读取数据;
c、public int read(byte[] b,int off,int len)throws IOException读取指定范围的数据;
d、public int unread(int b)throws IOException回退一个整数到缓冲区前面;
e、public int unread(byte[] b)throws IOException回退一个数组到缓冲区前面;
f、public int unread(byte[] b,int off,int len)throws IOException

13、对象序列化:
!这里对象序列化的机制和作用不明,留以后深入学习

猜你喜欢

转载自blog.csdn.net/qq_29070549/article/details/79448127