文章目录
File类的使用
File类的一个对象,代表一个文件或一个文件目录
File类声明在java.io包下
如何创建File类的实例
* File(String filePath)
* File(String parentPath, String childPath)
* File(File parentFile, String childPath)
//构造器1
File file = new File("test1.txt");
File file1 = new File("D:\\IDEA_Document_code\\OnlineClass\\IO流\\src\\File类的使用\\test1.txt");
System.out.println(file);
//构造器2
File file2 = new File("D:\\IDEA_Document_code","OnlineClass");
常用方法
* String getAbsolutePath():获取绝对路径
* String getPath():获取路径
* String getName():获取名称
* String getParent():获取上一层文件目录
* long length():获取文件长度(字节数)
* long lastModified():获取最后一次修改的时间,毫秒数
* String[] list():获取指定目录下的所有文件或者文件目录的数组名称
* File[] listFiles():获取指定目录下的所有文件或者文件目录的File数组
*
* File类的判断功能,返回Boolean
* isDirectory():判断是否是文件目录
* isFile():判断是否是文件
* exists():判断文件是否存在
* canRead():判断是否可读
* canWrite():判断是否可写
* isHidden():判断是否隐藏
*
* 创建文件:
* boolean createNewFile():创建文件,若存在则不创建,返回false
* boolean mkdir():创建文件目录,若上层目录不存在则不创建
* boolean mkdirs():创建文件目录,如果上层文件目录不存在则一起创建
*
* 删除文件:boolean delete():删除文件或文件夹
IO流
流的分类:
- 操作数据单位:字节流:处理非文本文件,字符流:处理文本文件
- 数据流向:输入流,输出流
- 流的特色:节点流,处理流
流的体系结构:
抽象类 | 节点流 | 缓冲流 |
---|---|---|
InputStream | FileInputStream | BufferedInputStream |
OutputStream | FileOutputSteam | BufferedOutputStream |
Reader | FileReader | BufferedReader |
Writer | FileWriter | BufferedWriter |
FileReader & FileWriter
将test1.txt文件内容读入程序,输出到控制台
read():返回读入的一个字符,如果到达文件末尾,返回-1
@Test
public void testFileReader() {
FileReader fileReader = null;
try {
File file = new File("test1.txt");
fileReader = new FileReader(file);
int len = 0;
char[] cbuf = new char[1024];
while ((len = fileReader.read(cbuf)) != -1) {
//方式一:
for (int i = 0; i < len; i++) {
System.out.print(cbuf[i]);
}
/*方式二:
* String str = new String(cubf, 0, len);
* System.out.print(str);
* */
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
写数据到文件中,如果File不存在,则自动创建FileWriter(file,false)/FileWriter(file):会对原有的文件内容进行覆盖 FileWriter(file, true):不会覆盖原有的文件
@Test
public void testFileWriter() {
FileWriter fw = null;
try {
File file = new File("test2.txt");
fw = new FileWriter(file);
fw.write("aaaaa");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件复制操作,结合上面读写两步操作
public void testFileReaderFileWriter(){
FileReader fr = null;
FileWriter fw = null;
try {
File file = new File("test1.txt");
File copyFile = new File("test2.txt");
fr = new FileReader(file);
fw = new FileWriter(copyFile);
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream & FileOutputStream
读取文件:
public void testFileInputStream(){
FileInputStream fis = null;
try {
File file = new File("test1.txt");
fis = new FileInputStream(file);
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer)) != -1){
String str = new String(buffer, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
复制文件:
public void testFileInputOutputStream(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File file = new File("test2.txt");
File copyfile = new File("test4.txt");
fis = new FileInputStream(file);
fos = new FileOutputStream(copyfile);
byte[] b = new byte[5];
int len;
while ((len = fis.read(b)) != -1){
fos.write(b, 0, len);
}
} catch (IOException e){
e.printStackTrace();
} finally {
try {
if (fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲流
缓冲流的使用:
* BufferedInputStream
* BufferedOutputStream
* BufferedReader
* BufferedWriter
* flush():清除内存中的缓冲内存
* 作用:提升读取写入的速度
* 先关闭外层的流,再关闭内层的流,但是关闭外层流时,内层流也会自动关闭
非文本文件的复制:
public void BufferedStreamTest(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File file = new File("test1.txt");
File copyFile = new File("text5");
//节点流
fis = new FileInputStream(file);
fos = new FileOutputStream(copyFile);
//缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1){
bos.write(b, 0, len);
}
} catch (IOException e){
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
文本文件的复制:
public void test1(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("test1.txt")));
bw = new BufferedWriter(new FileWriter(new File("test6.txt")));
//读写操作
//方式一:使用char[]数组
char[] cubf = new char[1024];
int len;
while ((len = br.read(cubf)) != -1){
bw.write(cubf, 0, len);
bw.flush();
}
/*方式二:使用String
* String data;
* while((data = br.readLine() != null){
* 方法一:bw.write(data + "\n"); //不包含换行
* 方法二:bw.write(data);
* bw.newLine();
* }
* */
} catch (IOException e){
e.printStackTrace();
}
}
对象流 ObjectInputStream 和 ObjectOutputStream
作用:用于储存的读取基本数据类型数据或对象的处理流,可以把java中的对象写入到数据中心,也能把对象从数据中还原回来
序列化类的实现条件:需要实现接口:Serializable
当前类提供一个全局常量:serialVersionUID
除了当前Person类需要实现Serializable接口之外,还必须保证内部所有属性也可序列化
序列化过程:将内存中的java对象保存到磁盘中或通过网络传出去
使用ObjectOutputStream
@Test
public void test(){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
String str = new String("hello world");
oos.writeObject(str);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
反序列化:将磁盘文件中的对象还原为内存中的一个java对象
public void test2(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String) obj;
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
}
}
转换流 InputStreamReader & OutputSteamWriter
转换流的使用:属于字符流
InputStreamReader:将字节的输入流转换为字符的输入流
OutputSteamWriter:将字符的输出流转换为字节的输出流
字节流转换成字符流输出:
public void test(){
FileInputStream fis = null;
InputStreamReader isr = null;
try {
fis = new FileInputStream("test1.txt");
isr = new InputStreamReader(fis,"UTF-8");
char[] cubf = new char[1024];
int len;
while ((len = isr.read(cubf)) != -1){
String str = new String(cubf, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
读写合一,转换文件存储方式:
public void test1(){
FileInputStream fis = null;
FileOutputStream fos = null;
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File file1 = new File("test1.txt");
File file2 = new File("test8.txt");
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis,"UTF-8");
osw = new OutputStreamWriter(fos, "gbk");
char[] cubf = new char[1024];
int len;
while ((len = isr.read(cubf)) != -1){
osw.write(cubf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
随机读取文件流 RandomAccessFile
- 1.RandomAccessFile直接继承于Object类,实现了DataInput和DataOutput接口
- 2.RandomAccessFile既可以作为输入流又可以作为输出流
- 3.作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建,如果文件存在则对原文件进行覆盖
- 4.seek():指定写入的位置
public class RandomAccessFileTest {
@Test
public void test(){
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
raf1 = new RandomAccessFile(new File("test1.txt"),"r");
raf1 = new RandomAccessFile(new File("test11.txt"),"rw");
byte[] b = new byte[1024];
int len;
while ((len = raf1.read(b)) != -1){
raf2.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (raf1 != null){
try {
raf1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (raf2 != null){
try {
raf2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test //使用RandomAccessFile实现数据的插入效果
public void test1(){
RandomAccessFile raf1 = null;
try {
raf1 = new RandomAccessFile("hello.txt","rw");
raf1.seek(3); //将指针调到角标为3的位置
//保存指针3后面的所有数据到StringBuilder中
StringBuffer sb = new StringBuffer((int) new File("hello.txt").length());
byte[] b = new byte[1024];
int len;
while ((len = raf1.read(b)) != -1){
sb.append(new String(b, 0, len));
}
//调回指针,写入"xyz"
raf1.seek(3);
//将StringBuilder中的数据写入到文件中
raf1.write("xyz".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (raf1 != null){
try {
raf1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
其它流
- 其它流的使用:
- 1.标准的输入、输出流:
-
1.1 System.in:标准输入流,默认从键盘输入 System.out:标准输出流,默认从控制台输出
-
1.2 System类的setIn(InputStream is)/setOut
- 2.打印流:PrintStream和PrintWriter
提供了一系列重载的print()和println() - 3.数据流:DataInputStream和DataOutputStream
作用:用于读取或写出基本数据类型的变量或字符串
读取数据的顺序要与写入数据的顺序相同