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. VS中Debug和Realease、及静态库和动态库的区别整理

    一.Debug和Realease区别产生的原因 Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发布版本,它往往是进行了各种优化,使得程序在代码 ...

  2. JQuery面试题答案

    jQuery面试题答案 转自:http://blog.csdn.net/zhangpei_xf/article/details/8822021 一.Jquery测试题 下面哪种不是jquery的选择器 ...

  3. CentOS 7 做服务器 CentOS 5 做客服机 搭建Apache+php+mysql网页

    1:在CentOS 7搭建yum源和php.Apache 安装    yum install php-mysql -y 文件 重启httpd服务:systemctl restart httpd.ser ...

  4. win7下IIS错误:"无法访问请求的页面,因为该页的相关配置数据无效"的解决方法(转)

    今天新装win7,然后在IIS下布署了一个网站,布署完成后运行,提示如下错误:HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效 ...

  5. 电脑控制台灯(c# hook,显示室温,联网校正时间)

          突发奇想,于是便写了一个小程序用于控制台灯,这几天功能也在不断的完善中,目前基本已经完成.下面进行功能的简述的代码的分析. 整体设计包含下位机程序和上位机程序.下位机用的c语言,上位机用的 ...

  6. vim 学习日志(4):多窗口使用技巧

    原文地址: http://blog.csdn.net/devil_2009/article/details/7006113 vim多窗口使用技巧 1.打开多个窗口打开多个窗口的命令以下几个:横向切割窗 ...

  7. Python基础篇【第1篇】: Python基础

    Python 简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...

  8. ArchLinux KDE安装中文输入法

    From: http://www.linuxdiyf.com/viewarticle.php?id=53375 1.安装中文输入法#pacman -S scim-pinyin #拼音输入法#pacma ...

  9. oracle连接的三个配置文件(转)

           Oracle中TNS的完整定义:transparence Network Substrate透明网络底层,监听服务是它重要的一部分,不是全部,不要把TNS当作只是监听器 ORACLE当中 ...

  10. sql 分组查询及格不及格人数

    select score as 类别,count(*) as 人数 from (select case when fen>=60 then '及格' else '不及格' end as scor ...