文件IO常用操作
package com.gu.file; import java.io.File; //相对路径与绝对路径构建file对象 public class Demo1 { public static void main(String[] args) { //String path="E:"+File.pathSeparator+""; String parentPath="E"; String name="2.txt"; //相对 File f=new File(parentPath, name); System.out.println(f.getName()); //在当前路径下构建文件 f=new File("test.txt"); System.out.println(f.getName()); System.out.println(f.getAbsolutePath()); //绝对路径 f=new File("E:/test2"); System.out.println(f.getAbsolutePath()); } } package com.gu.file; import java.io.File; import java.io.FilenameFilter; import java.util.Arrays; /** * 输出目录下文件 * @author 谷 * */ public class Demo2 { public static void main(String[] args) { String path="E:/test"; File parent =new File(path); //printName(parent); //printFiles(parent); File []roots=File.listRoots(); System.out.println(Arrays.toString(roots)); } //递归显示所有子文件和文件夹 public static void printFiles(File src){ if(src==null||!src.exists()){ return ; } System.out.println(src.getAbsolutePath()); if(src.isDirectory()){ for(File f:src.listFiles()){ printFiles(f); } } } //递归显示所有子文件 public static void printName(File src){ getdric(src); File []files=src.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return new File(dir, name).isDirectory(); } }); for(File ff:files){ //System.out.println(ff.getAbsolutePath()); printName(ff); } } //获取当前目录下的子文件夹 public static void getdric(File src){ if(src==null||!src.exists()){ return ; } if(src.isDirectory()){ for(File f:src.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return new File(dir, name).isFile(); } })){ System.out.println(f.getAbsolutePath()); src=f; } }
package com.gu.stream; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * 字节缓冲流无新增方法,但提高性能 * 字符缓冲流有reader.readLine(),write.newLine(); * 例如:String line=null; * while(null!=(line=bufferedreader.readline)) * 替换:char[]in=new char[1024]; * int len=0; * while(-1!=(len=reader.read(in))) * @author 谷 * */ public class filecopyUtil { public static void main(String[] args) { File src=new File ("E:/test/dd/Demo1.txt"); File dest=new File ("E:/test/"); try { copyFile(src, dest); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void copyFile(String srcPath,String destpath) throws IOException { copyFile(new File (srcPath),new File(destpath)); } public static void copyFile(File src,File dest) throws IOException ,FileNotFoundException{ if(!src.isFile()){ System.out.print("不是文件"); throw new IOException("只能拷贝文件"); } if(src==null||!src.exists()){ return ; } BufferedInputStream in=new BufferedInputStream(new FileInputStream(src)); //OutputStream out=new FileOutputStream(dest); BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(dest)); //BufferedOutputStream ,BufferedInputStream 无新增方法 byte[]car=new byte[10]; int len; while(-1!=(len=in.read(car))){ out.write(car, 0, len); } out.flush(); in.close(); out.close(); } }
}
}