错误。 BufferedWriter在文件末尾重复行

时间:2015-02-10 09:40:07

标签: java bufferedwriter

我这里有一个错误,不知道发生了什么。

我的类获取一个哈希映射和一个rute的向量,然后将该hashmap写入该路由中的文件。

这是代码:

/* Variables de entrada */
    Vector vecHm = (Vector) context.getAttribute(sVecHashmap);
    String strFileLocation = "" + context.getAttribute(sFileLocation);


    // Inicializamos variables
    FileWriter fileWriter = null;
    BufferedWriter bufferedWriter = null;

    try
    {
        fileWriter = new FileWriter(strFileLocation,true);
        bufferedWriter = new BufferedWriter(fileWriter);
        String linea = "";
        String lineaCabecera = "";


        for (int i=0;i<vecHm.size();i++)
        {
            HashMap hm = (LinkedHashMap) vecHm.get(i);
            Iterator it = hm.entrySet().iterator(); 
            linea = "";
            while (it.hasNext()) 
            {
                Map.Entry pairs = (Map.Entry)it.next();
                if (i==0)
                {
                    if (lineaCabecera.equals("") == false)
                    {
                        lineaCabecera = lineaCabecera + ";";
                    }
                    lineaCabecera = lineaCabecera + (String)pairs.getKey();
                }
                if (linea.equals("") == false)
                {
                    linea = linea + ";";
                }
                linea = linea + (String)pairs.getValue();
                //it.remove(); // avoids a ConcurrentModificationException
            }

            System.out.println("PRF:: HashmapToFile:: Iteracion: " + i + ". Linea: " + linea);

            if (i==0)
            {
                System.out.println("PRF:: Pinto Cabecera. ");
                bufferedWriter.write(lineaCabecera);
                bufferedWriter.newLine();
                //bufferedWriter.write('\n');
            }
            bufferedWriter.write(linea);
            bufferedWriter.newLine();
            //bufferedWriter.write('\n');
        }
    } catch (Exception e)
    {
        e.printStackTrace();
        throw new WFException(" ERROR writing the file");
    } finally
    {
        try
        {
            // Cerramos el fichero
            bufferedWriter.close();
            fileWriter.close();
        } catch (Exception e)
        {
            e.printStackTrace();
            throw new WFException(" ERROR closing the file");
        }
    }

我有一条跟踪显示要在文件中写入的行:

System.out.println(&#34; PRF :: HashmapToFile :: Iteracion:&#34; + i +&#34; .Linea:&#34; + linea);

我看到的日志就是这个(我将只放置最后四次迭代):

PRF :: HashmapToFile :: Iteracion:90。Linea:eufekeptuil; null; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo

PRF :: HashmapToFile :: Iteracion:91。Linea:hwukbzakmfuutrhnfzm; null; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo

PRF :: HashmapToFile :: Iteracion:92。Linea:Securitas Europe; 29-JAN-15; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo

PRF :: HashmapToFile :: Iteracion:93。Linea:Tarifa New 544; 05-FEB-15; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo

但是......当我看到文件时...我最后有这个:

Securitas Europe; 29-JAN-15; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo -OK。完美 -

Tarifa New 544; 05-FEB-15; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo -OK。完美 -

然后:

N-15; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo (重复和未完成的品系)

Tarifa New 60; 15-JAN-15; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo (重复行)

vjvrqgxavk; null; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo; Inactivo (重复行)

又重复了15行。

有任何线索吗?

全部谢谢

1 个答案:

答案 0 :(得分:1)

忘记问题了。文件很好。问题在于下载器。该系统正在为该功能提供更多数据。