【Java学习笔记】文件信息

简介:

文件路径:

import java.io.File; 
public class FilePath { 
    public static void main(String arg[]) { 
        String pname1 = 
            File.separator + "stuff" + 
            File.separator + "vtc" + 
            File.separator + "java6" + 
            File.separator + "1204"; 
        System.out.println(pname1); 
        String pname2 = 
            File.separator + "stuff" + 
            File.separator + "bin"; 
        System.out.println(pname2); 
        String path = pname1 + File.pathSeparator + pname2; 
        System.out.println(path); 
    } 
}

从文件名得到文件路径信息:

import java.io.File; 
import java.io.IOException; 
public class FileNames { 
    public static void main(String arg[]) { 
        File file; 
        try { 
            file = new File("FileNames.java"); 
            System.out.println("Name: " + file.getName()); 
            System.out.println("Parent: " + file.getParent()); 
            System.out.println("Path: " + file.getPath()); 
            System.out.println("Absolute path: " + file.getAbsolutePath()); 
            System.out.println("Canonical path: " + file.getCanonicalPath()); 
            System.out.println(); 
            file = new File( 
                File.separator + "home" + 
                File.separator + "doc" + 
                File.separator + "vtc" + 
                File.separator + "java6" + 
                File.separator + "1204"); 
            System.out.println("Name: " + file.getName()); 
            System.out.println("Parent: " + file.getParent()); 
            System.out.println("Path: " + file.getPath()); 
            System.out.println("Absolute path: " + file.getAbsolutePath()); 
            System.out.println("Canonical path: " + file.getCanonicalPath()); 
            System.out.println(); 
            file = new File("."); 
            System.out.println("Name: " + file.getName()); 
            System.out.println("Parent: " + file.getParent()); 
            System.out.println("Path: " + file.getPath()); 
            System.out.println("Absolute path: " + file.getAbsolutePath()); 
            System.out.println("Canonical path: " + file.getCanonicalPath()); 
            System.out.println(); 
        } catch(IOException e) { 
            System.out.println(e); 
        } 
    } 
}

文件信息:

import java.io.File; 
import java.io.IOException; 
public class FileInfo { 
    public static void main(String arg[]) { 
        File file; 
        if(arg.length != 1) { 
            System.out.println("Usage: java FileInfo <filename>"); 
            return; 
        } 
        try { 
            file = new File(arg[0]); 
            System.out.println("Canonical path: " + file.getCanonicalPath()); 
            System.out.println("Is a normal file: " + file.isFile()); 
            System.out.println("Is a directory: " + file.isDirectory()); 
            System.out.println("Is a hidden file: " + file.isHidden()); 
            System.out.println("Can execute: " + file.canExecute()); 
            System.out.println("Can read: " + file.canRead()); 
            System.out.println("Can Write: " + file.canWrite()); 
            System.out.println("Path name is absolute: " + file.isAbsolute()); 
            System.out.println("Length of file: " + file.length()); 
            if(file.isDirectory()) { 
                String name[] = file.list(); 
                for(int i=0; i<name.length; i++) 
                    System.out.println("        " + name[i]); 
            } 
        } catch(IOException e) { 
            System.out.println(e); 
        } 
    } 
}

 




本文转自gnuhpc博客园博客,原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822314.html,如需转载请自行联系原作者

相关文章
|
29天前
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
30 1
|
1月前
|
Java
java中替换文件内容
java中替换文件内容
14 1
|
1月前
|
Java API
Java中文件与输入输出
Java中文件与输入输出
|
1月前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
10 0
|
1月前
|
Java
java程序导出堆文件
java程序导出堆文件
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
28 3
|
1月前
|
Java 数据库连接 API
Java 学习路线:基础知识、数据类型、条件语句、函数、循环、异常处理、数据结构、面向对象编程、包、文件和 API
Java 是一种广泛使用的、面向对象的编程语言,始于1995年,以其跨平台性、安全性和可靠性著称,应用于从移动设备到数据中心的各种场景。基础概念包括变量(如局部、实例和静态变量)、数据类型(原始和非原始)、条件语句(if、else、switch等)、函数、循环、异常处理、数据结构(如数组、链表)和面向对象编程(类、接口、继承等)。深入学习还包括包、内存管理、集合框架、序列化、网络套接字、泛型、流、JVM、垃圾回收和线程。构建工具如Gradle、Maven和Ant简化了开发流程,Web框架如Spring和Spring Boot支持Web应用开发。ORM工具如JPA、Hibernate处理对象与数
92 3
|
1月前
|
Java
使用java将字符串写入到指定的文件中
使用java将字符串写入到指定的文件中
11 0
|
1月前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0
|
1月前
|
Java
Java文件类
Java文件类
7 0
Java文件类