java io学习之File类
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类的更多相关文章
- Java IO体系之File类浅析
Java IO体系之File类浅析 一.File类介绍 位于java.io下的Java File类以抽象的方式代表文件名和目录路径名.该类主要用于文件和目录的创建.文件的查找和文件的删除等.File对 ...
- java IO(一):File类
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- java io知识点汇总FIle类
1.路径分隔符问题: 因为java有跨平台行,而在windows和linux中的目录分隔符是不同的.windows是"\" 而linux是"/" 所以必须想办 ...
- 【Java IO流】File类的使用
File类的使用 Java中的File类是在java.io.File中,Java.IO.File类表示文件或目录. File类只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. 一 ...
- java IO流之——File类知识总结和面试
File类描述的是一个文件或文件夹.(文件夹也可以称为目录).该类的出现是对文件系统的中的文件以及文件夹进行对象的封装.可以通过对象的思想来操作文件以及文件夹.可以用面向对象的处理问题,通过该对象的方 ...
- java IO流 之 FIle类基础
package IO; import java.io.File;import java.io.IOException; public class FileIO { /** * 构建及获取文件名信息 * ...
- java学习一目了然——File类文件处理
java学习一目了然--File类文件处理 File类(java.io.File) 构造函数: File(String path) File(String parent,String child) F ...
- Java学习:File类
Java学习:File类 File类的概述 重点:记住这三个单词 绝对路径和相对路径 File类的构造方法 File类判断功能的方法 File类创建删除功能的方法 File类获取(文件夹)目录和文件夹 ...
- Java学习笔记——File类之文件管理和读写操作、下载图片
Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...
随机推荐
- SpringMVC访问静态资源
SpringMVC访问静态资源 在SpringMVC中常用的就是Controller与View.但是我们常常会需要访问静态资源,如html,js,css,image等. 默认的访问的URL都会被Dis ...
- [Eclipse] - 集成Tomcat热加载插件
使用Eclipse + Tomcat,要使用热加载,总是会重启tomcat webapp. 可以使用这个插件:jrebel 如果是Tomcat 7.0+版本,需要使用jrebel5.5.1+的版本,不 ...
- [2014.01.27]WFsoft.wfWebCtrl.wfUrlPager 3.2
wfUrlPager多功能.Net翻页组件,使用简单,功能强大. 提供"首页","上一页","下一页","末页",&qu ...
- shiro 标签
在使用Shiro标签库前,首先需要在JSP引入shiro标签: <%@ taglib prefix="shiro" uri="http://shiro.apache ...
- 利用docker搭建rtmp服务器(1)
以后的项目里面可能需要用到直播,所以就先看看 本来想在自己MAC上搭建nginx的,后来怕把自己的机子搞乱,刚好就学习了下docker,感觉docker强大就在于是一个操作系统软件的版本管理系统,可以 ...
- OGNL语言
OGNL 一.概述 以下内容摘自Ognl的官网: OGNL stands for Ob ...
- H5课程大纲
K1模块课程: 课程模块 课程阶段 课程内容 K1 模块 第1阶段 认识前端开发 环境配置.使用标签的分类.写法及使用规范CSS样式的使用.各类常见样式Photoshop使用16大常用样式盒模型.语义 ...
- Java锁 到底锁的是哪个对象?
更新:在一次和一位专家的交谈中,他对一下代码能否能够成功同步,给予了否定的答案, 他的理由是”以构造函数的成员变量作为synchronized的锁,在多线程的情况下,每一个线程都持有自己私有变量的锁, ...
- [IIS]IIS扫盲(一)
iis - IIS概念相关 1.IIS(Inter-IC Sound bus)又称I2S,是菲利浦公司提出的串行数字音频总线协议.目前很多音频芯片和MCU都提供了对IIS的支持.IIS总线只处理声音数 ...
- XE6移动开发环境搭建之IOS篇(8):在Mac OSX 10.8中安装XE6的PAServer(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 安装PAServer ...