Java RandomAccessFile .setLength (long newLength)
Syntax
RandomAccessFile.setLength(long newLength) has the following syntax.
public void setLength(long newLength) throws IOException
Example
In the following code shows how to use RandomAccessFile.setLength(long newLength) method.
/*from www.j a v a2 s . c o m*/
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");
raf.writeUTF("Hello World from java2s.com");
// set the file pointer at 0 position
raf.seek(0);
// print the string
System.out.println(raf.readUTF());
// print current length
System.out.println(raf.length());
// set the file length to 30
raf.setLength(30);
System.out.println(raf.length());
raf.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Home »
Java Tutorial »
java.io »
Java Tutorial »
java.io »