开发者

How to prefix string to the file at the starting location in a file?

I have file as below.

  rule "IC-86"
    agenda-group "commonATSP"
    dialect "mvel"
    when
        eval($count > 10)
    then
        modify( $a开发者_运维知识库ttribute ){ $imageVersion,$attributes.get(),imageName };
end

I need to prefix the below mentioned string at the top of the file.

import java.lang.Exception;

The output should looks like as below.

  import java.lang.Exception;
  rule "IC-86"
        agenda-group "commonATSP"
        dialect "mvel"
        when
            eval($count > 10)
        then
            modify( $attribute ){ $imageVersion,$attributes.get(),imageName };
    end

Please provide me some pointers to implement the same using Java.


This method prepends a CharSequence to the beginning of a file which is what I think you're looking for.

/**
 * Prepends a string value to the beginning of a file
 * @param prepend The prepended value
 * @param addEol If true, appends an EOL character to the prepend
 * @param fileName The file name to prepend to
 */
public static void prependToFile(CharSequence prepend, boolean addEol, String fileName) {
    if(prepend==null) throw new IllegalArgumentException("Passed prepend was null", new Throwable());
    if(fileName==null) throw new IllegalArgumentException("Passed fileName was null", new Throwable());
    File f = new File(fileName);
    if(!f.exists() || !f.canRead() || !f.canWrite()) throw new IllegalArgumentException("The file [" + fileName + "] is not accessible", new Throwable());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    byte[] buffer = new byte[8096];
    int bytesRead = 0;
    try {
        baos.write(prepend.toString().getBytes());
        if(addEol) {
            baos.write(System.getProperty("line.separator", "\n").getBytes());
        }
        fis = new FileInputStream(f);
        bis = new BufferedInputStream(fis);
        while((bytesRead = bis.read(buffer)) != -1) {
            baos.write(buffer, 0, bytesRead);
        }
        bis.close(); bis = null;
        fis.close(); fis = null;
        fos = new FileOutputStream(f, false);
        bos = new BufferedOutputStream(fos);
        bos.write(baos.toByteArray());          
        bos.close();            
    } catch (Exception e) {
        throw new RuntimeException("Failed to prepend to file [" + fileName + "]", e);
    } finally {
        if(bis!=null) try { bis.close(); } catch (Exception e) {}
        if(fis!=null) try { fis.close(); } catch (Exception e) {}
        if(bos!=null) try { bos.close(); } catch (Exception e) {}
        if(fos!=null) try { fos.close(); } catch (Exception e) {}                       
    }       
}

Example:

public static void main(String[] args) {
    prependToFile("import java.lang.Exception;", true, "/tmp/rule.txt");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