如果你的项目所有的文件都被vscode编辑过,并且这项目编辑时间短暂.那么还有激活挽救
这里是我使用java恢复的一个代码.我急着恢复自己的代码,代码简陋,不过可以参考
```java
package cn.cosoc.webscreenshot;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class FileInfoimplements Comparable{
public Stringid;
public long timestamp;
public StringgetId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@Override
public StringtoString() {
return "FileInfo{" +
"id='" +id +'\'' +
", timestamp=" +timestamp +
'}';
}
@Override
public int compareTo(FileInfo o) {
if (this.getTimestamp() - o.getTimestamp() >0) {
return -1;
}
if (this.getTimestamp() - o.getTimestamp() <0) {
return 1;
}
return 0;
}
}
//@SpringBootTest
class ApplicationTests {
@Test
void test ()throws IOException {
File srcFile =new File("/home/hzhzhzh/.config/Code/User/History");
getAllFilePath(srcFile);
}
void getAllFilePath( File srcFile)throws IOException {
//获取给定的File目录下所有文件或者目录下的所有File数组
File[] fileArray = srcFile.listFiles();
for (File file : fileArray){
// 判断File对象是否为目录
if(file.isDirectory()){
String path = file.getPath();
// entries
String entriesPath = path +"/entries.json";
File temp =new File(entriesPath);
// 如果存在
if (temp.exists()) {
JSONReader jsonReader = getJsonReader(entriesPath);
JSONObject jsonObject = jsonReader.readObject(JSONObject.class);
String resource = jsonObject.getString("resource");
if (resource.contains("/home/hzhzhzh/Development/WebProject/web-screenshot-chrome-plugin/")) {
// 获取文件记录
JSONArray entries = jsonObject.getJSONArray("entries");
List fileInfos =new ArrayList<>();
for (int i =0; i < entries.size(); i++) {
JSONObject jsonObject1 = entries.getJSONObject(i);
long timestamp1 = jsonObject1.getLongValue("timestamp");
String id = jsonObject1.getString("id");
FileInfo fileInfo =new FileInfo();
fileInfo.setId(id);
fileInfo.setTimestamp(timestamp1);
fileInfos.add(fileInfo);
}
Collections.sort(fileInfos);
// 获取最新的一个
FileInfo fs = fileInfos.get(0);
// 需要恢复的文件
String restoreF = path +"/" + fs.getId();
// 需要恢复的路径
String restoreP = resource.replace("file:///home/hzhzhzh/Development/WebProject/","/tmp/");
FileUtils.copyFile(new File(restoreF), new File(restoreP));
}
}
}else {
//获取绝对路径输出在控制台
System.out.println(file.getAbsoluteFile());
}
}
}
/**
* 获取JSONReader
* @param srcPath
* @return
*/
private JSONReadergetJsonReader(String srcPath) {
JSONReader jsonReader =null;
try {
FileReader fileReader=new FileReader(srcPath);
jsonReader =new JSONReader(fileReader);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
return jsonReader;
}
}
```