Я пытаюсь запустить класс Java, который считывает некоторые значения из файла JSON. Однако, когда я запускаю его, я получаю сообщение об ошибке:
java.io.FileNotFoundException: \file001.json (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at jsonPractice.main(jsonPractice.java:21)
Код, который я использую:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class jsonPractice {
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
JSONParser parser = new JSONParser();
try
{
Object obj = parser.parse(new FileReader("file001.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
System.out.println(name);
long age = (Long) jsonObject.get("age");
System.out.println(age);
JSONArray msg = (JSONArray) jsonObject.get("messages");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ParseException e) {
e.printStackTrace();
}
}
}
Я обязательно добавил json jar в путь сборки, и файл001.json находится внутри папки в рабочей области проекта.
Спасибо