给你一个例子.import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile;public class Test { public static void main(String[] args) {
try {
int MaxLength = 20;
RandomAccessFile oFile = new RandomAccessFile("1.txt","rw");
if (MaxLength > oFile.length()){ oFile.seek(oFile.length()); }
用它的SetLength()
import java.io.IOException;
import java.io.RandomAccessFile;public class Test { public static void main(String[] args) {
try {
int MaxLength = 20;
RandomAccessFile oFile = new RandomAccessFile("1.txt","rw");
if (MaxLength > oFile.length()){
oFile.seek(oFile.length());
}
oFile.writeChars("123456789012345678901324567980");
oFile.setLength(MaxLength);
oFile.close();
} catch (FileNotFoundException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
System.out.println("File Not Exit.");
} catch (IOException e1) {
// TODO 自動生成された catch ブロック
e1.printStackTrace();
System.out.println("Write Error.");
}
}
}