2015-03-25 77 views
0

我有一個由豎線分隔的文本文件。 我想創建一個新的文本文件(逗號分隔)。 我在下面只寫了一行文本文件! 但System.Out.PrintIn打印到控制檯的所有行!輸出文件僅添加一行

這是我的代碼。

private void doImport(String sfile,String outfile, String gl, int period, String glacc,String contral) throws FileNotFoundException 
{ 
    //java.util.List<Exports> pp= new ArrayList<Exports>(); 
    // imported data date format 
    SimpleDateFormat fm =new SimpleDateFormat("dd-MMM-yy"); 
    // output file date format 
    SimpleDateFormat fm1 =new SimpleDateFormat("dd-MM-yyyy"); 
    // file with data i want to change to new format 
    File file = new File(sfile); 
    // if file null?...add code in calling method to exit 
    // first line headers...counter to track that 
    int count=0; 
    FileReader f= new FileReader(file); 
    BufferedReader r = new BufferedReader(f); 
    String l =null; 
    try { 
     while((l=r.readLine()) != null) 
     { 
      count=count+1; 
      // skip first line 

      if (count>1) 
      { 
       // split data on | character 
      String tokenizer="\\|"; 
      String[] s= l.split(tokenizer); 
      // import only for references starting with f 
      if(s[0].trim().startsWith("F")) 
      { 
       Date d= null; 
       d= fm.parse(s[6].trim()); 
       Exports p= new Exports(); 
       p.setPeriod(period); 
       p.setExchangerate(1); 
       p.setReference(s[0].trim()); 
       p.setGlcode(gl); 
       p.setDate(fm1.format(d)); 
       p.setDescription(s[2].trim()); 
       p.setGlaccount(glacc); 
       p.setContral(contral); 
       p.setEmpty1(" "); 
       p.setEmpty2(" "); 
       p.setAmount(Double.parseDouble(s[9].trim())); 
       p.setHomeamount(Double.parseDouble(s[9].trim())); 
       p.setZero1(0); 
       p.setZero2(0); 
       p.setZ1(0); 
       p.setZ2(0); 
       p.setZ3(0); 
       p.setOne(1); 
       p.setEftnumber((s[5].trim())); 
       try 
        { 

        File f1 = new File(outfile); 
        if (f1.exists()) { 
        // kill 
        f1.delete(); 
        // create it then! 
        f1.createNewFile(); 
        } 

        FileWriter writer= new FileWriter(f1); 
        BufferedWriter b= new BufferedWriter(writer); 

        b.write(p.toString()); 
        b.newLine(); 
        b.close(); 


      }catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      System.out.println(p.toString()); 
      } 
      } 

     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ParseException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

} 

爲什麼我只能添加一行?

Ronald

回答

1

在這段代碼

   { 

       File f1 = new File(outfile); 
       if (f1.exists()) { 
       // kill 
       f1.delete(); 
       // create it then! 
       f1.createNewFile(); 
       } 

       FileWriter writer= new FileWriter(f1); 
       BufferedWriter b= new BufferedWriter(writer); 

       b.write(p.toString()); 
       b.newLine(); 
       b.close(); 


     } 

要刪除一個爲您創造循環的每個迭代一個新的文件。

你可以在同一時間和地點你的輸入文件做創建你的輸出文件,流和流

File file = new File(sfile); 
FileReader f= new FileReader(file); 
BufferedReader r = new BufferedReader(f); 

File f1 = new File(outfile); 
if (f1.exists()) { 
    // kill 
    f1.delete(); 
    // create it then! 
    f1.createNewFile(); 
} 

FileWriter writer= new FileWriter(f1); 
BufferedWriter b= new BufferedWriter(writer); 
2

您必須在循環前創建一次輸出文件。不在循環中。

private void doImport(String sfile,String outfile, String gl, int period, String glacc,String contral) throws FileNotFoundException 
{ 
    //java.util.List<Exports> pp= new ArrayList<Exports>(); 
    // imported data date format 
    SimpleDateFormat fm =new SimpleDateFormat("dd-MMM-yy"); 
    // output file date format 
    SimpleDateFormat fm1 =new SimpleDateFormat("dd-MM-yyyy"); 
    // file with data i want to change to new format 
    File file = new File(sfile); 
    // if file null?...add code in calling method to exit 
    // first line headers...counter to track that 
    int count=0; 
    FileReader f= new FileReader(file); 
    BufferedReader r = new BufferedReader(f); 
    String l =null; 
    try { 
      File f1 = new File(outfile); 
      if (f1.exists()) { 
        // kill 
        f1.delete(); 
        // create it then! 
        f1.createNewFile(); 
     } 

     while((l=r.readLine()) != null) 
     { 
      count=count+1; 
      // skip first line 

      if (count>1) 
      { 
       // split data on | character 
      String tokenizer="\\|"; 
      String[] s= l.split(tokenizer); 
      // import only for references starting with f 
      if(s[0].trim().startsWith("F")) 
      { 
       Date d= null; 
       d= fm.parse(s[6].trim()); 
       Exports p= new Exports(); 
       p.setPeriod(period); 
       p.setExchangerate(1); 
       p.setReference(s[0].trim()); 
       p.setGlcode(gl); 
       p.setDate(fm1.format(d)); 
       p.setDescription(s[2].trim()); 
       p.setGlaccount(glacc); 
       p.setContral(contral); 
       p.setEmpty1(" "); 
       p.setEmpty2(" "); 
       p.setAmount(Double.parseDouble(s[9].trim())); 
       p.setHomeamount(Double.parseDouble(s[9].trim())); 
       p.setZero1(0); 
       p.setZero2(0); 
       p.setZ1(0); 
       p.setZ2(0); 
       p.setZ3(0); 
       p.setOne(1); 
       p.setEftnumber((s[5].trim())); 

        FileWriter writer= new FileWriter(f1); 
        BufferedWriter b= new BufferedWriter(writer); 

        b.write(p.toString()); 
        b.newLine(); 
        b.close(); 


      }catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      System.out.println(p.toString()); 
      } 
      } 

     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ParseException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

}