如何在 Java 中计算 CSV 文件中没有".csv"扩展名的行?



我想获得文件行数,其中文件类似于"csv",但没有".csv"文件扩展名。我只有文件名。

我在这里提到的代码是一个函数,它只计算那些名为abc.txt或abc.csv的文件的文件行,但如果扩展名没有.txt或.csv,它就不会读取文件。也就是说,如果文件名只有"abc"而不是"abc.csv",代码就不会读取该文件

以下是完整的程序:有三类

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
}

如果您想要一个单行,可以使用readAllLines方法。

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

否则,您可能希望看到这个问题。

相关内容

最新更新