文件
1. 文件的一些常用方法
package com.ljw.study;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
public class FileDemo {
public static void main(String[] args) {
File f1 = new File("d:/work/code/ljw/Test");
System.out.println(f1.exists());
File f2 = new File("employee.ser");
System.out.println("f2的绝对路径:" + f2.getAbsolutePath());
System.out.println(f2.exists());
File f3 = new File(f1,"LOL.exe");
System.out.println("f3的绝对路径:" + f3.getAbsolutePath());
System.out.println(f2.isDirectory());
System.out.println(f2.isFile());
System.out.println(f2.length());
System.out.println(new Date(f2.lastModified()));
System.out.println(Arrays.toString(f1.list()));
System.out.println(Arrays.toString(f1.listFiles()));
System.out.println(f1.getParent());
f2.getParentFile();
f1.mkdir();
f1.mkdirs();
f1.createNewFile();
f1.getParentFile().mkdirs();
f1.listRoots();
f.delete();
f.deleteOnExit()
}
}
2. 读取和写入文件数据
2.1 以字节流方式
2.1.1 读取数据
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class TestStream {
public static void main(String[] args) {
try {
File f =new File("d:/lol.txt");
FileInputStream fis =new FileInputStream(f);
byte[] all =new byte[(int) f.length()];
fis.read(all);
for (byte b : all) {
System.out.println(b);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.1.2 写入数据
package com.ljw.practice;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File f = new File("d:/xyz/abc/def/lol2.txt");
f.getParentFile().mkdirs();
try {
FileOutputStream outputFile = new FileOutputStream(f);
String s = "秋天,多了一些思念";
byte[] b = s.getBytes();
outputFile.write(b);
outputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.2 以字符流方式
2.2.1 读取数据
package com.ljw.practice;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File f = new File("d:/lol.txt");
try(FileReader fr = new FileReader(f)){
char[] c = new char[(int) f.length()];
fr.read(c);
for(char i:c) {
System.out.print(i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.2.2 写入数据
package com.ljw.practice;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File f = new File("d:/lol.txt");
File f2 = new File("d:/lol2.txt");
try(FileReader fr = new FileReader(f);FileWriter fw = new FileWriter(f2)){
char[] c = new char[(int) f.length()];
fr.read(c);
fw.write(c);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.3 以缓存流方式
- 缓存流必须建立在一个存在的流的基础上
- 以介质是硬盘为例,字节流和字符流的弊端:在每一次读写的时候,都会访问硬盘。 如果读写的频率比较高的时候,其性能表现不佳。
- 为了解决以上弊端,采用缓存流。缓存流在读取的时候,会一次性读较多的数据到缓存中,以后每一次的读取,都是在缓存中访问,直到缓存中的数据读取完毕,再到硬盘中读取。
- 如果需要立即将缓存中的数据写入硬盘,可以使用
flush()
方法
2.3.1 读取数据
package stream;
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) {
File f = new File("d:/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) {
e.printStackTrace();
}
}
}
2.3.2 写入数据
package com.ljw.practice;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FileDemo {
public static void main(String[] args) {
File f = new File("d:/lol2.txt");
try (
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
) {
pw.println("garen kill teemo");
pw.println("teemo revive after 1 minutes");
pw.println("teemo try to garen, but killed again");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.4 以数据流方式
- 使用数据流的writeUTF()和readUTF() 可以进行数据的格式化顺序读写
- 要用DataInputStream 读取一个文件,这个文件必须是由DataOutputStream 写出的,否则会出现EOFException,因为DataOutputStream 在写出的时候会做一些特殊标记,只有DataInputStream 才能成功的读取。
2.4.1 实例
package com.ljw.practice;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
write();
read();
}
private static void read() {
File f = new File("d:/lol2.txt");
try(
FileInputStream fi = new FileInputStream(f);
DataInputStream di = new DataInputStream(fi);
)
{
boolean b = di.readBoolean();
int i = di.readInt();
String s = di.readUTF();
System.out.println(b);
System.out.println(i);
System.out.println(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private static void write() {
File f = new File("d:/lol2.txt");
try(
FileOutputStream fo = new FileOutputStream(f);
DataOutputStream dos = new DataOutputStream(fo);
)
{
dos.writeBoolean(false);
dos.writeInt(200);
dos.writeUTF("中国");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.5 以对象流(序列化对象)
- 把一个对象序列化有一个前提是:这个对象的类,必须实现了Serializable接口
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import charactor.Hero;
public class TestStream {
public static void main(String[] args) {
Hero h = new Hero();
h.name = "garen";
h.hp = 616;
File f =new File("d:/garen.lol");
try(
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos =new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois =new ObjectInputStream(fis);
) {
oos.writeObject(h);
Hero h2 = (Hero) ois.readObject();
System.out.println(h2.name);
System.out.println(h2.hp);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
3. 拆分文件 和 合并文件
3.1 拆分文件
package com.ljw.practice;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File file = new File("d:/F.docx");
try {
FileInputStream inFile = new FileInputStream(file);
int i = 1;
while(true) {
byte[] b = new byte[1024*100];
int n = inFile.read(b);
if(n==-1) {
break;
}
File f1 = new File(String.format("d:/F-%d.docx",i));
FileOutputStream outFile = new FileOutputStream(f1);
outFile.write(b);
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.2 合并文件
package com.ljw.practice;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File newF = new File("d:/newF.docx");
try {
FileOutputStream outNewF = new FileOutputStream(newF);
for(int i=1;i<5;i++) {
File f = new File(String.format("d:/F-%d.docx", i));
byte[] b = new byte[(int) f.length()];
FileInputStream inf = new FileInputStream(f);
inf.read(b);
outNewF.write(b);
inf.close();
}
outNewF.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. 中文问题
package com.ljw.practice;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File f = new File("d:/lol.txt");
try (FileInputStream fis = new FileInputStream(f);) {
byte[] all = new byte[(int) f.length()];
fis.read(all);
System.out.println("文件中读出来的数据是:");
for (byte b : all)
{
int i = b&0xff;
System.out.println(Integer.toHexString(i));
}
System.out.println("把这个数字,放在GBK的棋盘上去:");
String str = new String(all,"GBK");
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
综合练习
package com.ljw.practice;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class FilePractice {
public static void main(String[] args) {
File srcFile = new File("d:/lol.txt");
File destFile = new File("d:/lol3.txt");
copyFile(srcFile,destFile);
File srcFolder = new File("d:/xyz");
File destFolder = new File("d:/xyz-副本");
copyFolder(srcFolder,destFolder);
String s = "hello";
findFileContent(s, srcFolder);
}
public static void copyFile(File srcFile, File destFile) {
if(srcFile.exists()) {
try(
FileInputStream fi = new FileInputStream(srcFile);
FileOutputStream fo = new FileOutputStream(destFile);
)
{
byte[] b = new byte[10];
int i;
while(true) {
i = fi.read(b);
if(-1==i) {
break;
}
fo.write(b,0,i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("文件不存在");
}
}
public static void copyFolder(File srcFile, File destFile) {
if(srcFile.isDirectory()) {
File[] files = srcFile.listFiles();
destFile.mkdir();
for(File f:files) {
if(f.isDirectory()) {
String newFolderPath = destFile.getAbsolutePath()+"\\"+f.getName();
File newFolder = new File(newFolderPath);
newFolder.mkdir();
System.out.println("新文件夹的位置是:"+newFolderPath);
copyFolder(f,newFolder);
}else {
String newFilePath = destFile.getAbsolutePath()+"\\"+f.getName();
System.out.println("新文件的位置是:"+newFilePath);
File newF = new File(newFilePath);
copyFile(f,newF);
}
}
}else {
copyFile(srcFile, destFile);
}
}
public static void findFileContent(String s,File file) {
if(file.isDirectory()) {
File[] files = file.listFiles();
for(File f:files) {
findFileContent(s,f);
}
}else {
try(
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
) {
while(true) {
String str = br.readLine();
if(null == str) {break;}
if(str.contains(s)) {
System.out.println("找到目标字符串"+s+",在文件:"+file.getAbsolutePath());
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
打开App,阅读手记