文件和文件夹都是File对象;
File类常用方法
getAbsolutePath
public static void main(String[] args) {
// 绝对路径 : getAbsolutePath
File f1 = new File("d:/temp.txt");
System.out.println(f1.getAbsolutePath());
}
public static void main(String[] args) {
File file = new File("d:/temp.txt");
System.out.println("当前文件夹是:"+file);
// 文件是否存在
System.out.println("判断是否存在:"+file.exists());
// 是否是文件夹
System.out.println("判断是否是文件夹:"+file.isDirectory());
// 是否是文件
System.out.println("判断是否是文件:"+file.isFile());
// 文件长度
System.out.println("获取文件的长度:"+file.length());
// 文件最后修改时间
long time = file.lastModified();
Date date = new Date(time);
System.out.println("文件最后的修改时间"+date);
// 设置文件最后的修改时间
file.setLastModified(0);
// 文件重命名
File file1 = new File("D:/temp2.txt");
file.renameTo(file1);
System.out.println("将temp文件名修改为temp2");
}
public static void main(String[] args) throws IOException {
File file = new File("d:/oscar/env");
// 以字符串数组的形式返回当前文件夹下的所有文件 String(不包含文件及其子文件夹)
String[] list = file.list();
// 以文件数组的形式,返回当前文件夹下的所有文件(同上)
File[] files = file.listFiles();
// 以字符串形式返回所在文件夹
String fileParent = file.getParent();
// 以文件形式返回所在文件夹
File parentFile = file.getParentFile();
// 创建文件夹,如果父文件夹oscar不存在,创建就无效
file.mkdir();
// 创建文件夹,如果父文件夹不存在,就会创建父文件夹
file.mkdirs();
// 创建一个空文件,如果父文件夹不存在,就会抛出异常
file.createNewFile();
// 所以创建一个空文件之前,通常会创建父目录
file.getParentFile().mkdirs();
//列出所有的盘符
File[] listRoots = file.listRoots();
for (File root : listRoots) {
System.out.println(root);
}
//删除文件
file.delete();
//JVM结束的时候,删除文件,常用于临时文件的删除
file.deleteOnExit();
}
字节流 FileInputStream & FileOutputStream
public static void main(String[] args) {
File file = new File("d:/temp.txt");
try {
// 创建基于文件的输入流
FileInputStream fileInputStream = new FileInputStream(file);
// 通过这个输入流,就可以把数据从硬盘,读取到Java的虚拟机,也就是读取到内存中
// 输出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
File file = new File("d:/temp.txt");
FileInputStream fileInputStream = new FileInputStream(file);
byte[] all = new byte[(int)file.length()];
fileInputStream.read(all);
for (byte b : all) {
System.out.println(b);
}
fileInputStream.close();
}
public static void main(String[] args) throws IOException {
byte[] data = {88, 89};
File file = new File("d:/temp.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data);
fileOutputStream.close();
}
按自定义大小拆分文件
private static void splitFile(File splitFile, int eachSize) throws IOException {
if (0 == splitFile.length()){
throw new RuntimeException("文件长度为0,不可拆分");
}
// 源文件长度
System.out.println("真实文件长度:"+splitFile.length());
byte[] fileContent = new byte[(int) splitFile.length()];
System.out.println("文件源长度:"+fileContent.length);
try {
FileInputStream fileInputStream = new FileInputStream(splitFile);
fileInputStream.read(fileContent);
} catch (IOException e) {
e.printStackTrace();
}
// 子文件份数
int fileNums;
if (0 == fileContent.length % eachSize){
fileNums = (int) fileContent.length / eachSize;
}else {
fileNums = (int) ((fileContent.length / eachSize )+ 1);
}
System.out.println("拆分数量:"+fileNums);
for (int i = 0; i < fileNums; i++) {
// 拆分后的文件名
String eachFileName = splitFile.getName() + "-" + i;
File eachFile = new File(splitFile.getParent(), eachFileName);
byte[] eachContent;
if (i != fileNums -1){
eachContent = Arrays.copyOfRange(fileContent, eachSize * i, eachSize * (i+1));
}else {
// 拆完剩下的全部装在一起
eachContent = Arrays.copyOfRange(fileContent, eachSize* i, fileContent.length);
}
FileOutputStream fileOutputStream = new FileOutputStream(eachFile);
fileOutputStream.write(eachContent);
fileOutputStream.close();
System.out.printf("输出子文件%s,其文件大小是%d字节%n",eachFile.getAbsolutePath(),eachFile.length());
}
}
合并文件
private static void murgeFile(String folder,String fileName) throws IOException {
File destFile = new File(folder, fileName);
FileOutputStream fileOutputStream = new FileOutputStream(destFile);
int index = 0;
while (true){
File eachFile = new File(folder, fileName + "-" + index++ );
// 如果没找到 则break
if (!eachFile.exists())
break;
FileInputStream fileInputStream = new FileInputStream(eachFile);
byte[] eachContent = new byte[(int) eachFile.length()];
fileInputStream.read(eachContent);
fileInputStream.close();
fileOutputStream.write(eachContent);
fileOutputStream.flush();
System.out.printf("把子文件%s写入到目标文件中%n",eachFile);
}
fileOutputStream.close();
System.out.printf("最后目标文件的大小,%,d字节",destFile.length());
}
关闭流的方式 try(****)
public static void main(String[] args) {
File file = new File("d:/temp.txt");
try (FileInputStream fileInputStream = new FileInputStream(file)){
fileInputStream.read(new byte[(int) file.length()]);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
字符流
public static void read(){
File file = new File("d:/temp.txt");
try (FileReader fileReader = new FileReader(file)){
char[] chars = new char[(int) file.length()];
fileReader.read(chars);
for (char aChar : chars) {
System.out.println(aChar);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write(){
File file = new File("d:/temp.txt");
try (FileWriter fileWriter = new FileWriter(file)){
String data = "i am sorry";
char[] chars = data.toCharArray();
fileWriter.write(chars);
} catch (IOException e) {
e.printStackTrace();
}
}
缓存流
public static void main(String[] args) {
File file = new File("d:/temp.txt");
try (
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
){
while (true){
String line = bufferedReader.readLine();
if (null == line){
break;
}
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
flush
public static void main(String[] args) {
File file = new File("d:/temp.txt");
try (
FileWriter fileWriter = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(fileWriter);
){
printWriter.println("garen kill teemo");
// 强制把缓存中的数据写入硬盘,无论硬盘是否已满
printWriter.flush();
printWriter.println("teemo revive after 1 minutes");
printWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
DataInputStream & DataOutputStream
private static void write() {
File file = new File("d:/temp.txt");
try (
FileOutputStream fileOutputStream = new FileOutputStream(file);
DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
){
dataOutputStream.writeBoolean(true);
dataOutputStream.writeInt(1000);
dataOutputStream.writeUTF("this is green");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void read(){
File file = new File("d:/temp.txt");
try (
FileInputStream fileInputStream = new FileInputStream(file);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
){
boolean readBoolean = dataInputStream.readBoolean();
int i = dataInputStream.readInt();
String s = dataInputStream.readUTF();
System.out.println(readBoolean);
System.out.println(i);
System.out.println(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectInputStream && ObjectOutputStream
public static void main(String[] args) {
Hero hero = new Hero("oscar",23,190);
File file = new File("d:/temp.txt");
try (
FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
){
objectOutputStream.writeObject(hero);
Hero h2 = (Hero) objectInputStream.readObject();
System.out.println(h2.name);
System.out.println(h2.age);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}