-5

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
}
Nitish K
  • 51
  • 1
  • 1
  • 6
  • 2
    Sorry -- what?? – Nicholas K Nov 08 '18 at 12:35
  • Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. – GhostCat Nov 08 '18 at 12:37
  • Like: show us some example file names (?). Of file contents? Sorry, but you want others to spend their time to help you. But you have to enable us to do so. So you please spend all the time required to come up with a clear question that can be answered.... – GhostCat Nov 08 '18 at 12:38
  • 1
    What has the file extension to do with the number of lines in the file? – Henry Nov 08 '18 at 12:39
  • 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" – Nitish K Nov 08 '18 at 12:41
  • The code you posted in the question has no dependency on file extension at all. – Henry Nov 08 '18 at 12:44
  • @Henry Pls chck, i have added complete code – Nitish K Nov 08 '18 at 12:50
  • 2
    Just remove the few lines testing the file extension. What's the problem? – Henry Nov 08 '18 at 13:02

1 Answers1

0

If you want a one-liner, you can use the readAllLines method.

Files.readAllLines(java.nio.file.Path).size()

Otherwise you might want to see this question.

Jakg
  • 922
  • 12
  • 39