Java 如何解密文件并将其添加到字符串列表中
在我out.write(buffer,0,rumRead)所在的行中,如何将其添加到已定义的列表中,而不是写入文件?我尝试将其添加到对象列表中,但不起作用。 以下是我的解密方法:Java 如何解密文件并将其添加到字符串列表中,java,Java,在我out.write(buffer,0,rumRead)所在的行中,如何将其添加到已定义的列表中,而不是写入文件?我尝试将其添加到对象列表中,但不起作用。 以下是我的解密方法: public static void decrypt(String file) { try { File output = new File(file.replace(encryptedFileType, initialFileType)); if (!(output.exist
public static void decrypt(String file) {
try {
File output = new File(file.replace(encryptedFileType, initialFileType));
if (!(output.exists())) {
output.createNewFile();
}
InputStream in = new FileInputStream(file.replace(initialFileType, encryptedFileType));
OutputStream out = new FileOutputStream(output);
in = new CipherInputStream(in, dcipher);
int numRead = 0;
while ((numRead = in.read(buffer)) >= 0) {
out.write(buffer, 0, numRead);
}
out.close();
new File(file.replace(initialFileType, encryptedFileType)).delete();
} catch (IOException e) {
e.printStackTrace();
}
}
假设您希望以字符串形式读取文件中的内容并将其添加到字符串列表中,您可以首先解析刚以字符串形式读取的缓冲区并将其添加
List<String> strList = new LinkedList<String>();
strList.add(new String(buffer, 0, numRead));
List strList=newlinkedlist();
add(新字符串(缓冲区,0,numRead));
请注意,此代码从文件中读取固定长度的字符串(不以换行符分隔)。固定长度由缓冲区大小决定。还考虑Link表数据结构是否适合于
您可以使用BufferedReader从文件中读取换行符分隔的数据:
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt")));
List<String> strList = new LinkedList<String>();
String line = reader.readLine(); // will return null if reached end of stream
while(line != null) {
strList.add(line); // add into string list
line = reader.readLine(); // read next line
}
reader.close();
BufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(“file.txt”));
List strList=newlinkedlist();
字符串行=reader.readLine();//如果到达流的末尾,将返回null
while(行!=null){
strList.add(行);//添加到字符串列表中
line=reader.readLine();//读取下一行
}
reader.close();
我试过了,它将所有文件内容添加到列表中,但有些行丢失了部分内容。当我将其写入文件时,它工作正常,但由于某些原因,将其形成新字符串并不完全有效。请记住,代码只接受根据缓冲区大小固定长度的字符串。如果您需要用空白/换行来标记输入,请考虑使用扫描仪/FielePixFielts,请给我一个例子,请用我的方法使用扫描仪。