课后作业:
1编写一个程序,指定一个文件夹,能自动计算出其总容量。
import java.io.File; import java.util.ArrayList; public class Size { static long size=0; private static ArrayList<String> filelist=new ArrayList<String>(); public static void main(String[] args) { Size s=new Size(); String filePath="D:\软件"; s.getFiles(filePath); } //通过递归得到某一路径下所有的目录及文件 void getFiles(String filePath) { File root=new File(filePath); File[] files=root.listFiles(); for(File file:files) { if(file.isDirectory()) { getFiles(file.getAbsolutePath()); filelist.add(file.getAbsolutePath()); } else { size+=file.getAbsolutePath().length(); } } System.out.println("大小是"+size); } }
2加密文件
import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; public class Encryption { private static final int numOfEncAndDec=0x99;//加密解密密钥 private static int dataOfFile=0;//文件字节内容 public static void main(String[] args) { File srcFile=new File("aaaa.txt");//初始化文件 File encFile=new File("bbbb.txt"); //加密文件 File decFile=new File("cccc.txt"); //解密文件 try { EncFile(srcFile,encFile); //加密操作 DecFile(encFile,decFile); }catch(Exception e) { e.printStackTrace(); } } private static void EncFile(File srcFile,File encFile)throws Exception{ if(!srcFile.exists()) { System.out.println("source file not exixt"); } if(!encFile.exists()) { System.out.println("encrypt file created"); encFile.createNewFile();//若无加密文件,新建一个加密文件 } InputStream fis=new FileInputStream(srcFile); OutputStream fos=new FileOutputStream(encFile); while((dataOfFile=fis.read())>-1) {//当读到文件内容时 fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入 } fis.close(); fos.flush(); fos.close(); } private static void DecFile(File encFile,File decFile)throws Exception{ if(!encFile.exists()) { System.out.println("encrypt file not exixt"); } if(!decFile.exists()) { System.out.println("decrypt file created"); decFile.createNewFile(); } InputStream fis=new FileInputStream(encFile); OutputStream fos=new FileOutputStream(decFile); while((dataOfFile=fis.read())>-1) { fos.write(dataOfFile^numOfEncAndDec); } fis.close(); fos.flush(); fos.close(); } }
3编写一个文件加解密程序,通过命令行完成加解密工作
import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; public class Encryption { private static final int numOfEncAndDec=0x99;//加密解密密钥 private static int dataOfFile=0;//文件字节内容 public static void main(String[] args) { File srcFile=new File("aaaa.txt");//初始化文件 File encFile=new File("bbbb.txt"); //加密文件 File decFile=new File("cccc.txt"); //解密文件 try { EncFile(srcFile,encFile); //加密操作 DecFile(encFile,decFile); }catch(Exception e) { e.printStackTrace(); } } private static void EncFile(File srcFile,File encFile)throws Exception{ if(!srcFile.exists()) { System.out.println("source file not exixt"); } if(!encFile.exists()) { System.out.println("encrypt file created"); encFile.createNewFile();//若无加密文件,新建一个加密文件 } InputStream fis=new FileInputStream(srcFile); OutputStream fos=new FileOutputStream(encFile); while((dataOfFile=fis.read())>-1) {//当读到文件内容时 fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入 } fis.close(); fos.flush(); fos.close(); } private static void DecFile(File encFile,File decFile)throws Exception{ if(!encFile.exists()) { System.out.println("encrypt file not exixt"); } if(!decFile.exists()) { System.out.println("decrypt file created"); decFile.createNewFile(); } InputStream fis=new FileInputStream(encFile); OutputStream fos=new FileOutputStream(decFile); while((dataOfFile=fis.read())>-1) { fos.write(dataOfFile^numOfEncAndDec); } fis.close(); fos.flush(); fos.close(); } }
动手动脑
1.使用Files. walkFileTree()找出指定文件夹下所有扩展名为.txt和.java的文件。
import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class FileGlobNIO { public static void main(String args[]) throws IOException { String glob = "glob:**/*.{java,txt}"; String path = "C:/"; match(glob, path); } public static void match(String glob, String location) throws IOException { final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob); Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (pathMatcher.matches(path)) { System.out.println(path); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } }
2.使用Files. walkFileTree()找出指定文件夹下所有大于指定大小(比如1M)的文件。
import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.FileVisitOption; import java.nio.file.FileVisitResult; import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.util.EnumSet; public class Search implements FileVisitor { private final PathMatcher matcher; private final long accepted_size; public Search(String glob,long accepted_size) { matcher= FileSystems.getDefault().getPathMatcher("glob:" +glob); this.accepted_size=accepted_size; } void search(Path file) throws IOException { long size = (Long) Files.getAttribute(file, "basic:size"); if(size ==accepted_size) { System.out.println(file); } } @Override public FileVisitResult postVisitDirectory(Object dir, IOException exc)throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)throws IOException { search((Path) file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Object file, IOException exc)throws IOException { return FileVisitResult.CONTINUE; } public static void main(String[] args) throws IOException{ String glob= "*.jpg"; long size = 1048576;//1M=1024k=1048576字节 Path fileTree = Paths.get("C:/"); Search walk=new Search(glob, size); EnumSet opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS); System.out.println("C盘中大小等于1M的文件有"); Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk); } }
3.使用Files. walkFileTree()找出指定文件夹下所有包容指定字符串的txt文件。
import java.io.IOException; import java.io.*; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class FileGlobNIO { public static void main(String args[]) throws IOException { String glob = "glob:**/*.txt"; String path = "C:/"; match(glob, path); } public static void match(String glob, String location) throws IOException { final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob); Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (pathMatcher.matches(path)) { BufferedReader reader =Files.newBufferedReader(path);//读取文件内的内容 String line=null; while((line = reader.readLine()) !=null) { if(line=="asdfghjkl")//若读取的内容等于“asdfghjkl"则输出文件名 { System.out.println(path); break; } } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } }