|
天堂之光 人间希望
你我共同品味
JAVA的浓香.
Linux的清芬. |
« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
链接 |
联系我
msn:zhanglincon@hotmail.com |
Blog信息 |
blog名称: 日志总数:99 评论数量:281 留言数量:4 访问次数:808629 建立时间:2005年11月17日 |

| |
[java文件操作专题]列出文件的属性信息 原创空间, 软件技术
zhanglincon 发表于 2008/2/16 13:15:43 |
package book.io;
import java.io.File;import java.text.DecimalFormat;import java.util.Date;
/** * 获取文件的基本信息 * @author Administrator * */public class GetFileInfos {
public static void println(String s){ System.out.println(s); } public static void main(String[] args){ //用文件路径创建一个文件对象,文件路径可以是相对路径也可以是绝对路径 File file = new File("c:/temp/newTemp.txt"); println("文件名:\t"+file.getName()); //路径中的文件分隔符用系统默认分隔符替换 println("文件路径:\t"+file.getPath()); println("绝对路径:\t"+file.getAbsolutePath()); println("父目录:\t"+file.getParent()); println("文件是否存在:\t"+file.exists()); println("文件是否可读:\t"+file.canRead()); println("文件是否可写:\t"+file.canWrite()); println("文件是否是隐藏文件:\t"+file.isHidden()); println("文件是否是普通文件:\t"+file.isFile()); println("文件是否是文件目录:\t"+file.isDirectory()); println("文件路径是否是绝对路径:\t"+file.isAbsolute()); println("文件路径的URI:\t"+file.toURI()); println("文件最后修改时间:\t"+new Date(file.lastModified())); println("文件大小:\t"+file.length()+" bytes"); //java的java.text.DecimalFormat类专门用于对数字进行格式化 DecimalFormat df = new DecimalFormat(); String pattern = "0.00";//显示格式 df.applyPattern(pattern);//将格式应用于格式化器 println("文件大小:\t"+df.format((double)(file.length())/1024)+" kb"); }}
输出结果
文件名: newTemp.txt文件路径: c:\temp\newTemp.txt绝对路径: c:\temp\newTemp.txt父目录: c:\temp文件是否存在: true文件是否可读: true文件是否可写: true文件是否是隐藏文件: false文件是否是普通文件: true文件是否是文件目录: false文件路径是否是绝对路径: true文件路径的URI: file:/c:/temp/newTemp.txt文件最后修改时间: Fri Feb 15 17:15:48 CST 2008文件大小: 1217 bytes文件大小: 1.19 kb |
|
|