1

我无法调试这个小应用程序。代码应该是

  • 读取 .csv
  • 从第三类(标签)中选择数据
  • 创建文件以收集照片及其标签之间的节点和链接
  • 将这些标签写入准备在 Gephi 中使用的文件

我在第 91 行不断收到错误消息,目前,notes.txt 仅显示:

Id,标签,编号标签,标签,

不是实际的标签。我认为循环中有问题,但我不知道如何纠正这个问题。我还考虑使用此链接中演示的方法重写 BufferedWriter + Writer 代码:

http://tutorials.jenkov.com/java-io/bufferedwriter.html

但我不确定这是否是这里的问题。如果您对如何(在哪里)寻找解决方案有任何指示,请告诉我。

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Lecture4;

import Lecture3.*;
import com.csvreader.CsvReader;
import com.google.common.collect.HashMultiset;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

/**
 *
 * @author doyinue
 */
public class ReadingACSVFile1 {

    private static String tagsOfTheCurrentPicture;
    private static String oneLine;
    private static String[] myTagsAsAnArray;
    private static char[] firstTag;

    /**
     * @param args the command line argumentsileReader myFileReader = new
     * FileReader(myfile);
     *
     *
     *
     *
     *
     * }
     * }
     *
     */
    @SuppressWarnings("empty-statement")
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here

        String myfile;
        myfile = "D:\\Learning\\EUR - Handling Digital Data\\Resources\\rotterdam_erasmus.csv";

        FileReader myFileReader = new FileReader(myfile);
        BufferedReader myBufferedReader = new BufferedReader(myFileReader);

        //this if for converting the comma into a character this can understand
        String comma = ",";
        char fieldCharacter = comma.charAt(0);

        //csvReader
        CsvReader myReader;
        myReader = new CsvReader(myBufferedReader, fieldCharacter);

        //this if for converting the text into a character this can understand
        String text = "\"";
        char textCharacter = text.charAt(0);

        myReader.setTextQualifier(textCharacter);
        myReader.setUseTextQualifier(true);

        FileWriter nodesWriter = new FileWriter("D:\\Learning\\EUR - Handling Digital Data\\Output\\Lecture4\\nodes.txt");
        BufferedWriter nodesBWriter = new BufferedWriter(nodesWriter);

       // FileWriter connWriter = new FileWriter("D:\\Learning\\EUR - Handling Digital Data\\Output\\Lecture4\\conns.txt");
       // BufferedWriter connBWriter = new BufferedWriter(connWriter);

        //Multiset Tags
        HashMultiset<String> myTags = HashMultiset.create();

        //Multiset Links
        HashMultiset<String> myConnections = HashMultiset.create();




        //start the loop
        while (myReader.readRecord()) {

            String[] values = myReader.getValues();
            String tagsOfImage = values[2];
            String[] myTagsArrayed;
            myTagsArrayed = tagsOfImage.split(",");
            myTags.addAll(Arrays.asList(myTagsArrayed));


            nodesBWriter.write("Id,Label,Number");
            nodesBWriter.newLine();

            for (String tags: myTags.elementSet()){

                nodesBWriter.write(tags);
                nodesBWriter.write(",");
                nodesBWriter.write(tags);
                nodesBWriter.write(",");
                nodesBWriter.write(myTags.count(tags));
                nodesBWriter.newLine();


            }


            nodesBWriter.close();

            Utils usefulTools = new Utils();
            myConnections.addAll(usefulTools.getListOfLinks(myTagsArrayed));



        }


        myBufferedReader.close();


    }
}
4

1 回答 1

1

错误是你关闭nodesBWriter了你的循环。因此,在循环的第二次迭代中,写入失败。nodesBWriter.close();在循环结束后移动。

于 2013-10-03T06:50:08.683 回答