2011-03-21 21 views
1

我有一個要求,例如,在一個地址c:\onelocation將會有.txt文件的數量。我想以txt格式將內容寫入其他位置。這部分非常簡單直接。但是這裏有速度的破壞者。對文件的I/O操作

會有120秒的時間間隔。從上面的位置讀取文件,並用formate txt將其寫入另一個文件,直到120secs,並將名稱保存爲時間戳。
120秒後創建一個文件與該時間戳,但我們必須讀取光標留在前一個文件中的文件。

如果提供的代碼也很明顯,請您提出任何建議。

謝謝達穆。

+0

您的時間要求有多嚴格?例如,有時爲121s寫作是可以接受的嗎? – jmg 2011-03-21 07:16:54

+0

它不應該錯過的時間間隔 – developer 2011-03-21 07:39:47

+0

@jmg我期待你的一些線索 – developer 2011-03-25 09:46:56

回答

6

這個怎麼樣?一位作家,每120秒自動更改一次寫作的位置。

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.Writer; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class TimeBoxedWriter extends Writer { 
    private static DateFormat FORMAT = new SimpleDateFormat("yyyyDDDHHmm"); 

    /** Milliseconds to each time box */ 
    private static final int TIME_BOX = 120000; 


    /** For testing only */ 
    public static void main(String[] args) throws IOException { 
     Writer w = new TimeBoxedWriter(new File("."), "test"); 

     // write one line per second for 500 seconds. 
     for(int i = 0;i < 500;i++) { 
      w.write("testing " + i + "\n"); 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException ie) {} 
     } 
     w.close(); 
    } 

    /** Output folder */ 
    private File dir_; 

    /** Timestamp for current file */ 
    private long stamp_ = 0; 

    /** Stem for output files */ 
    private String stem_; 

    /** Current output writer */ 
    private Writer writer_ = null; 


    /** 
    * Create new output writer 
    * 
    * @param dir 
    *   the output folder 
    * @param stem 
    *   the stem used to generate file names 
    */ 
    public TimeBoxedWriter(File dir, String stem) { 
     dir_ = dir; 
     stem_ = stem; 
    } 


    @Override 
    public void close() throws IOException { 
     synchronized (lock) { 
      if (writer_ != null) { 
       writer_.close(); 
       writer_ = null; 
      } 
     } 
    } 


    @Override 
    public void flush() throws IOException { 
     synchronized (lock) { 
      if (writer_ != null) writer_.flush(); 
     } 
    } 


    private void rollover() throws IOException { 
     synchronized (lock) { 
      long now = System.currentTimeMillis(); 
      if ((stamp_ + TIME_BOX) < now) { 
       if (writer_ != null) { 
        writer_.flush(); 
        writer_.close(); 
       } 
       stamp_ = TIME_BOX * (System.currentTimeMillis()/TIME_BOX); 
       String time = FORMAT.format(new Date(stamp_)); 
       writer_ = new FileWriter(new File(dir_, stem_ + "." + time 
         + ".txt")); 
      } 
     } 
    } 


    @Override 
    public void write(char[] cbuf, int off, int len) throws IOException { 
     synchronized (lock) { 
      rollover(); 
      writer_.write(cbuf, off, len); 
     } 
    } 
} 
+0

+1爲一個聰明的解決方案。或者這應該是-1太聰明的一半;-)? – Raedwald 2011-03-31 12:09:49

0

文件io在java中很簡單,這裏是我在Web上將文件複製到另一個文件的示例。

import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 

public class Copy { 
    public static void main(String[] args) throws IOException { 
    File inputFile = new File("farrago.txt"); 
    File outputFile = new File("outagain.txt"); 

    FileReader in = new FileReader(inputFile); 
    FileWriter out = new FileWriter(outputFile); 
    int c; 

    while ((c = in.read()) != -1) 
     out.write(c); 

    in.close(); 
    out.close(); 
    } 
} 
+0

請讀下一次更勤快的quiestion ...%) – 2011-03-28 14:09:46

1

您可以複製文件而不是使用讀取和寫入操作。

示例代碼:

FileChannel ic = new FileInputStream("<source file location>")).getChannel(); 
FileChannel oc = new FileOutputStream("<destination location>").getChannel(); 
ic.transferTo(0, ic.size(), oc); 
ic.close(); 
oc.close(); 

HTH

+0

+1雖然你忘了時間限制,迭代IC和光標保存。這個解決方案可以很容易地修改,雖然在傳輸輸入文件時不會支持超時。 Ics一次轉移。 – extraneon 2011-03-21 06:43:18

4

使用RamdomAccessFile在java中移動光標文件中。

在開始複製之前,請檢查文件修改/創建(如果是新文件)時間,如果少於2分鐘,則只能開始複製或不跳過它。

保留每個文件讀取的字節數/行數的計數器。將光標移到該位置並從那裏讀取。