
- 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 length() method
Description
The Java RandomAccessFile length() method returns the length of this file.
length() method −
Returns the length of the file in bytes.
Useful for checking how much data the file contains.
Often used in file processing, truncation, or data validation tasks.
Declaration
Following is the declaration for java.io.RandomAccessFile.length() method.
public long length()
Parameters
NA
Return Value
This method returns the length of this file, measured in bytes.
Exception
IOException> − If an I/O error occurs.
Example - Usage of RandomAccessFile length() method
The following example shows the usage of RandomAccessFile length() method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.RandomAccessFile; 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); // read and print the contents of the file System.out.println("" + raf.readUTF()); // print the length of the file System.out.println("" + raf.length()); // write something more in the file raf.writeUTF("This is an example"); // print the length of the file 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 13 33
Example - Checking file length after writing data
The following example shows the usage of RandomAccessFile length() 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("length1.txt", "rw")) { raf.writeUTF("Hello, World!"); // Write a UTF string long fileLength = raf.length(); // Get length of file System.out.println("Length of file in bytes: " + fileLength); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Length of file in bytes: 15
Explanation
Writes a UTF string to the file.
Calls length() to retrieve the number of bytes written.
UTF strings contain metadata, so the length will be more than the character count.
Example - Using length() to loop through a file
The following example shows the usage of RandomAccessFile length() 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("length2.txt", "rw")) { raf.writeBytes("ABCDEFGH"); // 8 bytes written raf.seek(0); // Move pointer to start long len = raf.length(); // Get total length System.out.println("Reading file content:"); for (int i = 0; i < len; i++) { char ch = (char) raf.readByte(); System.out.print(ch + " "); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Reading file content: A B C D E F G H
Explanation
Writes 8 ASCII characters to the file.
Uses length() to determine how many bytes to read.
Reads each byte using a loop and prints the characters.