Fork me on GitHub

IO编程

所有的IO操作都在java.io包中进行定义,一共包含5个类和一个接口:

  • 五个类:File、InputStream、OutputStream、Reader、Writer
  • 一个接口:Serializable

文件操作类:File

File类常用方法

  • public File(String pathname) 构造方法 传递完整的文件操作路径
  • public File(File parent,String child) 构造方法 设置父路径和子文件路径
  • public boolean creatNewFile() throws IOException 普通 创建新文件
  • public boolean exists() 普通方法 判断给定的路径是否存在
  • pblic boolean delete() 普通方法 删除指定路径的文件
  • public File getParentFile() 普通方法 取得当前路径的父路径
  • public boolean mkdirs() 普通方法 创建多级目录
  • public long length() 普通方法 取得文件大小,以字节为单位返回
  • public boolean isFile() 普通方法 判断给定路径是否是文件
  • public boolean isDirectory() 普通方法 判断给定路径是否是目录
  • public long lastModified() 普通方法 取得最后一次修改日期时间
  • public String[] list() 普通方法 取得指定目录的所有内容
  • public File[] listFiles() 普通方法 列出所有路径以File类包含

字节输出流:OutputStream

定义:public abstract class OutputStream extends Object implements Closeable,Flushable

OutputStream类常用方法

  • public void close() throws IOException 普通方法 关闭字节输出流
  • public void flush() throws IOException 普通方法 强制刷新
  • public abstract void write(int b) throws IOException 普通方法 输出单个字节
  • public void write(byte[] b) throws IOException 普通方法 输出全部字节数组数据
  • public void write(byte[] b,int off,int len) throws IOException 普通方法 输出部分字节数组数据

OutputStream是一个抽象类,需要使用FileOutputStream子类完成操作

FileOutputStream常用方法

  • public FileOutputStream(File file) throws FileNotFoundException 构造方法 将内容输出到指定路径,如果文件存在,则覆盖
  • public FileOutputStream(File file,boolean append) throws FileNotFoundException 构造方法 布尔参数设置为true,则表示追加

字节输入流:InputStream

定义:public abstract class OutputStream extends Object implements Closeable

InputStream类常用方法

  • public void close() throws IOException 普通方法 关闭字节输入流
  • public abstract int read() throws IOException 普通方法 读取单个字节
  • public int read(byte[] b) throws IOException 普通方法 将数据读取到字节数组中,同时返回读取长度
  • public int read(byte[] b,int off,int len) throws IOException 普通方法 将数据读取到部分字节数组中,同时返回读取长度

InputStream是一个抽象类,需要使用FileInputStream子类完成操作

FileInputStream常用方法

  • public FileInputStream(File file) throws FileNotFoundException 构造方法 设置要读取文件的路径

字符输出流:Writer

定义:public abstract class Writer extends Object implements Appendable,Closeable,Flushable

利用Writer类可以实现字符数组(包含字符串)的输出

Writer类常用方法

  • public void close() throws IOException 普通方法 关闭字节输出流
  • public void flush() throws IOException 普通方法 强制刷新
  • public Writer append(CharSequence csq) throws IOException 普通方法 追加数据
  • public void write(String str) throws IOException 普通方法 输出字符串数据
  • public void write(char[] cbuf) throws IOException 普通方法 输出字符数组数据

Writer是一个抽象类,需要使用FileWriter子类完成操作

FileWriter常用方法

  • public FileWriter(File file) throws FileNotFoundException 构造方法 设置输出文件
  • public FileWriter(File file,boolean append) throws FileNotFoundException 构造方法 设置输出文件是否以追加方式进行数据追加

字符输出流:Reader

定义:public abstract class Reader extends Object implements Readable,Closeable

Reader类常用方法

  • public void close() throws IOException 普通方法 关闭字节输入流
  • public int read() throws IOException 普通方法 读取单个数据
  • public int read(char[] cbuf) throws IOException 普通方法 读取数据到字符数组,返回读取长度

Reader是一个抽象类,需要使用FileReader子类完成操作

FileWriter常用方法

  • publicFileReader(File file) throws FileNotFoundException 构造方法 设置要读取的文件路径

案例:文件复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package Test;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class testDemo13 {
public static void main(String args[]) throws Exception{
long star=System.currentTimeMillis();
if(args.length!=2)
{
System.out.println("参数输入错误");
System.exit(1);
}
File filein=new File(args[0]);
if(!filein.exists())
{
System.out.println("原文件不存在");
System.exit(1);
}
File fileout=new File(args[1]);
if(!fileout.getParentFile().exists())
{
fileout.getParentFile().mkdirs();
}
InputStream input=new FileInputStream(filein);
OutputStream output=new FileOutputStream(fileout);
int temp=0;
byte data[]=new byte[1024];
while((temp=input.read(data))!=-1)
{
output.write(data,0,temp);
}
input.close();
output.close();
long end=System.currentTimeMillis();
System.out.println("复制所花费的时间:"+(end-star));
}

}
-------------本文结束感谢您的阅读-------------
Donate comment here