Topic: write an application, enter a directory and a file type, display the directory meets all files of that type. Thereafter, the cut one of these files to another directory.
package zuoyeshiyi; import java.io. *; import java.util. *; public class fileDemo { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sca=new Scanner(System.in); System.out.println ( "Please enter the directory to be displayed"); String str=sca.nextLine(); System.out.println ( "Enter the desired file type filter"); String str1=sca.nextLine(); File f=new File(str); System.out.println ( "suffix" + str1 + "files have"); x(f,str1); System.out.println ( "Please enter the need to cut the file"); String str2=sca.nextLine(); System.out.println ( "Please enter the name of the file to cut"); String str3=sca.nextLine(); jianqie (str2, str3); File file=new File(str2); file.delete(); } public static void x(File f,String str1) { String []arr=f.list(new FilenameFilter() { public boolean accept(File dir,String name) { return name.endsWith (str1); // suffix name is the same } }); System.out.println(arr.length); for(String name:arr) { System.out.println(name); } } public static void jianqie(String str2,String str3) { BufferedInputStream bis=null; BufferedOutputStream bos=null; try { bis=new BufferedInputStream(new FileInputStream(str2)); bos=new BufferedOutputStream(new FileOutputStream(str3)); int k=0; while((k=bis.read())!=-1) { bos.write(k); bos.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace (); }finally { try { bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace (); } try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace (); } } } }