2014-03-13 2 views
0

내 코드에 문제가 있습니까? 저는 Java에 익숙하지 않고 MongoDB로 파일을 가져 오려고합니다. 그러나 나는 그것이 무엇인지 전혀 모른다는 오류가 있습니다. Eclipse를 사용하고 있습니다. 소스 첨부 파일에 JSON.class 파일의 소스가 없습니다.

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");     
      } 
    } 


} 

내가 그것을 말한다 com.mongodb.util.JSONParser.read (JSON.java:272)에서 클릭했을 때

[ 
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) 

표시되는 오류 그것은 나에게이 오류가 표시입니다 소스가 발견되지 않습니다. 원본 첨부 파일에 JSON.class 파일의 소스가 없습니다. DBObject의 변환을 포함하지 않은 경우 BufferedReader의 출력을 인쇄 할 수 있습니다. 미리 감사드립니다!

+0

(이 밖으로 시스템에 디버거 또는 간단한 인쇄를 사용하여 쉽게해야합니다). –

+0

내가 어떻게 할 것이라고 제안 했습니까? – user3414091

+0

대신 http://codereview.stackexchange.com/에 코드를 게시 할 가치가 있습니까? – Trisha

답변

0

1) JSON.parse (bufferedReader.readLine()) 대신 JSON.parse (줄) 을 쓰지 않으셨습니까? 마지막 반복에서 'null'을 구문 분석하려고 시도 할 수 있습니다.

2) 도움이되지 않으면 실패한 반복에서 '줄'의 정확한 문자열 값을 얻을 수 있습니까? 당신이 "분석"하려고하는 JSON은 한 줄에 모든 것을 나타납니다

감사

+0

안녕하세요, Java에 익숙하지 않아 죄송합니다. 디버거 나 간단한 인쇄로 시스템을 이해한다는 의미의 예가 있습니까? DBObject dbObject를 지우면 인쇄 할 수 있습니다. sb.append (dbObject = (DBObject) JSON.parse (bufferedReader.readLine())); collection.insert (dbObject); – user3414091

+0

답장을 늦게 드려서 죄송합니다. 지금까지 해결 되었기를 바랍니다 :) –

+0

하지만 "System.out.println (line);" 기존의 "sb.append (dbObject = (DBObject) JSON.parse (...") 직전에. 행을 구문 분석 할 수 없다는 이유로 불평하므로 해당 행의 내용을보고 싶습니다. –

관련 문제