File 类
File 类专门针对文件操作的一个类
常用的构造方法
| File 类常用的构造方法 | |
|---|---|
| File(String pathname) | 以pathname为路径创建File对象,可以是绝对路径或者相对路径 |
| File(String parent,String child) | 以parent为父路径,child为子路径创建File对象 |
| public File(File parent,String child) | 根据一个父File对象和子文件路径创建File对象 |
常用API
| 常用API | |
|---|---|
| public String getAbsolutePath() | 获取绝对路径 |
| public String getPath() | 获取路径 |
| public String getName() | 获取文件名称 |
| public String getParent() | 获取上层路径的名称,没有返回null |
| public long length() | 获取文件的长度字节数 |
| public long lastModified() | 文件最后依次修改的时间,毫秒数 |
| public String[] list() | 获取指定目录下的所有文件或者文件目录的名称数组 |
| public File[] listFiles() | 获取指定目录下的所有文件或者文件目录的File数组 |
| public boolean renameTo(File dest) | 把文件重命名为指定的文件路径 |
| public boolean isDirectory() | 判断是否是目录 |
| public boolean isFile() | 判断是否是文件 |
| public boolean exists() | 判断文件是否存在 |
| public boolean canRead() | 是否可读 |
| public boolean canWrite() | 是否可以写 |
| public boolean isHidden() | 是否可以隐藏 |
| public boolean createNewFile() | 创建文件。若文件存在,则不创建,返回false |
| public boolean mkdir() | 创建文件目录。如果此文件目录存在,就不创建了。 如果此文件目录的上层目录不存在,也不创建。 |
| public boolean mkdirs() | 创建文件目录。如果上层文件目录不存在,一并创建 |
| public boolean delete() | 删除文件或者文件夹 |
-
注意
-
Java中的删除不走回收站。
-
要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录
-
如果你创建文件或者文件目录没有写盘符路径,那么,默认在项目 路径下。
-
IO 流的分类
-
IO 流的分类可以从三个角度来划分
-
按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
-
按数据流的流向不同分为:输入流,输出流
-
按流的角色的不同分为:节点流,处理流
-
IO流体系图
输入流 InputStream 和 Reader
InputStream 和 Reader 作为输入流的一个基类
InputStream
| 常用API | |
|---|---|
| int read() | 从输入流中读取数据的下一个字节。返回 0 到 255 范围内的 int 字节值。如果因 为已经到达流末尾而没有可用的字节,则返回值 -1。 |
| int read(byte[] b) | 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。如果因为已 经到达流末尾而没有可用的字节,则返回值 -1。否则以整数形式返回实际读取 的字节数。 |
| int read(byte[] b, int off,int len) | 将输入流中最多 len 个数据字节读入 byte 数组。尝试读取 len 个字节,但读取 的字节也可能小于该值。以整数形式返回实际读取的字节数。如果因为流位于 文件末尾而没有可用的字节,则返回值 -1。 |
Reader
| 常用API | |
|---|---|
| int read() | 读取单个字符。作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff)(2个 字节的Unicode码),如果已到达流的末尾,则返回 -1 |
| int read(byte[] b) | 将字符读入数组。如果已到达流的末尾,则返回 -1。否则返回本次读取的字符数。 |
| int read(byte[] b, int off,int len) | 将字符读入数组的某一部分。存到数组cbuf中,从off处开始存储,最多读len个字 符。如果已到达流的末尾,则返回 -1。否则返回本次读取的字符数。 |
输出流 OutputStream 和 Writer
OutputStream 和 Writer 作为输出流的基类
OutputStream
| 常用API | |
|---|---|
| void write(int b) | 将指定的字节写入此输出流。write 的常规协定是:向输出流写入一个字节。要写 入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。 即写入0~255范围的。 |
| void write(byte[] b) | 将 b.length 个字节从指定的 byte 数组写入此输出流。write(b) 的常规协定是:应该 与调用 write(b, 0, b.length) 的效果完全相同。 |
| void write(byte[] b,int off,int len) | 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 |
| public void flush()throws IOException | 刷新此输出流并强制写出所有缓冲的输出字节,调用此方法指示应将这些字节立 即写入它们预期的目标。 |
| public void close() throws IOException | 关闭此输出流并释放与该流关联的所有系统资源。 |
Writer
| 常用API | |
|---|---|
| void write(int c) | 写入单个字符。要写入的字符包含在给定整数值的 16 个低位中,16 高位被忽略。 即 写入0 到 65535 之间的Unicode码 |
| void write(char[] cbuf) | 写入字符数组。 |
| void write(char[] cbuf,int off,int len) | 写入字符数组的某一部分。从off开始,写入len个字 |
| void write(String str) | 写入字符串。 |
| void write(String str,int off,int len) | 写入字符串的某一部分。 |
| void flush() | 刷新该流的缓冲,则立即将它们写入预期目标。 |
| public void close() throws IOException | 关闭此输出流并释放与该流关联的所有系统资源。 |
- 案例演示1 : 读取文件
@Test
public void Test3() throws IOException {
FileReader fileReader = new FileReader(new File("hello.txt"));
int data;
while((data = fileReader.read())!= -1){
System.out.print((char)data);
}
fileReader.close();
}
- 案例演示2 : 文件写入
@Test
public void Test4() throws IOException {
FileWriter fileWriter = new FileWriter(new File("write.txt"));
fileWriter.write("牛小牛牛");
fileWriter.close();
}
-
注意点
-
定义文件路径时,注意:可以用“/”或者“\”。
-
在写入一个文件时,如果使用构造器FileOutputStream(file),则目录下有同名文 件将被覆盖。
-
如果使用构造器FileOutputStream(file,true),则目录下的同名文件不会被覆盖, 在文件内容末尾追加内容。
-
在读取文件时,必须保证该文件已存在,否则报异常。
-
字节流操作字节,比如:.mp3,.avi,.rmvb,mp4,.jpg,.doc,.ppt
-
字符流操作字符,只能操作普通文本文件。最常见的文本文 件:.txt,.java,.c,.cpp 等语言的源代码。尤其注意.doc,excel,ppt这些不是文 本文件。
-
缓冲流
为什么要使用缓冲流呢?其主要的目的还是为了提高读写的速度,它的内部会有一个缓冲区默认大小是8K
缓冲流的使用要套接在相应的节点流之上
案例演示:使用缓冲流将一个视频或者i一个图片进行读写
@Test
public void Test5() throws IOException {
File fi = new File("D:\我的文档\Java基础入门(第二版).pdf");
File fw = new File("D:\我的文档\Java基础入门(第22版).pdf");
BufferedInputStream bsBuff = null;
BufferedOutputStream boBuff = null;
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(fi);
os = new FileOutputStream(fw);
bsBuff = new BufferedInputStream(is);
boBuff = new BufferedOutputStream(os);
int data;
while((data = bsBuff.read())!=-1){
boBuff.write(data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
bsBuff.close();
boBuff.close();
is.close();
os.close();
}
}
注意:要注意流的关闭顺序,应该是先关闭外层的流,然后在关闭内层的流。
转换流
转换流提供了在字节流和字符流之间的转换
可以处理两个文件编码格式不一致的问题
InputStreamReader
-
构造器
- public InputStreamReader(InputStream in)
- public InputSreamReader(InputStream in,String charsetName)
OutputStreamWriter
-
构造器
-
public OutputStreamWriter(OutputStream out)
-
public OutputSreamWriter(OutputStream out,String charsetName)
-
案例演示:
@Test
public void testMyInput() throws Exception {
FileInputStream fis = new FileInputStream("dbcp.txt");
FileOutputStream fos = new FileOutputStream("dbcp5.txt");
InputStreamReader isr = new InputStreamReader(fis, "GBK");
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
BufferedReader br = new BufferedReader(isr);
BufferedWriter bw = new BufferedWriter(osw);
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
打印流 PrintStream 和 PrintWriter
实现将基本数据类型的数据格式转化为字符串输出
案例演示:重新指定输出的位置
@Test
public void printStream(){
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("D:\IO\text.txt"));
// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
ps = new PrintStream(fos, true);
if (ps != null) {// 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 输出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { // 每50个数据一行
System.out.println(); // 换行
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
}
}
}
对象流 ObjectInputStream 和 OjbectOutputSteam
-
用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可 以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
-
序列化:用ObjectOutputStream类保存基本类型数据或对象的机制
-
反序列化:用ObjectInputStream类读取基本类型数据或对象的机制
-
ObjectOutputStream和ObjectInputStream不能序列化static和transient修 饰的成员变量
对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从 而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传 输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原 来的Java对象
演示案例:序列化和反序列化
- 序列化
@Test
public void test() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data1.txt"));
Person p = new Person("韩梅梅", 18, "中华大街");
oos.writeObject(p);
oos.flush();
oos.close();
}
- 反序列化
@Test
public void test02() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data1.txt"));
Person p1 = (Person)ois.readObject();
System.out.println(p1.toString());
ois.close();
}