1.先看下四个静态变量

static String pathSeparator

The system-dependent path-separator character, represented as a string for convenience.
static char pathSeparatorChar

The system-dependent path-separator character.
static String separator

The system-dependent default name-separator character, represented as a string for convenience.
static char separatorChar

The system-dependent default name-separator character.

由于windows和unix内核的系统路径表示方式不一致(windows为'\',unix内核的为'/'),所以java提供了公用的路径分割符,根据不同的系统自动变换,但是在实际使用中,都使用'/',windows和unix内核系统都支持。

Linux下测试

public class FileTest {
public static void main(String[] args) {
System.out.println(File.pathSeparator);
System.out.println(File.pathSeparatorChar);
System.out.println(File.separator);
System.out.println(File.separatorChar);
}
}

输出

:
:
/
/

2.File类的构造方法

File(File parent, String child)

Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)

Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)

Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)

Creates a new File instance by converting the given file: URI into an abstract pathname.

第一种

    @Test
public void test1() throws Exception{
File parent = new File("/joey/soft");
File son = new File(parent,"son.txt");
System.out.println(son.getAbsolutePath());
System.out.println(son.getParent());
System.out.println(parent.getName());
}

输出

/joey/soft/son.txt
/joey/soft
soft

第二种

    @Test
public void test2() throws Exception{
File son = new File("/joey/soft/son.txt");
System.out.println(son.getAbsolutePath());
System.out.println(son.getParent());
}

输出

/joey/soft/son.txt
/joey/soft

第三种

    @Test
public void test3() throws Exception{
File son = new File("/joey/soft","son.txt");
System.out.println(son.getAbsolutePath());
System.out.println(son.getParent());
}

输出:

/joey/soft/son.txt
/joey/soft

第四种

    @Test
public void test4() throws Exception{
File f = new File("/joey/soft","son.txt");
File son = new File(f.toURI());
System.out.println(son.getAbsolutePath());
System.out.println(son.getParent());
}

输出

/joey/soft/son.txt
/joey/soft

当然也可以不写绝对路径,写相对路径。

@Test
public void test5() throws Exception{
File son = new File("test.txt");
System.out.println(son.getAbsolutePath());
System.out.println(son.getParent());
}

输出

/home/joey/eclipse/workspace/IOTest/test.txt
null

