如果你的項(xiàng)目所有的文件都被vscode編輯過,并且這項(xiàng)目編輯時(shí)間短暫.那么還有激活挽救
這里是我使用java恢復(fù)的一個(gè)代碼.我急著恢復(fù)自己的代碼,代碼簡(jiǎn)陋,不過可以參考
```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數(shù)組
? ? ? ? File[] fileArray = srcFile.listFiles();
? ? ? ? for (File file : fileArray){
// 判斷File對(duì)象是否為目錄
? ? ? ? ? ? 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);
? ? ? ? ? ? ? ? ? ? ? ? // 獲取最新的一個(gè)
? ? ? ? ? ? ? ? ? ? ? ? FileInfo fs = fileInfos.get(0);
? ? ? ? ? ? ? ? ? ? ? ? // 需要恢復(fù)的文件
? ? ? ? ? ? ? ? ? ? ? ? String restoreF = path +"/" + fs.getId();
? ? ? ? ? ? ? ? ? ? ? ? // 需要恢復(fù)的路徑
? ? ? ? ? ? ? ? ? ? ? ? String restoreP = resource.replace("file:///home/hzhzhzh/Development/WebProject/","/tmp/");
? ? ? ? ? ? ? ? ? ? ? ? FileUtils.copyFile(new File(restoreF), new File(restoreP));
? ? ? ? ? ? ? ? ? ? }
}
}else {
//獲取絕對(duì)路徑輸出在控制臺(tái)
? ? ? ? ? ? ? ? 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;
? ? }
}
```