【发布时间】:2018-11-08 12:34:44
【问题描述】:
我想获取文件类似于“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
}
【问题讨论】:
-
对不起——什么??
-
请阅读“如何创建minimal reproducible example”。然后使用edit 链接改进您的问题(不要通过 cmets 添加更多信息)。否则我们无法回答您的问题并为您提供帮助。
-
Like:向我们展示一些示例文件名 (?)。文件内容?抱歉,但您希望其他人花时间帮助您。但你必须让我们这样做。因此,请您花费所有需要的时间来提出一个可以回答的明确问题....
-
文件扩展名与文件行数有什么关系?
-
只需删除测试文件扩展名的几行。有什么问题?