【Java学习笔记】文件信息

简介: 作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 文件路径: import java.io.File; public class FilePath {     public static void main(String arg[]) {         String pname1 =             File.

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

文件路径:

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/


               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

目录
相关文章
|
24天前
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
25 1
|
28天前
|
Java
java中替换文件内容
java中替换文件内容
14 1
|
29天前
|
Java API
Java中文件与输入输出
Java中文件与输入输出
|
30天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
9 0
|
1月前
|
Java
java程序导出堆文件
java程序导出堆文件
|
26天前
|
Java 数据库连接 API
Java 学习路线:基础知识、数据类型、条件语句、函数、循环、异常处理、数据结构、面向对象编程、包、文件和 API
Java 是一种广泛使用的、面向对象的编程语言,始于1995年,以其跨平台性、安全性和可靠性著称,应用于从移动设备到数据中心的各种场景。基础概念包括变量(如局部、实例和静态变量)、数据类型(原始和非原始)、条件语句(if、else、switch等)、函数、循环、异常处理、数据结构(如数组、链表)和面向对象编程(类、接口、继承等)。深入学习还包括包、内存管理、集合框架、序列化、网络套接字、泛型、流、JVM、垃圾回收和线程。构建工具如Gradle、Maven和Ant简化了开发流程,Web框架如Spring和Spring Boot支持Web应用开发。ORM工具如JPA、Hibernate处理对象与数
90 3
|
29天前
|
Java
使用java将字符串写入到指定的文件中
使用java将字符串写入到指定的文件中
11 0
|
29天前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0
|
29天前
|
Java
Java文件类
Java文件类
6 0
Java文件类
|
1月前
|
Java
java base64转doc文件
以下是使用Java进行Base64转换的例子: 1. 使用Java Base64工具类进行转换 ```java // 导入相关包 import java.util.Base64; import java.io.FileOutputStream; import java.io.IOException; // 将Base64字符串转换为字节数组 byte[] decodedBytes = Base64.getDecoder().decode(base64String); // 将字节数组写入到文件中 try (FileOutputStream fos = new FileOutputStrea