❶ 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);
...............
以此方式解析