import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FilePerformance {
public static void main(String[] args) {
// Example usage: Replace with your file path
String filePath = "test.txt";
long startTime = System.currentTimeMillis();
long fileSize = getFileSize(filePath);
long endTime = System.currentTimeMillis();
System.out.println("File size: " + fileSize + " bytes");
System.out.println("Time to get file size: " + (endTime - startTime) + " ms");
startTime = System.currentTimeMillis();
try {
readWholeFile(filePath);
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
endTime = System.currentTimeMillis();
System.out.println("Time to read whole file: " + (endTime - startTime) + " ms");
}
private static long getFileSize(String filePath) throws IOException {
File file = new File(filePath);
return file.length();
}
private static void readWholeFile(String filePath) throws IOException {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
byte[] fileContent = fileInputStream.readAllBytes(); // Read entire file content
// Process fileContent here (e.g., analyze, validate)
// For this example, we just read it.
} finally {
if (fileInputStream != null) {
fileInputStream.close(); // Ensure file stream is closed
}
}
}
}
Add your comment