import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.ClassUtils;
import net.sf.json.JSONObject;
@SpringBootApplication
public class RunApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(RunApplication.class, args);
// 服务器存文件
JSONObject jObject2 = new JSONObject();
jObject2.put("missionStatus", "一号房间");
jObject2.put("destination", "destination");
jObject2.put("arrived", "arrived");
jObject2.put("vin", "vin");
// 获取项目classes/static的地址
String realPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath() + "/file/";
String fileNameString = "missionStatus.txt";
File savedFile = new File(realPath, fileNameString);
try {
FileUtils.writeStringToFile(savedFile, jObject2.toString(), Charset.defaultCharset());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//取文件
String nullpath2 = ClassUtils.getDefaultClassLoader().getResource("static").getPath()
+ "/file/missionStatus.txt";
BufferedReader br2 = new BufferedReader(new FileReader(nullpath2));
String str2 = null;
while ((str2 = br2.readLine()) != null) {
JSONObject jsonObject = JSONObject.fromObject(str2);
System.out.println("服务器读取文件" + jsonObject.toString());
System.out.println("服务器读取文件" + str2);// 这里是读取到的数据,一行一行读取 } }}
}
}
在项目中可能找不到这个文件,但是找到项目编译后的内容,可以看到这个文本