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. IEnumerable 和 IQueryable 区别

    IQueryable继承自IEnumerable,所以对于数据遍历来说,它们没有区别. 但是IQueryable的优势是它有表达式树,所有对于IQueryable的过滤,排序等操作,都会先缓存到表达式 ...

  2. Docker之功能汇总

    Docker-给容器做端口映射 基本的命令是 -P(大写) :Docker 会随机映射一个 49000~49900 的端口到内部容器开放的网络端口基本的命令是 -p(小写) :Docker则可以指定要 ...

  3. MSSQL FOR MXL PATH 运用(转载)

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  4. 一个服务器上面配置多个IP ,实现指定IP的域名请求

    //配置多个IP命名using System.Net; //********************************************************************** ...

  5. linux下的vim使用教程

    命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...

  6. SparkContext源码阅读

    SparkContext是spark的入口,通过它来连接集群.创建RDD.广播变量等等. class SparkContext(config: SparkConf) extends Logging w ...

  7. 什么是BOM头,及PHP解决办法

    类似WINDOWS自带的记事本等软件,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM).它是一串隐藏的字符,用于让记事本等编辑器识别 ...

  8. SSH框架流程

    流程图 具体步骤 一.实体类 //Serializable在网络的环境下做类传输public class Category implements Serializable { private Inte ...

  9. 静态修饰符(关键字static)

    1.Static修饰的方法或变量通常称为类方法和类属性 2.静态方法中不能使用this和super关键字,也不能做为局部变量使用 3.在静态方法中不能访问非静态成员方法和非静态成员变量,但是在非静态成 ...

  10. alarm rtc

    http://sharp2wing.iteye.com/blog/1329518 http://blog.csdn.net/sking002007/article/details/6593809 io ...