一. File类 (java.io.File)
1. 凡是与输入、输出相关的类、接口等都定义在java.io包下
2. File时一个类, 可以有构造器创建其对象. 此对象对应着一个文件(.txt .avi .doc .ppt .mp3 .ipg)或文件目录
3. File类对象时与平台无关的
4. File中的方法, 仅涉及到如何创建、删除、重命名等等. 只要涉及文件内容的, File是无能为力的, 必须由IO流来完成
5. File类的对象常作为IO流的具体类的构造器的形参
路径:
绝对路径: 包括盘符在内的完整的文件路径
相对路径: 在当前文件目录下的文件的路径
File类的方法
1. 访问文件名
getName() 获取文件名字
getPath() 获取文件路径
getAbsoluteFile() 获取绝对文件名
getAbsolutePath() 获取绝对文件路径
getParent() 获取上一层文件目录
renameTo(File newName) 把一个文件改成另一个文件
2. 文件检测(判断文件)
exists() 是否存在
canWrite() 是否可写
canRead() 是否可读
isFile() 是否是一个文件
isDirectory() 是否是一个目录
3. 获取常规文件信息
lastModified() 获取文件的最后修改时间
length() 获取文件的大小
4. 文件操作相关
createNewFile() 创建一个文件
delete() 删除文件或目录
5. 目录操作相关
mkDir() 创建一个文件目录, 只有在上层文件目录存在的起你工况下, 才能返回true
mkDirs() 创建一个文件目录, 若上层文件目录不存在, 一并创建
list() 获取File的文件目录
listFiles() 获取File的文件对象
二. IO流
IO原理:
输入input: 堆区外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中
输出output: 将程序(内存)数据输出到磁盘、光盘等储存设备中
IO流的分类
按操作数据单位分为: 字节流(8bit), 字符流(16bit)
按数据流的流向分为: 输入流, 输出流
按流的角色的分为: 节点流, 处理流
| (抽象基类) | 字节流 | 字符流 |
|---|---|---|
| 输入流 | InputStream | Reader |
| 输出流 | OutputStream | Writer |
1. java的IO流共涉及40多个类, 实际上非常规则, 都是从如上4个抽象基类派生的
2. 由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀
IO流的体系
| 分类 | 字节流 | 字符流 | 字节流 | 字符流 |
|---|---|---|---|---|
| 抽象基类 | InputStream | OutputStream | Reader | Writer |
| 访问文件 | FileInputStream | FileOutputStream | FileReader | FileWriter |
| 访问数组 | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
| 访问管道 | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
| 访问字符串 | StringReader | StringWriter | ||
| 缓冲流 | BufferedIputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
| 转换流 | InputStreamReader | OutputStreamWriter | ||
| 对象流 | ObjectInputStream | ObjectOutputStream | ||
| FilterInputStream | FilterOutputStream | FilterRead | FilterWriter | |
| 打印流 | PrintStream | PrintWriter | ||
| 推回输入流 | PushbackInputStream | PushbackReader | ||
| 特殊流 | DataInputStream | DataOutputStream |
文件流即为为节点流, 缓冲流即为处理流
三. IO流的应用
使用FileInputStream、FileOutputStream完成文件的复制
public void fileCapy(String src, String dest) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File(src));
fos = new FileOutputStream(new File(dest));
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) != -1) {
fos.write(bytes, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用FileReader、 FileWriter完成文本的复制(对于非文本文件, 只能使用字节流)
public void textCapy(String src, String dest) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(new File(src));
fw = new FileWriter(new File(dest));
char[] chars = new char[1024];
int length;
while ((length = fr.read(chars)) != -1) {
fw.write(chars, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输入流对应的文件src一定要存在, 否则FileNotFoundException异常
输出流对应的文件dest可以不存在, 执行过程中会自动创建
四. 处理流一 缓冲流(可以提升文件操作的效率)
使用BufferedInputStream、BufferedOutputStream实现文件的复制
public void bufferedFileCapy(String src, String dest) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File(src)));
bos = new BufferedOutputStream(new FileOutputStream(new File(dest)));
byte[] bytes = new byte[1024];
int length;
while ((length = bis.read(bytes)) != -1) {
bos.write(bytes, 0, length);
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用BufferedReader、BufferedWriter实现文件的复制
public void bufferedTextCapy(String src, String dest) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File(src)));
bw = new BufferedWriter(new FileWriter(new File(dest)));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五. 处理流二 转换流(转换流提供了在字节流和字符流之间的转换)
转换流: InputStreamReader、OutPutStreamWriter
编码: 字符串 -> 字节数组
解码: 字节数组 -> 字符串
使用转换流完成文件的复制
public void transform(String src, String dest) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
InputStreamReader streamReader = new InputStreamReader(new FileInputStream(new File(src)), "UTF-8");
br = new BufferedReader(streamReader);
OutputStreamWriter streamWriter = new OutputStreamWriter(new FileOutputStream(new File(dest)), "UTF-8");
bw = new BufferedWriter(streamWriter);
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null)
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
六. 标准的输入输出流
标准的输入流: System.in 标准的输出流: System.out
public void systemIn() {
BufferedReader br = null;
try {
InputStream in = System.in;
InputStreamReader streamReader = new InputStreamReader(in);
br = new BufferedReader(streamReader);
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
七. 打印流
字节流: PrintStream
字符流: PrintWriter
public void printStream(String fileName) {
PrintStream printStream = null;
try {
printStream = new PrintStream(new FileOutputStream(new File(fileName)), true);
if (printStream != null)
System.setOut(printStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (printStream != null)
printStream.close();
}
}
八. 数据流
为了方便地操作Java语言的基本数据类型的数据, 可以使用数据流
数据输出流
public void dataOutStream(String fileName) {
DataOutputStream outputStream = null;
try {
outputStream = new DataOutputStream(new FileOutputStream(new File(fileName)));
outputStream.writeBoolean(true);
outputStream.writeDouble(10.24);
outputStream.writeUTF(" Hello World");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
数据输入流
public void dataInStream(String fileName) {
DataInputStream inputStream = null;
try {
inputStream = new DataInputStream(new FileInputStream(new File(fileName)));
boolean b = inputStream.readBoolean();
double d = inputStream.readDouble();
String str = inputStream.readUTF();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
九. 对象流(ObjectInputStream、ObjectOutputStream)
用于储存和读取对象的处理流. 他的强大之处就是可以把Java中的对象写入到数据源中, 也能把对象从数据源中还原出来
要实现序列化的类:
1. 要求次类是可序列化的: 实现Serializable接口
2. 要求类的属性同样的要实现Serializable接口
3. 使用static或transient修饰的属性, 不可实现序列化
4. 提供一个版本号
class Person implements Serializable {
private static final Long serialVersionUID = 67348253671L;
private String name;
private Integer age;
private Dog dog;
public Person(String name, Integer age, Dog dog) {
this.name = name;
this.age = age;
this.dog = dog;
}
}
class Dog implements Serializable{
private String name;
public Dog(String name) {
this.name = name;
}
}
ObjectOutputStream
public void objectOutStream(String fileName) {
Person person = new Person("小个子", 19, new Dog("小黑"));
Person person1 = new Person("小兄弟", 20, new Dog("小白"));
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File(fileName)));
oos.writeObject(person);
oos.flush();
oos.writeObject(person);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ObjectInputStream
public void objectInStream(String fileName) {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(new File(fileName)));
Person o = (Person) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
十. RandomAccessFile类(支持随机访问)
1. 既可以充当一个输入流, 也可以冲淡一个输出流
2. 支持从文件的开头读取、写入
3. 支持从任意位置的读取、写入(插入)
4. RandomAccessFile类需要指定的访问模式:
① r: 以只读方式打开
② rw: 打开以便读取和写入
③ rwd: 打开以便读取和写入; 同步文件内容的更新
④ rws: 打开以便读取和写入; 同步文件内容和元数据的更新
RandomAccessFile类 完成文件的复制
public void RandomAccessFile(String src, String srcMode, String dest, String destMode) {
RandomAccessFile accessFile = null;
RandomAccessFile accessFile1 = null;
try {
accessFile = new RandomAccessFile(new File(src), srcMode);
accessFile = new RandomAccessFile(new File(dest), destMode);
byte[] bytes = new byte[1024];
int length;
while ((length = accessFile.read(bytes)) != -1) {
accessFile1.write(bytes, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (accessFile != null)
try {
accessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
if (accessFile1 != null) {
try {
accessFile1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
RandomAccessFile类 完成覆盖、修改
RandomAccessFile类的方法:
accessFile.readLine(); // 读取指针之后的内容
accessFile.getFilePointer(); // 获取指针位置
accessFile.seek(4); // 定位当前指针位置
public void randomAccessFileSet(String fileName, String mode) {
RandomAccessFile accessFile = null;
try {
accessFile = new RandomAccessFile(new File(fileName), mode);
accessFile.seek(4);
accessFile.write("sd".getBytes());
// 从指针seek开始用写入的内容进行修改
} catch (IOException e) {
e.printStackTrace();
} finally {
if (accessFile != null)
try {
accessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
RandomAccessFile类 完成插入
public void randomAccessFileAdd(String fileName, String mode) {
RandomAccessFile accessFile = null;
try {
accessFile = new RandomAccessFile(new File(fileName), mode);
accessFile.seek(4);
byte [] bytes = new byte[1024];
int length;
StringBuffer buffer = new StringBuffer();
while ((length = accessFile.read(bytes))!= -1){
buffer.append(new String(bytes, 0, length));
}
accessFile.seek(4);
accessFile.write(" 插入的内容".getBytes());
accessFile.write(buffer.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (accessFile != null)
try {
accessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}