动手动脑11.19
动手动脑
一
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); } } public FileVisitResult postVisitDirectory(Object dir, IOException exc)throws IOException { return FileVisitResult.CONTINUE; } public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)throws IOException { return FileVisitResult.CONTINUE; } public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)throws IOException { search((Path) file); return FileVisitResult.CONTINUE; } 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; Path fileTree = Paths.get("F:/"); Search walk=new Search(glob, size); EnumSet opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS); System.out.println("F盘中大小等于1M的文件有"); Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk); } }
二
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 Search { public static void main(String args[]) throws IOException { String glob = "glob:**/*.{java,txt}"; String path = "F:/"; 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; } }); } }
课后作业
一
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="G:\\JAVA";
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);
}
}
二
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileCode {
private static final int numOfEncAndDec=0x99;//加密解密密钥
private static int dataOfFile=0;//文件字节内容
public static void main(String[] args) {
File srcFile=new File("G:\\JAVA\\新建文件夹\\实验用\\初始化.txt");//初始化文件
File encFile=new File("G:\\JAVA\\新建文件夹\\实验用\\加密.txt"); //加密文件
File decFile=new File("G:\\JAVA\\新建文件夹\\实验用\\解密.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();
}
}
三
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CutFile {
public static void main(String[] args) {
//调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
cutFile("G:\\JAVA\\新建文件夹\\实验用\\初始化.txt", "G:\\JAVA\\新建文件夹\\实验用",1024 * 1024 * 20);
}
private static void cutFile(String src, String endsrc, int num) {
FileInputStream fis = null;
File file = null;
try {
fis = new FileInputStream(src);
file = new File(src);
//创建规定大小的byte数组
byte[] b = new byte[num];
int len = 0;
//name为以后的小文件命名做准备
int name = 1;
//遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
while ((len = fis.read(b)) != -1) {
//分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备
String name2 = file.getName();
int lastIndexOf = name2.lastIndexOf(".");
String substring = name2.substring(0, lastIndexOf);
String substring2 = name2.substring(lastIndexOf, name2.length());
FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2);
//将byte数组写入对应的小文件中
fos.write(b, 0, len);
//结束资源
fos.close();
name++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
//结束资源
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*public class GotherFile {
public static void main(String[] args){
//调用togetherFile()函数合并小文件到大文件 参数列表分别为 (小文件所在的父文件夹路径,所合成的大文件的路径)
togetherFile("G:\\JAVA\\新建文件夹\\实验用","G:\JAVA\新建文件夹\实验用");
}
private static void togetherFile(String src, String endsrc){
FileOutputStream fos = null;
File file1 = null;
File file2 = null;
try {
file1 = new File(endsrc);
file2 = new File(src);
//获得大文件的存储路径的FileOutputStream对象
fos = new FileOutputStream(endsrc);
//循环遍历对应文件夹中的所有小文件
for(File file : file2.listFiles()){
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
byte[] b = new byte[1024*1024];
int len = 0;
//将小文件读入byte数组,之后再将byte数组写入大文件中
while((len = fis.read(b)) != -1){
fos.write(b, 0, len);
}
//结束资源
fis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fos != null){
//结束资源
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}*/