/**
* WriteLine.java
*
* Copyright 2012 Niolex, Inc.
*
* Niolex licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.apache.niolex.common.file;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* @author <a href="mailto:xiejiyun@gmail.com">Xie, Jiyun</a>
* @version 1.0.0, $Date: 2012-12-24$
*/
public class WriteLine {
/**
* A方法追加文件:使用RandomAccessFile
*/
public static void appendMethodA(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* B方法追加文件:使用FileWriter
*/
public static void appendMethodB(String fileName, String content) {
try {
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String s = "D:\\home\\tmp\\" + System.nanoTime();
long in, out;
in = System.currentTimeMillis();
for (int i = 0; i < 2000; ++ i) {
appendMethodA(s, "This is good!\r\n");
}
out = System.currentTimeMillis();
System.out.println("使用RandomAccessFile " + (out - in));
in = System.currentTimeMillis();
for (int i = 0; i < 2000; ++ i) {
appendMethodB(s, "This is good!\r\n");
}
out = System.currentTimeMillis();
System.out.println("使用FileWriter " + (out - in));
}
}