标签:
import java.io.*; class FileDemo { public static<T> void sop(T t) { System.out.println(t); } public static void main(String[] args)throws IOException { //Consmethod(); //method_1(); //method_2(); //method_3(); method_4(); } public static void method_1()throws IOException { //创建文件 File f = new File("F:\\myfile\\file.txt"); //f.deleteOnExit(); //sop("create:"+f.createNewFile()); sop("delete:"+f.delete()); //创建文件夹 File dir = new File("F:\\File\\dir"); sop("create:"+dir.mkdir()); // File dir = new File("F:\\File\\dir\\aaa\\bb\\cc"); // sop("create:"+dir.mkdirs()); } public static void method_2()throws IOException { File f = new File("E:\\java0217\\Java IO\\File\\FileDemo.java"); sop("canExecute():"+f.canExecute()); sop("exists()):"+f.exists()); //在判断文件目录是否是目录或文件时,必须先判断该文件对象封装的内容是否存在,通过上面的exists()判断。 sop("isdir:"+f.isDirectory()); sop("isfile:"+f.isFile()); sop("isHidden:"+f.isHidden()); sop("isAbsolute:"+f.isAbsolute()); } public static void method_3()throws IOException { File f = new File("F:\\myfile\\file.txt"); sop("getName:"+f.getName()); sop("getParent:"+f.getParent()); sop("getPath:"+f.getPath()); sop("getAbsolutePath:"+f.getAbsolutePath()); sop("lastModified:"+f.lastModified()); sop("length:"+f.length()); } public static void method_4()throws IOException { File f1 = new File("F:\\myfile\\file.txt"); File f2 = new File("F:\\myfile\\fileDemo.txt"); sop("renameTo:"+f1.renameTo(f2)); } //创建文件对象 public static void Consmethod() { //将file.txt封装成File对象,可以将已有的和未出现的文件或者文件夹封装成对象。 //构造方法一File(String pathname) File f1 = new File("F:\\myfile2\\file1.txt"); //构造方法二File(String parent, String child) File f2 = new File("F:\\myfile2","file2.txt"); //构造方法三File(File parent, String child) File d = new File("F:\\myfile2"); File f3 = new File(d,"file3.txt"); //static String separator String s = File.separator;//默认当前系统下的分隔符。具有跨平台性 File f4 = new File("F:"+s+"myfile2"+s+"file4.txt"); sop("f1:"+f1);//f1:F:\myfile2\file1.txt sop("f2:"+f2);//f2:F:\myfile2\file2.txt sop("f3:"+f3);//f3:F:\myfile2\file3.txt sop("f4:"+f4);//f4:F:\myfile2\file4.txt } }
import java.io.*; class FileDemo2 { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] args) { //listRoots(); //list(); listFiles(); } public static void listRoots() { File[] files = File.listRoots(); for(File file: files) { sop(file); } } public static void list() { File f = new File("F:\\"); String[] str = f.list();//调用list方法的file对象必须是一个封装的目录,而且还必须存在,否则返回空指针异常 for(String s: str) { sop(s); } } }
递归文件目录:
列出指定目录下的文件或文件夹,包含子目录中的内容,也就是列出指定目录下的所有内容。
import java.io.*; class FileDemo3 { public static void main(String[] args) { showdir(new File("E:\\java0217")); // ToBin(6); // getsum(10); // int x = getsum(10); // System.out.println(x); } public static void showdir(File dir) { System.out.println(dir); File[] files = dir.listFiles(); for(int i=0; i<files.length; i++) { if(files[i].isDirectory()==true) showdir(files[i]);//递归调用showdir() else System.out.println(files[i]); } } public static void ToBin(int num) { if(num>0) { ToBin(num/2);//递归调用ToBin() System.out.println(num%2); } } public static int getsum(int x) { if(x==1) return 1; else return x+getsum(x-1);//递归调用getsum() } }
import java.io.*; import java.util.*; class JavaFileList { public static void main(String[] args)throws IOException { File dir = new File("E:\\java0217"); ArrayList<File> al = new ArrayList<File>(); filetolist(dir,al); System.out.println(al.size()); } public static void filetolist(File dir,List<File> list)throws IOException { //System.out.println(dir); File[] files = dir.listFiles(); for(int i=0; i<files.length; i++) { if(files[i].isDirectory()) filetolist(files[i],list); else { files[i].getName().endsWith(".java"); list.add(files[i]); } } BufferedWriter buf = new BufferedWriter(new FileWriter("F:\\myfile\\listjava.txt")); for(File f: list) { String str = f.getAbsolutePath(); buf.write(str); buf.newLine(); buf.flush(); } buf.close(); } }
import java.io.*; class RemoveDir { public static void main(String[] args) { Removedir(new File("F:\\aaa")); } public static void Removedir(File dir) { File[] files = dir.listFiles(); for(int i=0; i<files.length; i++) { if(files[i].isDirectory()==true) Removedir(files[i]);//递归调用Removedir() else System.out.println(files[i].toString()+"-file-"+files[i].delete()); } System.out.println(dir+"::dir::"+dir.delete()); } } /* F:\aaa\bbb\bb::dir::true F:\aaa\bbb\ccc\a.txt.txt-file-true F:\aaa\bbb\ccc::dir::true F:\aaa\bbb::dir::true F:\aaa::dir::true */
练习三:先切割文件(流),在合并文件(流)
import java.io.*; import java.util.*; class SplitFileDemo { public static void main(String[] args)throws IOException { SplitFile(); SequenceFile(); } public static void SplitFile()throws IOException { FileInputStream fis = new FileInputStream("F:\\myfile\\b03.mp3"); byte[] buf = new byte[1024*1024]; int len = 0; int count=1; FileOutputStream fos = null; while((len=fis.read(buf))!=-1) { fos = new FileOutputStream("F:\\myfile\\splitfiles\\part"+(count++)+".suipian");//以1M为单位进行切割 fos.write(buf,0,len); fos.close(); } fis.close(); } public static void SequenceFile()throws IOException { Vector<FileInputStream> v = new Vector<FileInputStream>(); for(int i=1;i<=3;i++) { v.add(new FileInputStream("F:\\myfile\\splitfiles\\part"+i+".suipian")); } Enumeration<FileInputStream> enu = v.elements(); SequenceInputStream sis = new SequenceInputStream(enu);//将多个流整合为一个流对象 FileOutputStream fos = new FileOutputStream("F:\\myfile\\splitfiles\\part123.mp3"); byte[] buf = new byte[1024]; int num = 0; while((num = sis.read(buf))!=-1) { fos.write(buf,0,num); } fos.close(); sis.close(); } }
import java.io.*; import java.util.*; class RunCount { public static void main(String[] args)throws IOException { Properties prop = new Properties(); File f = new File("F:\\myfile\\count.ini");//"count.properties" if(!f.exists()) f.createNewFile(); FileInputStream fis = new FileInputStream(f); prop.load(fis); int count=0; String value = prop.getProperty("time"); if(value!=null) { count = Integer.parseInt(value); if(count>=5) { System.out.println("你好,使用次数已到,再用需要收费!"); return ; } } count++; prop.setProperty("time",count+""); FileOutputStream fos = new FileOutputStream(f); prop.store(fos,""); fis.close(); fos.close(); } }
标签:
原文地址:http://www.cnblogs.com/XYQ-208910/p/4918754.html