1)//递归删除某个目录下的所有的文件
public class DeteleFile {
public static void main(String[] args) {
File f=new File("D:\\cutvideo");//引入要删除的文件的文件夹目录的路径
deteleShowFile(f); //调用方法
System.out.println("删除成功");
}
static void deteleShowFile(File file) {
File [] fileList=file.listFiles();//将文件夹下面的文件取出来存到数组中
for(File f:fileList) {
if(f.isDirectory()) {//如果文件是目录,则接着递归调用,如果不是,就删除
deteleShowFile(f);
}
else {
f.delete();
}
}
}
}
2)//递归查看目录 ,将所有的文件(不是目录) 名,放到一个集合中,再把这个集合的内容写到一个文件中
public class Work4 {
private static ArrayList<String> list=new ArrayList<>();
public static void main(String[] args) throws IOException {
File f=new File("D:\\cutvideo");
lookDir(f);
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\cutvideo\\work.txt",true));
for(Iterator<String> it = list.iterator(); it.hasNext();){
bw.write(it.next()+"\r\n");
}
bw.close();
System.out.println("成功");
}
static void lookDir(File file) throws IOException {
File [] fileList=file.listFiles();
for(File f:fileList) {
if(f.isDirectory()) {
lookDir(f);
}
else {
System.out.println(f.getName());
list.add(f.getName());
}
}
}
}
3) 有一个记事本文件,里面是人员名单
有一个文件夹,里面是人员的照片,照片的在命名的时候,含有这个人的姓名信息
写一个程序,将名单中有照片的人的照片选出来,存到一个文件夹里
public class Work5 {
public static void main(String[] args) throws IOException {
File file1=new File("D:\\cutvideo\\photo");
File file2=new File("D:\\cutvideo\\list.txt");
photo( file1, file2);
}
static void photo(File file1,File file2) throws IOException {
String [] photoList=file1.list();//照片名字
String len=null;
for(String photoname:photoList) {
//读取记事本文件的人员名单
Reader rd=new FileReader(file2);
BufferedReader br=new BufferedReader(rd);
while((len = br.readLine())!=null){
if(photoname.contains(len)) {
InputStream in=new FileInputStream(file1+"\\"+photoname);
OutputStream out=new FileOutputStream("D:\\cutvideo\\photo-copy\\"+len+".jpg");
int size=in.available();
byte [] buff=new byte[size];
in.read(buff);
out.write(buff);
in.close();
out.close();
break;
}
}
rd.close();
br.close();
}
System.out.println("输出成功");
}
}
4)有一个文本文件,里面是学生名单, 还有一个文件夹,里面是学生上交的作业, 编写程序,将所有的没有上交作 业的学生的名字打印出来
public class StampStudentList {
public static void main(String[] args) throws IOException {
Scanner scan=new Scanner(System.in);
System.out.println("请将要比对的文件夹的路径粘贴到此处");
String path=scan.nextLine();
String path1=path.replace("\\", "/");
File nameFile=new File(path1);
System.out.println("请将学生名单的文件路径粘贴在此处");
String pathStudent=scan.nextLine();
String path2=pathStudent.replace("\\", "/");
File nameFile1=new File(path2);
studentList(nameFile,nameFile1);
}
static void studentList(File file,File nameFile1) throws IOException {
String [] fileNameList=file.list(); //已提交的文件名
BufferedReader b2 = new BufferedReader(new FileReader(nameFile1));
ArrayList<String> list2=new ArrayList<String>();//存储名单中的所有人
ArrayList<String> list1 =new ArrayList<String>();//存储已提交的人
String s=null;
String context=null;
for(String name:fileNameList) {
//将学生名单读取出来
Reader rd=new FileReader(nameFile1);
BufferedReader br=new BufferedReader(rd);
while((context = br.readLine())!=null){
if(name.contains(context)) {
list1.add(context);
//System.out.println("提交"+context);
break;
}
}
br.close();
}
//将全部人员名单添加至list2中
while((s=b2.readLine())!=null){
list2.add(s);
}
//移除名单中已经提交的人
list2.removeAll(list1);
for(String s1:list2) {
System.out.println("未提交"+s1);
}
b2.close();
}
}
5)
请编写类实现ServiceInfoManager接口,该类能自动读取service.txt配置文件生成ServiceInfo对象,并对外提供最快的按serviceName检索ServiceInfo的功能。
1)实现类
public class Service implements ServiceInfoManager {
public static void main(String[] args) throws IOException {
Scanner scan= new Scanner(System.in);
System.out.println("请输入服务器名称");
String serviceName=scan.nextLine();
Service ser = new Service();
ServiceInfo serinfo=ser.getServiceInfo( serviceName);
System.out.println(serinfo);
}
public ServiceInfo getServiceInfo(String serviceName) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("service.txt"));
ServiceInfo sinfo=new ServiceInfo();
String len=null;
while((len=br.readLine())!=null) {
if(len.contains(serviceName)) {
String[] ele = len.split("\t");
if(ele[0].equals(serviceName)) {
sinfo.setServiceName(ele[0]);
sinfo.setAddress(ele[1]);
sinfo.setPort(Integer.parseInt(ele[2]));
sinfo.setServiceClassName(ele[3]);
sinfo.setDescription(ele[4]);
}
}
}
return sinfo;
}
}
2)接口
package com.dao;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.beans.ServiceInfo;
public interface ServiceInfoManager {
public ServiceInfo getServiceInfo(String serviceName) throws FileNotFoundException, IOException;
}
3)实体类
package com.beans;
public class ServiceInfo {
private String serviceName;
private String address;
private int port;
private String serviceClassName;
private String description;
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getServiceClassName() {
return serviceClassName;
}
public void setServiceClassName(String serviceClassName) {
this.serviceClassName = serviceClassName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "ServiceInfo [serviceName=" + serviceName + ", address=" + address + ", port=" + port
+ ", serviceClassName=" + serviceClassName + ", description=" + description + "]";
}
}
4)service.txt 在src目录下直接建
#服务名 目的地址 端口号 服务类名 系统名称
HOST localhost 9870 TransferService 后台主机
HIPAY localhost 9870 BJElecToHipayService 应用服务器
HK 127.0.0.1 8080 UserService 用户主机