Java-从文件读取第二行

时间:2018-11-12 13:12:30

标签: java

我有一个从文件读取的构造函数,但是它每隔一行读取一次。我该如何解决?我希望它阅读每一行。 我已经写了一个DataFrame类。如果您需要更多详细信息,请写。

 public DataFrame(String filename, ArrayList<Class<? extends Value>> dataTypes, boolean header){

    myDatabase = new HashMap<String, ArrayList<Value>>();
    colType = new HashMap<String, Class<? extends Value>>();
    colNames = new String[dataTypes.size()];
    dTypes = new ArrayList<>();
    String [] colHeader;
    String [] values;

    try{
        System.out.println(filename);
        FileInputStream fstream = new FileInputStream(filename);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

        String strLine;

        strLine = br.readLine();
        System.out.println(strLine);

        colHeader = strLine.split(",");



        for(int i = 0 ; i < dataTypes.size() ; i++){

            ArrayList<Value> tempList = new ArrayList<Value>();

            // Initializing database: ColumnName --> ArrayList<Object>
            myDatabase.put(colHeader[i], tempList);

            // Creating mapping: ColumnName --> DataType
            colType.put(colHeader[i], dataTypes.get(i));

            // Creating columnNames table
            colNames[i] = colHeader[i];

            // Creating dataTypes table
            dTypes.add(dataTypes.get(i));
        }


        //  Read File Line By Line
        int counter = 0;
        while ((strLine = br.readLine()) != null) {
            strLine = br.readLine();
            values = strLine.split(",");

            Map<String, Value> myNewRow = new HashMap<String, Value>();

            for(int i = 0 ; i < values.length ; i++){


                if(dTypes.get(i).equals(Integer.class)){
                    Value obj = new Integer(parseInt(values[i]));
                    myNewRow.put(colNames[i], obj);
                } else if (dTypes.get(i).equals(Double.class)){
                    Value obj = new Double(parseDouble(values[i]));
                    myNewRow.put(colNames[i], obj);
                } else if (dTypes.get(i).equals(Float.class)){
                    Value obj = new Float(parseFloat(values[i]));
                    myNewRow.put(colNames[i], obj);
                } else if (dTypes.get(i).equals(Str.class)){
                    Value obj = new Str(values[i]);
                    myNewRow.put(colNames[i], obj);
                } else if (dTypes.get(i).equals(DateTime.class)){
                    DateTime d = new DateTime();
                    DateTime obj = (DateTime) d.create(values[i]);
                    myNewRow.put(colNames[i], obj);

                }

            }

            this.insertRow(myNewRow);

            counter++;

        }



        br.close();
    }
    catch(FileNotFoundException exn){
        System.out.println("File not found");
    }
    catch(IOException ioex){
        System.out.println("Reading exception");
    }

}

1 个答案:

答案 0 :(得分:2)

您为每次循环迭代两次调用br.readLine()

while ((strLine = br.readLine()) != null) {
        strLine = br.readLine();
...

因此,您正在读取所有行,但是第二次调用br.readLine()时,您会“丢失”其他所有行。

删除第二个br.readLine(),它应该可以工作。