通过File类获取文件信息,未封装,求封装成类
2012-05-12 17:08:43 来源:WEB开发网核心提示:package com.taoniwu.io;import java.io.*;public class TestFile {/** * @param args */public static void main(String[] args) throws IOException {File f;//键盘输入 字符串赋
package com.taoniwu.io;
import java.io.*;
public class TestFile {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
File f;
//键盘输入 字符串赋给sr
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(in);
String sr = null;
try {
System.out.print("请输入文件路径:");
sr = buf.readLine();
} catch (Exception e) {
System.out.println(e + "输入错误");
System.exit(-1);
}
//判断文件或文件路径是否存在,存在则创建File对象
try {
if (sr.length() > 0) {
f = new File(sr);
if(f.exists()){
info(f);
}
else{
System.out.println("这不是一个文件或文件夹路径!");
}
}
} catch (Exception e) {
System.out.println(e + "运行错误");
}
}
//info方法,输出文件或文件夹信息
public static void info(File f) throws IOException{
//判断路径是文件还是文件夹
String sw;
long leng = 0;
if(f.isDirectory()){
sw = "文件夹";
leng = 0;
}
else{
sw = "文件";
leng = f.length();
}
System.out.println(sw+"名字:"+f.getName());
System.out.println(sw+"路径:"+f.getPath());
System.out.println(sw+"是否可读:"+(f.canRead()?"可读":"不可读"));
System.out.println(sw+"是否可写:"+(f.canWrite()?"可写":"不可写"));
//判断文件长度是否为0
System.out.println(sw+"长度为:"+(leng == 0?"文件长度为0或为文件夹":leng));
}
}
- ››获取Android手机型号,系统版本,App版本号等信息
- ››获取Windows xp系统高权限无需注销用户
- ››通过远程管理更改ESXi主机root用户密码
- ››获取checkedListBox 选中的项并且移动
- ››通过JS得到当前焦点(鼠标)的坐标
- ››通过Mysql命令行语句来导入、导出数据库文件
- ››获取ios手机的mac地址的函数
- ››通过查看mysql 配置参数、状态来优化你的mysql
- ››获取Android手机中SD卡内存信息
- ››通过oracle的sys密码重置其它密码
- ››获取计算机电源或者电池状态
- ››通过HashMap和ArrayList容器实现一个Key对多个值
赞助商链接
