2016-09-26 129 views
-2
  • 我有幾個文件保存在我的本地目錄中。一個視頻文件和一個 xml文件。視頻文件的詳細信息將存儲在xml文件中。For循環中的Java文件迭代

    • 我們正在將視頻從一個系統移動到另一個系統。
      將視頻文件和xml數據從一個系統上傳到另一個系統
      系統,需要檢查其他系統中的視頻標題,並且只有在相同標題不存在的情況下才能上傳 。

    • 這工作正常。但上傳發生了4次,而不是2次 次。請幫忙。

這裏是主代碼:

List<File> videoFiles = new ArrayList<File>(); 
     List<File> xmlFiles = new ArrayList<File>(); 
File[] allVideos = checkVideos(); 
     for(File file:allVideos) { 
      if(file.getName().endsWith("flv")) { 
       videoFiles .add(file); 
      } 
      if(file.getName().endsWith("xml")) { 
       xmlFiles .add(file); 
      } 
     } 

     System.out.println(videoFiles.size()); 
     System.out.println(xmlFiles.size()); 

     processUpload(videoFiles ,xmlFiles); 

這裏是方法:

private static void processUpload(List<File> videoFiles, List<File> xmlFiles) throws ParserConfigurationException, SAXException, IOException, ApiException { 
    NodeList nodes = null; 
    File video= null; 
    File xml = null; 
    String title = null; 
    String localFileTitle = null; 
    Media newMedia = null; 
    for(int i=0;i < videoFiles.size();i++) { 
     System.out.println("videoFiles.getName() ->"+videoFiles.get(i).getName()); 
     video= videoFiles.get(i); 
     for(int j=0;j < xmlFiles.size();j++) { 
      xml = xmlFiles.get(j); 
      System.out.println("xmlFiles.getName() ->"+xmlFiles.get(i).getName()); 
       nodes = parseXml(xml); 
       localFileTitle = processNodes(nodes); 
       title = checkTitles(localFileTitle); 
       newMedia = initiateUploadProcess(flv, title); 
       } 
      } 
     } 

private static NodeList parseXml(File xmlFile) throws ParserConfigurationException, SAXException, IOException { 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
    Document doc = dBuilder.parse(xmlFile); 
    doc.getDocumentElement().normalize(); 
    //System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
    NodeList nList = doc.getElementsByTagName("video"); 
    return nList; 
} 

private static String processNodes(NodeList nodes) { 
String fileTitle = null; 
    if(nodes.getLength() >= 1) { 
     for (int temp = 0; temp < nodes.getLength(); temp++) { 
      Node nNode = nodes.item(temp); 
      if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
       Element eElement = (Element) nNode; 
       fileTitle = eElement.getElementsByTagName("title").item(0).getTextContent(); 
       if(fileTitle != null) { 
       //System.out.println("Local File Title ------>"+fileTitle); 
       } 
      } 
     } 
    } 
    return fileTitle; 
} 

private static String checkTitles(String localTitle) throws ApiException { 
    String title = null; 
    MediaList mediaResponse = fetchVideos(caID); 
    if(mediaResponse.totalCount >= 1) { 
     for(MediaEntry media:mediaResponse.objects) { 
      if(!localTitle.equals(media.name)) { 
       System.out.println("Titles are not same. Need to return"); 
       title = localTitle; 
       } 
      } 
     } 
    return title ; 
} 

private static MediaEntry initiateUploadProcess(File videoFile, 
     String localFileTitle) throws ApiException, ParserConfigurationException, SAXException, IOException { 
    UploadToken ktoken = null; 
    UploadMedia entry = null; 
    MediaEntry mediaEntry = null; 
    ktoken = generateToken(); 
    if (ktoken != null) { 
     //System.out.println("ktoken.id ----------->" + ktoken.id); 
     if (ktoken.id != null) { 
      uploadToken(ktoken.id, flvFile); 
      entry = uploadMediaToChannel(categoryID, categoryName, localFileTitle); 
      if (entry.id != null) { 
       System.out.println("entry.id ------->" + entry.id); 
       mediaEntry = addMediaContent(ktoken.id, entry.id); 
       } 
      } 
     } 
    return mediaEntry; 
} 

這裏是輸出:

videoFiles.getName() ->22701846_91167469.flv 
xmlFiles.getName() ->22701846_91167469.xml 
Titles are not same. Need to return 
Titles are not same. Need to return 
video.id ------->0_50wh1m4p 
xmlFiles.getName() ->22701846_91167469.xml 
Titles are not same. Need to return 
Titles are not same. Need to return 
video.id ------->0_79v605ue 
videoFiles.getName() ->22701846_91477939.flv 
xmlFiles.getName() ->22701846_91477939.xml 
Titles are not same. Need to return 
Titles are not same. Need to return 
video.id ------->0_0kihent1 
xmlFiles.getName() ->22701846_91477939.xml 
Titles are not same. Need to return 
Titles are not same. Need to return 
Titles are not same. Need to return 
video.id ------->0_miogft0i 
+0

這是2016年你爲什麼不使用新的文件API? – fge

回答

0

在PS中eudo-code,你有這樣的兩個循環:

for (i in 1,2) 
    for (j in 1, 2) 
    ... 
    upload 

你驚訝,你有4(2×2)上傳?

嘗試類似:

for (i in 1,2) 
... fetch video file info 
... fetch xml file info 
upload 

,而不是!

0

做嵌套for循環不是很有效,但我認爲問題出在您的checkVideos()方法(未在問題中列出)並且它返回重複的文件對象。

編輯:在您打印的xml文件是使用「i」的變量(從外環)行

+0

不,我打印並看到只有4個文件在這個文件夾中。 2個視頻和2個xml文件。 – Sakuntala