[英]The source attachment does not contain the source for the file JSON.class
This there anything wrong with my code? 我的代码有什么问题吗? I'm new to Java and i'm trying to import a file into MongoDB. 我是Java新手,正在尝试将文件导入MongoDB。 However there is a error that i have no idea what is it. 但是有一个错误,我不知道这是什么。 I am using Eclipse. 我正在使用Eclipse。
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.util.JSON;
import com.mongodb.util.JSONParseException;
public class readwrite {
public static void main(String[] args) throws FileNotFoundException,IOException,JSONParseException{
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("actualdata");
DBCollection collection = db.getCollection("metadata");
String line = null;
StringBuilder sb = new StringBuilder();
try {
FileInputStream fstream = null;
try {
fstream = new FileInputStream("/home/Output/json1-100000-all");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File does not exist, exiting");
return;
}
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(fstream));
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
DBObject dbObject;
sb.append(dbObject = (DBObject) JSON.parse(bufferedReader.readLine()));
collection.insert(dbObject);
DBCursor cursorDoc = collection.find();
while (cursorDoc.hasNext()) {
System.out.println(cursorDoc.next());
}
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file");
}
catch(IOException ex) {
System.out.println(
"Error reading file");
}
}
}
This is the error that is displayed 这是显示的错误
[
Exception in thread "main" com.mongodb.util.JSONParseException:
{
^
at com.mongodb.util.JSONParser.read(JSON.java:272)
at com.mongodb.util.JSONParser.parseObject(JSON.java:230)
at com.mongodb.util.JSONParser.parse(JSON.java:195)
at com.mongodb.util.JSONParser.parse(JSON.java:145)
at com.mongodb.util.JSON.parse(JSON.java:81)
at com.mongodb.util.JSON.parse(JSON.java:66)
at readwrite.main(readwrite.java:45)
It show me this error when i clicked on at com.mongodb.util.JSONParser.read(JSON.java:272) where it says that the Source is not found. 当我单击com.mongodb.util.JSONParser.read(JSON.java:272)时,它向我显示此错误,其中指出未找到源。 The source attachment does not contain the source for the file JSON.class. 源附件不包含文件JSON.class的源。 I can print the output of BufferedReader if i did not included the conversion of DBObject. 如果我不包括DBObject的转换,则可以打印BufferedReader的输出。 Thanks in advance! 提前致谢!
1) Didn't you mean to write JSON.parse(line) instead of JSON.parse(bufferedReader.readLine())) ? 1)您不是要写JSON.parse(line)而不是JSON.parse(bufferedReader.readLine()))吗? This might cause it to try and parse 'null' at the last iteration 这可能会导致它在最后一次迭代时尝试解析“ null”
2) If that doesn't help, could you get the exact string value of 'line' on the failed iteration? 2)如果这样做没有帮助,您能否在失败的迭代中获得准确的'line'字符串值? (this should be easy using debugger or simple printing to system out) (使用调试器或简单地打印到系统中应该很容易)
Regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.