Java 为什么我有98行而不是99行?
我有一个名为json的目录,其中包含99个json文件 这些文件名为:i.json,其中i是从1开始的递增整数 我编写了一些代码,从这些文件中读取一些内容,并将这些内容写入一个txt文件,每个json文件一行Java 为什么我有98行而不是99行?,java,Java,我有一个名为json的目录,其中包含99个json文件 这些文件名为:i.json,其中i是从1开始的递增整数 我编写了一些代码,从这些文件中读取一些内容,并将这些内容写入一个txt文件,每个json文件一行 public static void main(String[] args) throws Exception, IOException, ParseException { String type =""; ArrayList<String&
public static void main(String[] args) throws Exception, IOException, ParseException {
String type ="";
ArrayList<String> sentiment = new ArrayList<String>();
int i= 1;
double score = 0;
File dir = new File("json");
File[] directoryListing = dir.listFiles();
JSONObject jsonObject;
if (directoryListing != null) {
for (File child : directoryListing) {
JSONParser parser = new JSONParser();
try {
jsonObject = (JSONObject) parser.parse(new FileReader("json/sentiment_"+i+".json"));
} catch (FileNotFoundException e) {
i++;
continue;
}
JSONObject doc = (JSONObject) jsonObject.get("docSentiment"); // get the nested object first
type = (String)doc.get("type"); // get a string from the nested object
// CODICE PER PRENDERE ANCHE LO SCORE
if (!(type.equals("neutral"))){
score = (double) doc.get("score");
} else score = 0;
sentiment.add(type+";"+score);
i++;
}
}
PrintWriter out = new PrintWriter("sentiment.txt");
for(String value: sentiment)
out.println(value);
out.close();
}
}
在我的情况下,这是毫无用处的,让我解释一下原因
如前所述,在json文件夹中,有这样的json文件:“情绪_1”、“情绪_2”等等
文件夹中有1000个这样的数字,但不是从1到1000的每个数字都有
如果依赖FileReader(子),则在for循环中,文件的读取顺序不正确(1,2,3,4…)
这是因为对于文件夹中的排序顺序,例如10在2之前,因为顺序是1,10,2,3,4
因此,正如下行选民显然根本不理解的那样,问题似乎并不那么容易
这不是一个简单的循环问题lol。因为这段代码:
try {
jsonObject = (JSONObject) parser.parse(new FileReader("json/sentiment_"+i+".json"));
} catch (FileNotFoundException e) {
i++;
continue;
}
如果您忽略了错误,请改用此选项:
try {
jsonObject = (JSONObject) parser.parse(new FileReader("json/sentiment_"+i+".json"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
我相信您的json文件名格式有误:
try {
jsonObject = (JSONObject) parser.parse(new FileReader("json/sentiment_"+i+".json"));
} catch (FileNotFoundException e) {
i++;
continue;
}
这段代码告诉我们,如果没有找到一个文件,它将被忽略,没有任何控制台反馈
替换为:
try {
jsonObject = (JSONObject) parser.parse(new FileReader("json/sentiment_"+i+".json"));
} catch (FileNotFoundException e) {
System.out.println("Missing json file: " + e.getMessage());
i++;
continue;
}
你会知道发生了什么事
更好的解决方案
当前,您正在文件中循环,但从未使用当前迭代,您使用的是i
变量。文件读取器
可以用文件
而不是文件路径作为字符串实例化:
try {
jsonObject = (JSONObject) parser.parse(new FileReader(child));
} catch (FileNotFoundException e) {
System.out.println("Missing json file: " + e.getMessage());
i++;
continue;
}
捕获并忽略
FileNotFoundException
不是一个好主意。依赖FileNotFoundException的成本相对较高。你应该做一个文件存在检查。这样,您的程序就可以在每次缺少某个对象时进行堆栈跟踪和保存所有内容。您可以显示directoryListing和i的值吗?在try/catch中,您将+1添加到i
,然后再执行一次。你检查过了吗?@tumisma他正在向i添加+1,但他没有再添加,他用更好的解决方案continue
跳到下一次迭代,但有一个编译错误:资源类型JSONParser没有实现java.lang.AutoCloseable。第一个解决方案与我的代码完全一样,只给出98行而不是99行!呜呜,对不起,我以为它是自动关闭的,我把它修好了。我不是说第一个模块将输出99行,我是说它将警告一个命名错误的文件,在执行代码时在控制台上观察,您将被警告一个“找不到文件”
try {
jsonObject = (JSONObject) parser.parse(new FileReader(child));
} catch (FileNotFoundException e) {
System.out.println("Missing json file: " + e.getMessage());
i++;
continue;
}