开发者

I/O operations on file

开发者 https://www.devze.com 2023-02-18 12:00 出处:网络
I have a requirement, for example, there will be number of .txt files in one loation c:\\onelocation. I want to write the content to another location in txt format. This part is pretty easy and straig

I have a requirement, for example, there will be number of .txt files in one loation c:\onelocation. I want to write the content to another location in txt format. This part is pretty easy and straight forward. But there is speed breaker here.

There will be time interval take 120 seconds. Read the files from above location and write it to another files with formate txt till 120secs an开发者_Go百科d save the file with name as timestamp.

After 120sec create one more files with that timestamp but we have to read the files were cursor left in previous file.

Please can you suggest any ideas, if code is provided that would be also appreciable.

Thanks Damu.


How about this? A writer that automatically changes where it is writing two every 120 seconds.

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);
        }
    }
}


Use RamdomAccessFile in java to move the cursor within the file.

Before start copying check the file modification/creation(in case of new files) time, if less than 2 mins then only start copying or else skip it.

Keep a counter of no.of bytes/lines read for each file. move the cursor to that position and read it from there.


You can duplicate the file rather than using reading and writing operations.

sample code:

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


File io is simple in java, here is an example I found on the web of copying a file to another file.

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

精彩评论

暂无评论...
验证码 换一张
取 消