I want to get file lines count where file is similar to "csv" but it doesn't have ".csv" file extension. I have only filename.
The code i have mentioned here is the function that count file lines of only those files which have name as abc.txt or abc.csv,, but it will not read the file if the extension doesn't have .txt or .csv. That is the code won't the file if file name is only "abc" instead of "abc.csv"
Here's the complete program: There are three class
package file_count_checker;
import java.util.Scanner;
public class File_Count_Checker {
public static void main(String[] args) {
//String s = "C:\\Users\\Nitish.kumar\\Desktop\\Automation\\Input";
String path;
System.out.println("Enter the folder Path : ");
Scanner in1 = new Scanner(System.in);
path = in1.nextLine();
FileTester ftMain = new FileTester();
ftMain.printFileList(path);
}
}
//------------
//Class2:
package file_count_checker;
import java.io.File;
import java.util.*;
public class FileTester {
// FileTester ft = new FileTester();
Map<String, List<String>> map = new HashMap();
FileLineCounter flc = new FileLineCounter();
boolean isFound; boolean isFoundTxt;
public void fileChecker(String folderPath) {
File f = new File(folderPath);
File[] listOfFiles = f.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
String path = file.getParent();
if (map.get(path) == null) {
List<String> fileList = new ArrayList<>();
map.put(path, fileList);
}
List<String> fileList = map.get(path);
fileList.add(file.getName());
} else if (file.isDirectory()) {
String s2 = file.getPath();
fileChecker(s2);
}
}
}
public void printFileList(String path){
fileChecker(path);
for(Map.Entry<String, List<String>> entry : map.entrySet()){
System.out.println("");
System.out.println("Folder Name : "+entry.getKey());
for(String file : entry.getValue()){
//Below code to check whether file is CSV, TXT or with other extension
isFound = file.contains(".csv");
isFoundTxt = file.contains(".txt");
System.out.println(" File Name : "+file);
if(isFound != true){
if(file.contains(".txt") !=true){
System.out.println(" Invalid file: unable to read " );}
}
else {
flc.startCount(entry.getKey()+"\\"+file); }
if(isFoundTxt != true ) { }
else {
flc.startCount(entry.getKey()+"\\"+file);
}
}
}
}
}
//Class 3--------------
package file_count_checker;
import java.io.File;
import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.IOException;
public class FileLineCounter {
int totalLines = 0;
public void startCount(String filePath){
try {
File file =new File(filePath);
FileReader fr = new FileReader(file);
LineNumberReader lnr = new LineNumberReader(fr);
int linenumber = 0;
while (lnr.readLine() != null){
linenumber++;
}
System.out.println(" Total number of lines : " + linenumber);
System.out.println(" Size: "+getFileSizeKiloBytes(file));
lnr.close();
} //close of try block
//Below function to check file size
//File file = new File();
catch (IOException e){
System.out.println("Issues with the file at location:"+ filePath);
}
}
private static String getFileSizeKiloBytes(File file) {
return (double) file.length() / 1024 + " kb";
}
//close of main methods
}