❶ java怎麼對bytes數組進行位操作,例如取出buf是bytes數組,怎麼取出bytes[0]一個位元組裡面的前4位
//byte buf[]=為數組
for(byte b:buf){
System.out.print(b&15);//列印每個節的低四位
System.out.println(b>>>4);//列印每個節的高四位
}
❷ java中怎樣按位元組讀取文件並復制到另一個文件夾
這里以位元組流FileInputStream,FileOutputStream為例。代碼例子如下:
importjava.io.File;
/**
*把一個文件夾中的文件復制到一個指定的文件夾
*@authoryoung
*
*/
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
publicclassCopyFile{
publicstaticvoidmain(String[]args){
/*指定源exe文件的存放路徑*/
Stringstr="f:/jdk-1_5_0_06-windows-i586-p.exe";
/*指定復制後的exe的目標路徑*/
Stringstrs="e:/.exe";
/*創建輸入和輸出流*/
FileInputStreamfis=null;
FileOutputStreamfos=null;
try{
/*將io流和文件關聯*/
fis=newFileInputStream(str);
fos=newFileOutputStream(strs);
byte[]buf=newbyte[1024*1024];
intlen;
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
fis.close();
fos.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
❸ java 截取字元串第一個字元
使用substring() 方法返回字元串的子字元串。詳細解析如下:
1、語法:
(1)public String substring(int beginIndex)。
(2)public String substring(int beginIndex, int endIndex)。
2、參數:
(1)beginIndex -- 起始索引(包括), 索引從 0 開始。
(2)endIndex -- 結束索引(不包括)。
3、返回值:
返回一個新字元串,它是此字元串的一個子字元串。該子字元串從指定的 beginIndex 處開始,一直到索引 endIndex - 1處的字元。因此,該子字元串的長度為 endIndex-beginIndex。
4、substring函數存在的拋出錯誤:
IndexOutOfBoundsException - 如果 beginIndex 為負,或 endIndex 大於此 String 對象的長度,或 beginIndex 大於 endIndex。
5、實例代碼如下:
❹ java將byte數組中的中間一部分值取出來怎麼做啊
如果以這種方式存儲,那麼一定是定長字元串,byte[]是以位元組來存儲的,你直接取規則的長度就行了啊
如下:
byte[] b = new byte[10];
b[0]='a';
b[1]='b';
b[2]='c';
b[3]='d';
String a = new String(b,0,2);
用你的例子來說:比如你的標志是5位的,編號12位,日期20位,測量值10位
那麼應該是
String bz = new String(b,0,5);
String bh = new String(b,5,12);
...............
以此方式解析