
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - RandomAccessFile setLength(long newLength) method
Description
The Java RandomAccessFile setLength(long newLength) method sets the length of this file.
Declaration
Following is the declaration for java.io.RandomAccessFile.setLength(long newLength) method.
public void setLength(long newLength)
Parameters
newLength − The desired length of the file
Return Value
This method does not return a value.
Exception
IOException − If an I/O error occurs.
Example - Usage of RandomAccessFile setLength(long newLength) method
The following example shows the usage of RandomAccessFile setLength(long newLength) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try { // create a new RandomAccessFile with filename test RandomAccessFile raf = new RandomAccessFile("test.txt", "rw"); // write something in the file raf.writeUTF("Hello World"); // 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); // print the new length System.out.println(raf.length()); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −
ABCDE
Let us compile and run the above program, this will produce the following result −
Hello World 34 30
Example - Truncate a File
The following example shows the usage of RandomAccessFile setLength(long newLength) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try { RandomAccessFile raf = new RandomAccessFile("truncate.dat", "rw"); // Write 10 bytes to the file raf.writeBytes("ABCDEFGHIJ"); // Truncate the file to 5 bytes raf.setLength(5); // Read the truncated content raf.seek(0); byte[] data = new byte[5]; raf.readFully(data); System.out.println("Truncated content: " + new String(data)); // Output: ABCDE raf.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Truncated content: ABCDE
Explanation
setLength(5) cuts the file to 5 bytes, discarding everything after.
Useful when you want to remove content from the end of a file, like shrinking logs or undoing writes.
Example - Extend a File
The following example shows the usage of RandomAccessFile setLength(long newLength) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try { RandomAccessFile raf = new RandomAccessFile("extend.dat", "rw"); // Write 3 bytes to the file raf.writeBytes("XYZ"); // Extend the file to 10 bytes raf.setLength(10); // Reset and read all bytes raf.seek(0); byte[] data = new byte[10]; raf.readFully(data); // Print bytes as characters or placeholder for empty space for (byte b : data) { System.out.print((b == 0 ? '_' : (char) b) + " "); } // Output: X Y Z _ _ _ _ _ _ _ raf.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
X Y Z _ _ _ _ _ _ _
Explanation
setLength(10) increases file size from 3 to 10 bytes.
The new space is filled with null bytes (0).
This is useful for preallocating space or reserving room for future data.