第91行我一直收到错误,我无法调试...'nodesBWriter.write()'

时间:2013-10-02 10:15:25

标签: csv bufferedreader bufferedwriter

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

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

我一直在第91行收到错误,目前,notes.txt只显示:

标识,标签,编号 标记,标记,

不是实际的标签。我觉得循环中有些东西是错误的,但我不知道如何纠正这个问题。我还考虑使用此链接中演示的方法重写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();


    }
}

1 个答案:

答案 0 :(得分:1)

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