Hi, I actually wrote the code of the program that does the following - the user writes the * .txt file path, the program calculates how many lines in the document, displays the number of lines on the screen + creates a * .txt file in which also writes the number of lines. Here is the code, use:
public class GetStrings { public static void main(String[] args) { System.out.println("Введите абсолютный путь к файлу: "); Scanner scanner = new Scanner(System.in); String inputValue = scanner.next(); String n = inputValue; try { File myFile = new File(n); FileReader fileReader = new FileReader(myFile); LineNumberReader lineNumberReader = new LineNumberReader(fileReader); int lineNumber = 0; while (lineNumberReader.readLine() != null) { lineNumber++; } System.out.println(lineNumber); lineNumberReader.close(); String lineNumber1 = String.valueOf(lineNumber); File newFile = new File("d:\\myFile.txt"); FileWriter fileWriter = new FileWriter(newFile); fileWriter.write(lineNumber1 + " Строк в файле: " + n); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } And now I decided to make the user write the directory, the program looked at the directory for the presence of the * .txt extension files and read the lines in each of the documents, display all the values on the screen and also write them to the new * .txt file. I wrote the code to read files in a directory, but I cannot pull out every variable in the file in a directory to count the number of lines. Here is the time, but how to connect it, help)):
public class Blabla { public static void main(String[] args) { System.out.println("Введите абсолютный путь: "); Scanner scanner = new Scanner(System.in); String inputValue = scanner.next(); String n = inputValue; File folder = new File(n); final String[] extension = {".txt"}; String[] files = folder.list(new FilenameFilter() { @Override public boolean accept(File folder, String name) { for(String ext : extension) if(name.toLowerCase().endsWith(ext)) return true; return false; } }); for(String fileName : files) try { FileReader fileReader = new FileReader(fileName); LineNumberReader lineNumberReader = new LineNumberReader(fileReader); int lineNumber = 0; while (lineNumberReader.readLine() != null) { lineNumber++; } System.out.println(lineNumber); lineNumberReader.close(); String lineNumber1 = String.valueOf(lineNumber); File newFile = new File("d:\\myFile.txt"); FileWriter fileWriter = new FileWriter(newFile); fileWriter.write(lineNumber1 + " Строк в файле: " + n); fileWriter.close(); System.out.println("File: " + fileName); } catch (IOException e) { e.printStackTrace(); } } }