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)