![](/img/trans.png)
[英]How to write ASCII extended characters(which has ascii code > 127) to XML file using java?
[英]How to read/write extended ASCII characters as a string into ANSI coded text file in java
這是我的加密程序。 主要用於加密文件(文本),此部分程序將List<Integer>
元素轉換為byte []
並將其寫入文本文件。 不幸的是我無法提供算法。
void printit(List<Integer> prnt, File outputFile) throws IOException
{
StringBuilder building = new StringBuilder(prnt.size());
for (Integer element : prnt)
{
int elmnt = element;
//building.append(getascii(elmnt));
building.append((char)elmnt);
}
String encryptdtxt=building.toString();
//System.out.println(encryptdtxt);
byte [] outputBytes = offo.getBytes();
FileOutputStream outputStream =new FileOutputStream(outputFile);
outputStream.write(outputBytes);
outputStream.close();
}
這是解密程序,其中解密程序從.enc文件獲取輸入
void getfyle(File inputFile) throws IOException
{
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int)inputFile.length()];
inputStream.read(inputBytes);
inputStream.close();
String fylenters = new String(inputBytes);
for (char a:fylenters.toCharArray())
{
usertext.add((int)a);
}
for (Integer bk : usertext)
{
System.out.println(bk);
}
}
由於此處使用的方法,在我的算法中,要求List<Integer>
byte[]
首先轉換為String
,然后轉換為List<Integer>
,反之亦然。
加密期間寫入文件時的元素與從.enc文件讀取的元素不匹配。
我將List<Integer>
轉換為byte[]
正確嗎? 還是其他問題? 。 我確實知道Java無法打印擴展的ASCII字符,所以我使用了它。但是,即使失敗了,它也給出了很多?
有解決方案嗎? 請幫助我..以及其他格式(.png.mp3 ....等)的處理方法
加密文件的格式可以是xx以外的任何格式(不必是.enc)。
有成千上萬種不同的“擴展ASCII”代碼 ,Java支持大約一百種,但是您必須告訴它使用哪個“字符集”,否則默認值通常會導致數據損壞。
雖然通常以十六進制或base64表示任意“二進制”字節是常見且經常必要的,但如果將以保留所有256個值(通常稱為“ 8位清除” )和File{Input,Output}Stream
方式存儲和/或傳輸字節File{Input,Output}Stream
確實可以使用“ ISO-8859-1” ,它可以將Java char
代碼0-255映射到字節0-255之間或從字節0-255映射出來,因為Unicode部分基於8859-1。
在輸入時 ,讀取(讀入) byte[]
,然后讀取new String (bytes, charset)
,其中charset是名稱 "ISO-8859-1"
或該名稱的java.nio.charset.Charset
對象 ,可以作為java.nio.charset.StandardCharSets.ISO_8859_1
; 或在使用該字符集名稱或對象的緩沖區中讀取字節或直接從文件中讀取字節的流上創建InputStreamReader
,並從Reader
讀取char和/或String
在輸出上 ,使用String.getBytes(charset)
,其中charset是該字符集名稱或對象,並寫入byte[]
; 或在使用該字符集名稱或對象的字節上將字節寫入緩沖區或文件的流上創建OutputStreamWriter
,並將字符和/或String
Writer
但你實際上並不需要 char
和String
和Charset
的 。 實際上,您實際上想將一系列Integer
作為字節寫入,並將一系列字節作為Integer
讀取。 因此,只需執行以下操作:
void printit(List<Integer> prnt, File outputFile) throws IOException
{
byte[] outputBytes = new byte[prnt.size()]; int i = 0;
for (Integer element : prnt) outputBytes[i++] = (byte)element;
FileOutputStream outputStream =new FileOutputStream(outputFile);
outputStream.write(b);
outputStream.close();
// or replace the previous three lines by one
java.nio.file.Files.write (outputFile.toPath(), outputBytes);
}
void getfyle(File inputFile) throws IOException
{
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int)inputFile.length()];
inputStream.read(inputBytes);
inputStream.close();
// or replace those four lines with
byte[] inputBytes = java.nio.file.Files.readAllBytes (inputFile.toPath());
for (byte b: inputBytes) System.out.println (b&0xFF);
// or if you really wanted a list not just a printout
ArrayList<Integer> list = new ArrayList<Integer>(inputBytes.length);
for (byte b: inputBytes) list.add (b&0xFF);
// return list or store it or whatever
}
任意數據字節都不能全部轉換為任何字符編碼,並且加密會創建包括所有值0-255的數據字節。
如果必須將加密的數據轉換為字符串格式,則標准方法將轉換為Base64或十六進制。
在加密部分:
`for (Integer element : prnt)
{
int elmnt = element;
//building.append(getascii(elmnt));
char b = Integer.toString(elmnt).charAt(0);
building.append(b);
}`
->這會將int轉換為char,例如1到'1'以及5到'5'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.