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(); } } } 
  • What is manifested weakness? - Roman C
  • @RomanC I can not pull out the variables of each file, to hold over them the function of counting the number of lines - Hate Fate
  • declare the class variable and put your files in there, no matter what the counting function is, it didn’t write it anyway. - Roman C
  • @RomanC I wrote the counting function. But how can I pull out every element that finds it in the files variable and perform an operation on it I can’t understand .. - Hate Fate
  • an element must have a type, without understanding this you will fail - Roman C

2 answers 2

It looks like a learning task, so I will propose a verbal algorithm:

  1. We receive the address from the user
  2. Check whether this directory (File.isDirectory)
  3. If this is a folder, then we take all its folders and go through them. At each attached file we check: 3.1. If the file is a directory, recursively return to the beginning of p.3 3.2. If the file is a file, check the extension, and if txt, then put in the sheet
  4. At output p3 we get a list of files with the extension txt
  5. Go through all the txt files and count the lines
  6. Print the number of lines.

If you dig a little, you can use Files.walkFileTree (), which will do points 1-3 for you.

  • Why did you write down on points what I wrote in the description and in the comments below? - Hate Fate
 import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.FilenameFilter; import java.io.IOException; import java.io.LineNumberReader; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class Blabla { public static void main(String[] args) { System.out.println("Введите абсолютный путь: "); Scanner scanner = new Scanner(System.in); String inputValue = scanner.next(); File folder = new File(inputValue); final String[] extension = {".txt"}; File[] files = folder.listFiles(new FilenameFilter() { @Override public boolean accept(File folder, String name) { for (String ext : extension) { if (name.toLowerCase().endsWith(ext)) { return true; } } return false; } }); StringBuilder sb = new StringBuilder(); for (File fileName : files) { try { FileReader fileReader = new FileReader(fileName); LineNumberReader lineNumberReader = new LineNumberReader(fileReader); int lineNumber = 0; while (lineNumberReader.readLine() != null) { lineNumber++; } lineNumberReader.close(); sb.append(lineNumber).append(" Строк в файле: ").append(fileName).append("\r\n"); System.out.println(lineNumber); System.out.println("File: " + fileName); } catch (IOException e) { Logger.getLogger(Blabla.class.getName()).log(Level.SEVERE, null, e); } } File newFile = new File("d:\\myFile.txt"); try { FileWriter fileWriter = new FileWriter(newFile); fileWriter.write(sb.toString()); fileWriter.close(); } catch (IOException ex) { Logger.getLogger(Blabla.class.getName()).log(Level.SEVERE, null, ex); } } } 
  • if you understand your problem correctly, the solution looks like this - Dmitriy
  • @ 269255 exactly as I wanted, thanks. But here the problem is that the file is constantly being rewritten, so it only records the last value, not all files - Hate Fate
  • What file is constantly rewritten? I ask in more detail - Dmitry
  • @ 269255 myFile.txt, as I understand it, it is just re-created for each file, and since the name myFile.txt is the same, it just overwrites, well, it stops at the last value - Hate Fate
  • Well, of course, if you turn FileWriter in a loop, along with a collection of your files, write a new file each time ... you need to take it out of the loop and everything is ok. code rewrote - Dmitriy