拷贝文件的例子,不一定正确

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class FileUtils { /**
* copy file
* @param srcPath eg. /home/joey/soft/aa.txt
* @param destPath eg. /home/joey/soft/aa.txt
*/
public static void copyFile(String srcPath,String destPath) {
File src = new File(srcPath);
File desc = new File(destPath); if(!src.exists()){
System.out.println("src is not found");
return ;
} // if(!desc.isFile()){
// System.out.println("desc is not a file");
// return;
// } InputStream is = null;
OutputStream out = null;
try {
is = new FileInputStream(src);
out = new FileOutputStream(desc); int len = 0;
byte b[] = new byte[1024];
while((len=is.read(b))!=-1){
out.write(b, 0, len);
}
out.flush(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* copyFolder Recursively
* @param srcPath eg. /home/joey/soft/test
* @param destPath eg. /home/joey/soft/test2
*/
public static void copyFolder(String srcPath,String destPath){ File src = new File(srcPath);
File dest = new File(destPath);
copyFileOrFolderUtil(src, dest); } public static void copyFolder(File src,File dest){ copyFileOrFolderUtil(src, dest); } private static void copyFileOrFolderUtil(File f,File destFile){ if(f.isDirectory()){
destFile = new File(destFile,f.getName());
destFile.mkdirs(); File[] fs = f.listFiles();
for(File f1 : fs){
copyFileOrFolderUtil(f1, destFile);
} }else{
System.out.println(f.getAbsolutePath());
System.err.println(destFile.getAbsolutePath()+"/"+f.getName());
FileUtils.copyFile(f.getAbsolutePath(), destFile.getAbsolutePath()+"/"+f.getName());
}
} }

参考:http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.net.URI)

java io学习之File类的更多相关文章

  1. Java IO体系之File类浅析

    Java IO体系之File类浅析 一.File类介绍 位于java.io下的Java File类以抽象的方式代表文件名和目录路径名.该类主要用于文件和目录的创建.文件的查找和文件的删除等.File对 ...

  2. java IO(一):File类

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  3. java io知识点汇总FIle类

    1.路径分隔符问题: 因为java有跨平台行,而在windows和linux中的目录分隔符是不同的.windows是"\" 而linux是"/"  所以必须想办 ...

  4. 【Java IO流】File类的使用

    File类的使用 Java中的File类是在java.io.File中,Java.IO.File类表示文件或目录. File类只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. 一 ...

  5. java IO流之——File类知识总结和面试

    File类描述的是一个文件或文件夹.(文件夹也可以称为目录).该类的出现是对文件系统的中的文件以及文件夹进行对象的封装.可以通过对象的思想来操作文件以及文件夹.可以用面向对象的处理问题,通过该对象的方 ...

  6. java IO流 之 FIle类基础

    package IO; import java.io.File;import java.io.IOException; public class FileIO { /** * 构建及获取文件名信息 * ...

  7. java学习一目了然——File类文件处理

    java学习一目了然--File类文件处理 File类(java.io.File) 构造函数: File(String path) File(String parent,String child) F ...

  8. Java学习:File类

    Java学习:File类 File类的概述 重点:记住这三个单词 绝对路径和相对路径 File类的构造方法 File类判断功能的方法 File类创建删除功能的方法 File类获取(文件夹)目录和文件夹 ...

  9. Java学习笔记——File类之文件管理和读写操作、下载图片

    Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...

随机推荐

  1. 036. asp.netWeb用户控件之五使用用户控件实现分页数据导航

    UserDataPager.ascx用户控件代码: <%@ Control Language="C#" AutoEventWireup="true" Co ...

  2. 电脑ip(本地ip和本机ip)

    1.localhost:localhost 是个域名,不是地址,它可以被配置为任意的 IP 地址,不过通常情况下都指向 127.0.0.1(ipv4)和 [::1](ipv6) 2.127.0.0.1 ...

  3. 使用 Windows AIK 创建自定的客户端系统WIM文件

    Windows 7/2008 的AIK 3.0下载页面:地址链接 1.8G [3.1补充包为1.4G] 安装3.0后,升级为3.1方法: xcopy E:\ "C:\Program File ...

  4. 【MySQL】结构行长度的一些限制

    今天被开发提交的DDL变更再次困惑,表中字段较多,希望将已有的两个varchar(4000)字段改为varchar(20000),我想innodb对varchar的存储不就是取前768字节记录当前行空 ...

  5. bootstrap-图文混排 media

    <!-- media 图文混排 media-left(right) 图片的区域 在左边显示(右边) media-body 内容区域 media-heading 内容区域里的标题 media-mi ...

  6. 图的存储,搜索,遍历,广度优先算法和深度优先算法,最小生成树-Java实现

    1)用邻接矩阵方式进行图的存储.如果一个图有n个节点,则可以用n*n的二维数组来存储图中的各个节点关系. 对上面图中各个节点分别编号,ABCDEF分别设置为012345.那么AB AC AD 关系可以 ...

  7. restfull api

    REST 表示状态传输.这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗.核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP 用于使它们之间调 ...

  8. 23. Sum Root to Leaf Numbers

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  9. Mono addin 学习笔记 1

    Mono Addin是一个开源的插件框架,其主要支持的特性如下: The main features of Mono.Addins are: Supports descriptions of add- ...

  10. 去块率波 Deblocking filter

    基于块的视频编码的一个典型特点就是在图像中会出现偶发的可察觉的块结构,这是由于重构块的边缘像素与块内部像素相比恢复精度要低,块效应是目前压缩编码最明显的视觉失真之一.在H.264/ AVC视频编码标准 ...