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



我想在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();
}

您提供的代码在我的系统上运行良好。

但是,如果您的目标是加密一个文件(而不解析它(,那么就没有理由将其读取为字符串。您可以将其视为字节流并对其进行加密。

下面的代码就是一个例子:

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(

最新更新