❶ java中創建byte數組如下 byte a[]=new byte[1024]; 問: new後面
new其實就是創建一個新的熟悉,在內存中開辟一個空間。
new 就是創建一個對象的意思。
這里new就是創建一個byte數組,
byte[1024]是數組長度為1024
❷ java中如何把一個文件轉化為byte數組
java將文件轉換為byte數組,主要是使用輸出流,實例如下:
/**
*根據byte數組,生成文件
*/
publicstaticvoidgetFile(byte[]bfile,StringfilePath,StringfileName){
BufferedOutputStreambos=null;//新建一個輸出流
FileOutputStreamfos=null;//w文件包裝輸出流
Filefile=null;
try{
Filedir=newFile(filePath);
if(!dir.exists()&&dir.isDirectory()){//判斷文件目錄是否存在
dir.mkdirs();
}
file=newFile(filePath+"\"+fileName);//新建一個file類
fos=newFileOutputStream(file);
bos=newBufferedOutputStream(fos);//輸出的byte文件
bos.write(bfile);
}catch(Exceptione){
e.printStackTrace();
}finally{
if(bos!=null){
try{
bos.close();//關閉資源
}catch(IOExceptione1){
e1.printStackTrace();
}
}
if(fos!=null){
try{
fos.close();//關閉資源
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
}
❸ java 中byte數組存的是什麼類型的數據,byte數組有什麼用
java中數組本身可以劃分為一個類型.這個類型就是存放各種類型的對象或者值.
至於你的問題,則可以回答為存放byte的數組應該是數組類型.或者說數組的泛型是byte.
具體作用,泛泛的說,應該是存儲數據.至於存儲後干什麼.那就看具體需求了.比如用於顯示,用於邏輯判斷......等等.
❹ java ByteArrayOutputStream從壓縮文件中讀取到位元組數組,轉換成字元串後再轉換成數組發現數據不一樣了
您好, 提問者:
ISO8859-1是佔1個位元組。
而UTF-8的漢字是佔三個位元組。
GBK的漢字的是占兩個位元組,當然不一樣了。
//轉換
new String(splitData.getBytes("ISO8859-1"),"UTF-8");
❺ java裡面byte數組和String字元串怎麼轉換
Java中byte數組轉換成string字元串可以直接使用string類的構造函數。而string轉byte數組,則可以使用string類型的getBytes()方法進行轉換,如下形式:
1、string 轉 byte[]
String str = "Hello";//聲明一個字元串
byte[] srtbyte = str.getBytes();//使用string類的getBytes方法進行轉換
2、byte[] 轉 string
byte[] srtbyte;//聲明一個byte位元組數組
String res = new String(srtbyte);//使用構造函數轉換成字元串
System.out.println(res);
也可以將byte轉換的時候,設定編碼方式相互轉換,如下代碼:
String str = "hello";
byte[] srtbyte = null;
try {
srtbyte = str.getBytes("UTF-8");//設定轉換的編碼格式
String res = new String(srtbyte,"UTF-8");
System.out.println(res);
} catch (UnsupportedEncodingException e) {//有可能會出現不能支持的編碼格式,捕捉異常。
e.printStackTrace();
}
❻ 用java讀取壓縮文件以byte數組形式傳送給C接收然後用c解壓 內存中出現大量空字元導致無法正確讀取zip,why
@Test
public void test02() throws IOException{
int nullByte = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:/download/201009067.rar"));
byte[] outByte = new byte[in.available()];
System.out.println(in.available());
in.read(outByte);
for(byte b : outByte)
if(b == 32)
nullByte ++;
System.out.println(nullByte);
in.close();
}
輸出
11635215
47092
我讀是沒有問題呢。
❼ 請問您的那個java和c++之間關於zip數據壓縮和解壓的問題解決了嗎我也正遇到這個問題,想請教一下
java用
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
//壓縮數據並返回壓縮後的長度
int compressedDataLength =compresser.deflate(output);
//分離壓縮後的數據
byte[] encodestrig = new byte[compressedDataLength];
System.array(output, 0,
encodestrig, 0, compressedDataLe
伺服器c++:
Byte buf[1024];
memset(buf,0,1024);
memcpy(buf,strdata.c_str(),strdata.size());
Byteodata[1024];
memset(odata,0,1024);
uLongnodata=1024;
zdecompress(buf,strdata.size(),odata,&nodata);
這個完全能夠解決。
❽ java中如何將一個zip文件轉為byte數組
input1.read(data1);
這一句執行好data1
就是文件的byte數組了!!!
String
str
=
new
String(data1);//轉換成string
下面不要了
for
(int
length1
=
0;
(length1
=
input1.read(data1))
>
0;)
{
output.write(data1,
0,
length1);
}
❾ Java中一個byte是多少個位元組,如果byte數組的長度是1024,是1mb的大小嗎
1byte 就是1個位元組
1byte = 8bit
1KB = 1024 byte
1MB = 1024 KB
所以如果byte數組的長度是1024,應該是1KB的大小
❿ 求教如何將一個java的byte數組快速轉換為int數組
byte[] bytes = new byte[1024];
int [] ints = new int[bytes.length];
int index = 0;
for(byte b : bytes){
ints[index++] = (int)b;
}