For循环中的Java文件迭代

时间:2016-09-26 14:11:49

标签: java file for-loop

  • 我的本​​地目录中保存的文件很少。一个视频文件和一个 xml文件。视频文件详细信息将存储在xml文件中。

    • 我们正在将视频从一个系统移动到另一个系统。
      前 将视频文件和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

2 个答案:

答案 0 :(得分:0)

在伪代码中,你有两个循环:

DrawingItem

你有4(2 x 2)次上传感到惊讶吗?

尝试类似:

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

,而不是!

答案 1 :(得分:0)

执行嵌套for循环效率不高,但我认为问题出在checkVideos()方法中(未在问题中列出),并且它返回了重复的文件对象。

编辑:您打印xml文件的行正在使用&#34; i&#34;变量(来自外循环)