实例1:
删除指定目录下的所有空文件
import java.io.File;
public class DelDri {
public static void main(String[] args) {
File file = new File("d:/IO");
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
File[] file3 = file2.listFiles();
if (file3.length == 0) {
file2.delete();
}
}
}
}
}
实例2:
删除指定目录下的所有空文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class DelFile {
public static void main(String[] args) {
File file = new File("D:/IO/");
file.delete();
File[] file1 = file.listFiles();
for (File file2 : file1) {
if (file2.isDirectory()) {
System.out.println("这是个文件夹");
} else {
if (file2.length() == 0) {
file2.delete();
}
}
}
}
}
实例3:
将D:/IO下的所有文件复制到目录D:/new_io
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IO3 {
public void addFile(String string1, String string2) {
File file1 = new File(string1);
File[] f = file1.listFiles();
for (File file : f) {
String string3 = string2;
String string4 = string1;
string3 += "/" + file.getName();
string4 += "/" + file.getName();
File file2 = new File(string3);
if (file.isDirectory()) {
file2.mkdir();
System.out.println("文件夹建立完毕");
addFile(string4, string3);
} else {
try {
file2.createNewFile();
@SuppressWarnings("resource")
FileInputStream fis = new FileInputStream(string4);
@SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream(string3);
byte[] data = new byte[1024];
int len;
while ((len = fis.read(data)) != -1) {
String string = new String(data, 0, len);
fos.write(string.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
IO3 io = new IO3();
io.addFile("d:/IO", "d:/new_io");
}
}
实例4:
用File类,在d:/io 目录中创建新的目录 test 在此目录中创建新的文件test.txt,使用BufferedWriter类写入5行数据,使用BufferedReader类读取出来,并显示到控制台
public class HomeWork1 {
public void Mymkdir(String string, String string2) {
File file = new File(string);
if (!file.exists()) {
file.mkdirs();
}
string2 = string + string2;
File file2 = new File(string);
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void ioOUT(String string) {
try {
FileWriter fw = new FileWriter(string);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("古军");
bw.newLine();
bw.write("华是");
bw.newLine();
bw.write("山东");
bw.newLine();
bw.write("科技大学");
bw.newLine();
bw.write("学生");
fw.flush();
bw.flush();
fw.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void ioINT(String string) {
try {
FileReader fr = new FileReader(string);
BufferedReader br = new BufferedReader(fr);
String string2 = null;
while ((string2 = br.readLine()) != null) {
System.out.println(string2);
}
fr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
HomeWork1 homeWork1 = new HomeWork1();
homeWork1.Mymkdir("d:/IO/test", "test.txt");
String string = "d:/IO/test/test.txt";
homeWork1.ioOUT(string);
homeWork1.ioINT(string);
}
}
实例5:
使用File类获得指定文件的扩展名,比如,d:/io/IODemo.java显示java
public class HomeWork2 {
public void f1(String string) {
File file = new File(string);
String string2 = file.getAbsolutePath();
int key = string2.lastIndexOf(".");
for (int i = key + 1; i < string2.length(); i++) {
System.out.print(string2.charAt(i));
}
}
public static void main(String[] args) {
HomeWork2 homeWork2 = new HomeWork2();
homeWork2.f1("dsjkfljslkfjsl.java");
}
}
实例6:
使用File类创建新文件,文件名为 当前时间.txt,格式示例如下:20170831163809.txt
public class HomeWork3 {
public void createFile(String string) {
string = "d:/IO/"+string+".txt";
File file = new File(string);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String data() {
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddhhmmss");
String dateString = sf.format(date);
return dateString;
}
public static void main(String[] args) {
HomeWork3 homeWork3 = new HomeWork3();
homeWork3.createFile(homeWork3.data());
}
}