there is a task to count the number of Latin letters in a text file, I only managed to translate the letters in upper case.

static void copyToUpperCase(String fileNameFrom, String fileNameTo) { try { FileReader e = new FileReader(fileNameFrom); BufferedReader br = new BufferedReader(e); PrintWriter out = new PrintWriter(new FileWriter(fileNameTo)); String s; while((s = br.readLine()) != null) { out.write(s.toUpperCase() + "\n"); } out.close(); e.close(); br.close(); } catch (Exception var6) { var6.printStackTrace(); } } public static void main(String[] args) { copyToUpperCase("E:\\Java\\14 лабораторная\\src\\com\\example\\Lab14\\file.txt", "E:\\Java\\14 лабораторная\\mahesh.txt"); } 

    2 answers 2

    Use the String class's getBytes() method, it converts letters to bytes using the default encoding.

    Further, using the encoding table, determine the range of bytes in which the Latin letters are located and cycle through the file. In the loop, sequentially read the bytes from the file and match them with a range of Latin characters.

      In this case, you can do without explicit conversion to bytes, and determine whether each element of the line belongs to the range a - z and A - Z.

      (The ASCII table contains Latin letters from a to z, and letters A to Z are also consecutive)

      To check whether the letter belongs to the gap, you need to check that:

       (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z')) 

      where x is a character from a file

      If this condition is true, then the symbol belongs to {a, b, c, ..., x, y, z, A, B, C, ..., X, Y, Z}; if the condition is false, then no.

      It is better to read the contents of the file character by character.

      (for example, as described in this question): Reading from files character-by-character into JAVA array