1.文件对象
文件和文件夹都用File表示。创建一个文件对象使用File file = new File("文件路径");文件常用方法很多,列举部分如下:
package com.zqj.io;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestFile {
public static void main(String[] args) {
File file = new File("D:/demo/lol.txt");
File file2 = new File("D:/demo/cf.txt");
System.out.println(""+file.exists());
System.out.println(""+file.isDirectory());
System.out.println(""+file.getName());
System.out.println(""+file.length());
System.out.println(""+file.getAbsolutePath());
System.out.println(""+file.toString());
System.out.println(file.getAbsoluteFile().equals(file.toString()));
System.out.println(""+file.compareTo(file2));
Long time = file.lastModified();
System.out.println(time);
Date date = new Date(time);
System.out.println(date);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str = sf.format(date);
System.out.println(str);
}
2.流的概念
当不同介质之间有数据交换时就是用流来实现。流包括字节流和字符流。
3.字节流
字节输入流inputStream,字节输出流outputStream;输入流是指外部到内存,输出流指内存到外部。
流的执行过程分为:
1.创建文件对象;2.创建基于文件的输入/输出流;3.执行流;4.关闭流
字节输入流FileInputStream
package com.zqj.io;
import java.io.*;
public class TestStream {
public static void main(String[] args) {
//准备文件lol.txt其中的内容是AB,对应的ASCII分别是65 66
File f =new File("d:/demo/lol.txt");
//创建基于文件的输入流
FileInputStream fis = null;
try {
System.out.println(f.length());
fis = new FileInputStream(f);
//创建字节数组
byte[] all =new byte[2];//每次读取两个字节
int len = 0;
//以字节流的形式读取文件所有内容
while((len = fis.read(all)) != -1){//两个字节一读取,读取完=-1
for (byte b : all) {
//打印出来是65 66
System.out.println(b);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//每次使用完流,都应该进行关闭
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
byte[] bytes = {88,89};
fos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
标准的关闭流的方式
- 首先把流的引用声明在try的外面,如果声明在try里面,其作用域无法抵达finally.
- 在finally关闭之前,要先判断该引用是否为空
- 关闭的时候,需要再一次进行try catch处理
4.字符流
字符输入流Reader,字符输出流Writer。字符流读取的是字符
package com.zqj.io;
import java.io.*;
public class TestStream {
public static void main(String[] args) {
//字符输出流
File file = new File("d:/demo/cf.txt");
// 创建基于文件的Writer
FileWriter fw = null;
try{
fw = new FileWriter(file);
// 以字符流的形式把数据写入到文件中---->数据从内存到磁盘,为输出流
String data="abcdefg1234567890";
char[] cs = data.toCharArray();//字符数组
fw.write(cs);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//字符输入流
//创建基于文件的reader
FileReader fr = null;
try {
fr = new FileReader(file);
// 创建字符数组,其长度就是文件的长度
char[] chars = new char[(int)file.length()];
fr.read(chars);
for(char ch : chars){
System.out.print(ch);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
5.缓存流
缓存字符输入流 BufferedReader 可以一次读取一行数据
package com.zqj.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class TestStream {
public static void main(String[] args) {
// 准备文件lol.txt其中的内容是
// garen kill teemo
// teemo revive after 1 minutes
// teemo try to garen, but killed again
File f = new File("d:/demo/lol.txt");
// 创建文件字符流
// 缓存流必须建立在一个存在的流的基础上
try (FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);) {
while (true) {
// 一次读一行
String line = br.readLine();
if (null == line)
break;
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
PrintWriter 缓存字符输出流, 可以一次写出一行数据
try(FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);)
{
pw.print("aaaaaaa");
pw.print("bbbbbbbbbbb");
} catch (IOException e) {
e.printStackTrace();
}