import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
public class FilePath {
public static void main(String[] args) {
//写入文件路径
String writePath = "D:/bingdu.com.doc";
//读取文件路径
String path = "E:/G";
fileterInfo(path);
writerFile(writePath, path);
}
// 读取文件信息
public static void fileInfo(String path) {
File file = new File(path);
// String fileName = path.substring(path.lastIndexOf("/")+1);
if (file.exists()) {
System.out.println(file.getName() + "文件已存在!");
System.out.println("file.getName:" + file.getName());
System.out
.println("file.getAbsolutePath:" + file.getAbsolutePath());
try {
System.out.println("file.getCanonicalPath:"
+ file.getCanonicalPath());
System.out.println("file.getCanonicalPath:"
+ file.getCanonicalFile());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("file.getParent:" + file.getParent());
System.out.println("file.getPath:" + file.getPath());
System.out
.println("file.getAbsoluteFile:" + file.getAbsoluteFile());
System.out.println("file.getClass:" + file.getClass());
System.out.println("file.getParentFile:" + file.getParentFile());
} else {
System.out.println(file.getName() + "文件不存在!");
// System.out.println(file.exists() ? fileName + "文件已存在!" : fileName
// + "文件不存在!");
}
}
// 读取文件目录
public static String fileterInfo(String path) {
fileCount = 0;
directoryCount = 0;
sizes = 0;
content = "";
/*
* System.out.println(path + ">>>详细信息如下:/n");
* System.out.println("总文件个数:" + fileCount + " 个");
* System.out.println("总目录个数:" + directoryCount + " 个");
* System.out.println("总文件大小:" + new DecimalFormat("0.##").format(sizes)
* + " MB(兆)");
*/
return path + ">>>详细信息如下:/r/n/r/n" + fileterInfoToo(path)
+ "/r/n总文件个数:" + fileCount + " 个" + "/r/n总目录个数:"
+ directoryCount + " 个" + "/r/n总文件大小:"
+ new DecimalFormat("0.##").format(sizes) + " MB(兆)/r/n"
+ new DecimalFormat("0.##").format(sizes / 1024)
+ " GB(千兆)/r/n";
}
static int fileCount, directoryCount;
static double sizes;
static String content;
// 递归读取文件
public static String fileterInfoToo(String path) {
File file = new File(path);
String size = "";
if (file.exists() && file.isDirectory()) {
/*
* String[] arrStr = file.list(); for (String string : arrStr) {
* System.out.println(string); }
* System.out.println("总文件个数:"+arrStr.length);
*/
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isFile()) {
/*
* System.out.println(file2.exists() ? file2.getParent() +
* "-->" + file2.getName() : "");
*/
try {
if (file2.length() < 1024)
size = String.valueOf(file2.length()) + " B(字节)";
else if (file2.length() < 1024 * 1024)
size = new DecimalFormat("0.##")
.format(((double) file2.length() / 1024))
+ " KB(千字节)";
else if (file2.length() < 1024 * 1024 * 1024)
size = new DecimalFormat("0.##")
.format(((double) file2.length() / 1024 / 1024))
+ " MB(兆)";
else
size = new DecimalFormat("0.##")
.format(((double) file2.length() / 1024 / 1024 / 1024))
+ " GB(千兆)";
content += file2.getPath() + "的文件大小:" + size
+ file2.length() + " Bytes/r/n";
} catch (Exception e) {
e.printStackTrace();
}
sizes += ((double) file2.length() / 1024 / 1024);
fileCount++;
} else if (file2.isDirectory()) {
fileterInfoToo(file2.getPath());
directoryCount++;
}
}
}else { // System.out.println(path + "文件目录不存在!");
content = path +"文件目录不存在!";
}
return content;
}
// 写入文件
public static void writerFile(String writePath, String path) {
try {
BufferedWriter bWriter = new BufferedWriter(new FileWriter(
writePath));
bWriter.write(fileterInfo(path));
bWriter.flush(); bWriter.close(); System.out.println("Over"); } catch (IOException e) { e.printStackTrace(); } }}