一、File类的应用
- 什么是文件: 文件可认为是相关记录或放在一起的数据集合
- 在Java在中,使用java.io.File类对文件进行操作
package com.csdn.file;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File file1 = new File("/Users/***/Desktop/study", "test.txt");
// 判断目录文件
System.out.println("file1 是否为目录: " + file1.isDirectory());
System.out.println("file1 是否为文件: " + file1.isFile());
// 创建目录
File file2 = new File("/Users/***/Desktop/study", "test");
if (!file2.exists()) {
file2.mkdir();
}
// 创建多层级目录
File file3 = new File("/Users/***/Desktop/study", "test/test");
if (!file3.exists()) {
file3.mkdirs();
}
// 创建文件
try {
file1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 绝对路径和相对路径
- 绝对路径:是从盘符开始的路径
- 相对路径:是从当前路径开始的路径
package com.csdn.file;
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
public static void main(String[] args) {
// 相对路径和绝对路径
File file1 = new File("thread.txt");
try {
file1.createNewFile();
// 判断是否为绝对路径
System.out.println(file1.isAbsolute());
// 获取相对路径
System.out.println(file1.getPath());
// 获取绝对路径
System.out.println(file1.getAbsolutePath());
// 获取文件名
System.out.println(file1.getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
二、字节流
1、字节输入流InputStream
- FileInputStream
- 从文件系统中的某个文件中获得输入字节
- 用于读取诸如图像数据之类的原始字节流
- FileInputStream常用方法
数据读取Demo1
package com.csdn.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileDemo3 {
public static void main(String[] args) {
try {
FileInputStream f = new FileInputStream("speech.txt");
int count = 0;
int n;
while ((n = f.read()) != -1) {
System.out.print((char) n);
count ++;
}
System.out.println();
System.out.print("一共有" + count + "字节");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
数据读取Demo2
package com.csdn.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileDemo4 {
public static void main(String[] args) {
try {
FileInputStream f = new FileInputStream("speech.txt");
byte[] b = new byte[100];
f.read(b);
System.out.print(new String(b));
System.out.println();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、字节输出流OutputStream
package com.csdn.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputDemo {
public static void main(String[] args) {
FileOutputStream fos;
FileInputStream fis;
try {
fos = new FileOutputStream("speech.txt");
fis = new FileInputStream("speech.txt");
fos.write(50);
fos.write('a');
System.out.println(fis.read());
System.out.println((char)fis.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、缓冲流
- 缓冲输入流 BufferedInputStream
- 缓冲输出流 BufferedOutputStream
package com.csdn.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedDemo {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("speech.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("speech.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(50);
bos.write('a');
// 清空缓冲流 写入文件
bos.flush();
System.out.println(bis.read());
System.out.println((char)bis.read());
// 关闭文件
bos.close();
fos.close();
fis.close();
bis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
缓冲流与普通写入对比, 时间相差60倍, 缓冲流更快
package com.csdn.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedDemo2 {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("one.txt");
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
fos.write(i);
}
long endTime = System.currentTimeMillis();
System.out.println("FileOutputStream 耗时: " + (endTime - startTime));
FileOutputStream fos2 = new FileOutputStream("two.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos2);
long startTime2 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
bos.write(i);
}
bos.flush();
long endTime2 = System.currentTimeMillis();
System.out.println("BufferedOutputStream 耗时: " + (endTime2 - startTime2));
fos.close();
fos2.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
三、字符流
1、字符输入流Reader
2、字符输出流Writer
3、字节字符转换流
- InputStremReader
- OutputStreamWriter
案例:
package com.csdn.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ReaderDemo {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("speech.txt");
InputStreamReader isr = new InputStreamReader(fis);
FileOutputStream fos = new FileOutputStream("speech1.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
int n = 0;
char[] cbuf = new char[10];
while((n=isr.read(cbuf)) != -1) {
// String s = new String(cbuf, 0 ,n);
osw.write(cbuf, 0, n);
osw.flush();
}
fis.close();
isr.close();
fos.close();
osw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
四、对象序列化与反序列化
1、步骤
- 创建一个类,继承Serializable接口
- 创建对象
- 将对象写入文件
- 从文件读取对象信息
2、类
- 对象输入流ObjectInputStream
- 对象输出流ObjectOutputStream
3、对象序列化
- 序列化: 把Java对象转换成字节序列的过程
- 反序列化:把字节序列回复为Java对象的过程
?
?
??坐得住板凳,耐得住寂寞,守得住初心!??
|