1、使用createNewFile创建文件
package com.controller;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{
@GetMapping("/{id}")
public Boolean findById(@PathVariable Long id) throws InterruptedException, IOException {
File file = new File("D:\\sz\\a.txt");
Boolean newFile = file.createNewFile();
return newFile;
}
}
2、使用mkdir创建单级文件夹
package com.controller;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{
@GetMapping("/{id}")
public Boolean findById(@PathVariable Long id) throws InterruptedException, IOException {
File file1 = new File("D:\\sz\\dade");
Boolean newFile2 = file1.mkdir();
return newFile2;
}
}
3、使用mkdirs创建多级文件夹
package com.controller;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{
@GetMapping("/{id}")
public Boolean findById(@PathVariable Long id) throws InterruptedException, IOException {
File file1 = new File("D:\\sz\\dade\\dad\\dad\\dad");
Boolean newFile2 = file1.mkdirs();
return newFile2;
}
}
4、使用delete删除文件和文件夹,只能一层一层删除
package com.controller;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{
@GetMapping("/{id}")
public Boolean findById(@PathVariable Long id) throws InterruptedException, IOException {
File file1 = new File("D:\\sz\\dade");
Boolean newFile2 = file1.delete();
return newFile2;
}
}
5、获得文件和文件夹
package com.controller;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{
@GetMapping("/{id}")
public Boolean findById(@PathVariable Long id) throws InterruptedException, IOException {
File file1 = new File("D:\\sz\\dade");
Boolean newFile2 = file1.delete();
return newFile2;
}
public static void main(String[] arge){
File file1 = new File("D:\\sz\\dade");
File[] files = file1.listFiles();
for(File s : files){
System.out.print(s+"\n");
}
}
}