如何在文本文件中读取字符并获取字符长度?

时间:2015-09-15 01:57:31

标签: java file-io

我有一个名为“text_in.txt”的java文本文件,其中包含

IDESOFMARCH

长度为11个字符,如何查找此文件的长度并在H的末尾添加“X”字符,以便将文本分解为每个2个字符的块以供使用用于RSA加密/解密。

示例:ID ES OF MA RC HX

我有一个main()从我的rsa.java文件(文件,私钥,公钥)调用encrypt方法

code.encrypt(files, 17, 2773);

文件包含“text_in.txt”和“text_out.txt”以及一个布尔值为true的文件。

主要是这样称呼:

public void encrypt(FileInOut files, int e, int n) {



//Get the in file length and add a 'X' if uneven character blocks
    if((files.length() % 2) == 1)
      files.write("X");

//Print a 0 in the encrypted file to represent the end of a line
    if(files != char)
      files.write("0");

1 个答案:

答案 0 :(得分:1)

好好借用Reading a plain text file in Java

的答案
String str = new String(Files.readAllBytes(Paths.get("text_in.txt")));
int len = str.length();