Created
August 29, 2017 18:43
-
-
Save javamultiplex/76544c9116219f30faab1c0ed5f9fd01 to your computer and use it in GitHub Desktop.
File Handling Problem 13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.FileHandling; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Scanner; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category File Handling | |
* @problem How to find length of given file in Java? | |
* | |
*/ | |
public class LengthFinder { | |
public static void main(String[] args) throws IOException { | |
Scanner input = null; | |
try { | |
input = new Scanner(System.in); | |
System.out.println("Enter file name with extension : "); | |
String fileName = input.nextLine(); | |
if (isValidFileName(fileName)) { | |
// File is present in current working directory. | |
File file = new File(fileName); | |
long length = file.length(); | |
System.out.println("Length of file is : " + length + " bytes."); | |
} else { | |
System.out.println("File name is invalid."); | |
} | |
} finally { | |
if (input != null) { | |
input.close(); | |
} | |
} | |
} | |
private static boolean isValidFileName(String fileName) { | |
// Regular expression for validating file name. | |
String pattern = "^.+\\..+$"; | |
boolean result = false; | |
if (fileName.matches(pattern)) { | |
result = true; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment