java scanner.nextline()未读取包含259094个字符的长行

gmxoilav  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(310)

我想在java中将xml文件作为字符串读取,以便对其进行加密。
我目前的方法是把它当作一个txt文件。
我的问题是xml文件中的第三行有259094个字符长,由于某种原因,scanner的nextline()方法只能将最多131072个字符读入字符串,而不是整行。下面是读取xml文件的代码,这是我使用的xml文件。

try {
  File myFile = new File(filename);
  Scanner myReader = new Scanner(myFile);
  int lineCount = 0;

  while (myReader.hasNextLine()) {
    if (lineCount > 0) { // To make sure it doesn't append \n before the first line[enter link description here][1]
      data += "\n";
    }
    String temp = myReader.nextLine();
    data += temp;
    lineCount += 1;
  }

  myReader.close();
}
catch (FileNotFoundException e) {
  System.out.println("An error occurred.");
  e.printStackTrace();
}
dfddblmv

dfddblmv1#

你提供的代码在我的系统上运行良好。
但是,如果您的目标是加密文件(不解析它),那么就没有理由将其作为字符串读取。你可以把它当作一个字节流来加密。
下面的代码就是一个例子:

public static void main(String[] args) throws NoSuchAlgorithmException {
            String filename = "/tmp/xml.xml";

            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(256);
            SecretKey secretKey = keygen.generateKey();
            byte[] IV = new byte[16]; //TODO The bytes should be random and different for each file
            GCMParameterSpec gcmSpec = new GCMParameterSpec(128, IV);

            try {
                encryptFile(new File(filename), new File(filename + ".encrypted"), secretKey, gcmSpec);
                decyptFile(new File(filename + ".encrypted"), new File(filename + ".decrypted"), secretKey, gcmSpec);
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        static void encryptFile(File inputFile, File outputFile, SecretKey secretKey, GCMParameterSpec gcmSpec) throws InvalidKeyException, IOException {
            InputStream input = null;
            OutputStream output = null;
            try {
                input = new BufferedInputStream(new FileInputStream(inputFile));
                output = new BufferedOutputStream(new FileOutputStream(outputFile));
                Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");

                cipher.init(Cipher.ENCRYPT_MODE, secretKey, gcmSpec);

                while (input.available() > 0) {
                    byte[] bytes = input.readNBytes(128);
                    output.write(cipher.update(bytes));
                }
                output.write(cipher.doFinal());

            } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
                e.printStackTrace();
                System.exit(1);
            } finally {
                if (input != null) input.close();
                if (output != null) output.close();
            }
        }

        static void decyptFile(File encryptedFile, File outputFile, SecretKey secretKey, GCMParameterSpec gcmSpec) throws InvalidKeyException, IOException {
            InputStream input = null;
            OutputStream output = null;
            try {
                input = new BufferedInputStream(new FileInputStream(encryptedFile));
                output = new BufferedOutputStream(new FileOutputStream(outputFile));
                Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");

                cipher.init(Cipher.DECRYPT_MODE, secretKey, gcmSpec);

                while (input.available() > 0) {
                    byte[] bytes = input.readNBytes(128);
                    output.write(cipher.update(bytes));
                }

                output.write(cipher.doFinal());

            } catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException e) {
                e.printStackTrace();
            } finally {
                if (input != null) input.close();
                if (output != null) output.close();
            }
        }

这将读取一个文件并将输出保存到另一个文件。请注意,为了确保安全,您需要将iv更改为随机值,并对每个文件进行更改(可能是在加密文件的开头保存iv)

相关问题