java实现将txt文件中最后一条json数据读取出来
时间: 2023-09-06 12:08:40 浏览: 49
以下是一个Java示例代码,可以将txt文件中的最后一个JSON数据读取出来:
```java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.json.JSONObject;
public class ReadLastJSONFromFile {
public static void main(String[] args) {
File file = new File("test.txt"); // txt文件路径
BufferedReader reader = null;
JSONObject lastJson = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
lastJson = new JSONObject(tempString); // 将当前行转换成JSON对象
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("最后一条JSON数据为:" + lastJson.toString());
}
}
```
注意,这里假设每一行都是一个完整的JSON数据,如果不是,需要根据实际情况进行处理。