编码问题:
File类只用于表示文件目录的信息(名称大小),不能用于文件内容的访问
API:
File file = new File();
file.exists(); //是否存在
file.mkdir(); //创建目录
file.delete(); //删除目录
file.isDirectory(); //是不是目录
file.isFile //是不是文件
file.createNewFile();//创建文件
System.out.println(file,file.getAbsolutePath(),file.getName);//前两个结果一样都是返回目录加file名,后一个只有文件名
file.getParent();返回父目录
RandomAccessFile java提供的对文件内容的访问,既可以°文件,也可以写文件
RandomAccessFile支持随机访问文件,可以访问文件的任意位置
打开方式:
有rw 读写,r 只读
RandomAccessFile raf = new RandomAccessFile(file,"rw");
文件指针,打开文件时指针在开头pointer = 0;
写方法
raf.write(int) ,writeInt(); 只写一个字节(后8位),同时指针指向下一个位置
都方法
int b = raf.read() 读一个字节
57 111001
2^(6-1)+2^(5-1)+2^(4-1)+2^(1-1)
短除法:
除法 商 余数
57/2 28 1
28/2 14 0
14/2 7 0
7/2 3 1
3/2 1 1
1/2 0 1
将余数从下往上数,答案:111001
位运算:
& 两位全为1,结果才为1
用法:
清零,与0相与,结果为0
取一个数的指定位:设x=10101110,去x的低四位,用x&00001111=00001110
| 只要一个为1,结果就为1
用途:
将一个数的某些位置1
将x=10100000的低四位置1,x|00001111=10101111
^ 两个相应位为异,结果为1
用途:
使特定位反转
x=10101110,使x低四位反转,用x^00001111=10100001
利用与0相异,保留原值,可以实现值交换
x^00000000=10101110
a=a^b;b=a^b;a=a^b;将a,b的值交换
~取反运算
<<左移运算:
所有位全部左移,左边二进制丢弃,右边补0
若左移时舍弃的高位不包含1,则每左移一位,相当于该数的十进制乘以2
>>右移运算 正数左补1,负数左补0 除以2
无符号右移 >>>
左边不分正负,空出的位用0来填充
-14为例
负数以其正值的补码形式表示:
14的原码:0000 0000 0000 0000 0000 0000 0000 1110
反码: 1111 1111 1111 1111 1111 1111 1111 0001
补码: 1111 1111 1111 1111 1111 1111 1111 0010
<<2 1111 1111 1111 1111 1111 1111 1100 1000
然后只需将该补码的源码对应的正值,然后去相反数
补码减一:11000111 原码:00111000=56 去相反数既是-56
8143 0000 0000 0000 0000 0001 1111 1100 1111
=>byte[] b = [-49,31,0,0]
第一个低端字节:8143>>0*8&0xff=(11001111) =-49
第二个低端字节:8143>>1*8&0xff=00011111=31
第三,第四个均为0
另还可以按高端字节[0,0,31,-49]
字符串转换字节:
String s; byte[] bs = s.getBytes();
字节转字符串
byte[] bs = new byte[int];
String s = new String(bs);或
String s = new String(bs,encode);编码方式
例子代码:
结果:
-49 31 0 0
8143
-43 -59 -56 -3
张三
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "我爱Java";
byte[] bytes1 = s.getBytes();//转换成字节序列,使用项目默认的编码
for (byte b : bytes1) {
//把字节转换成了int以16进制的方式显示
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
byte[] bytes2 = s.getBytes("gbk");
//gbk编码中文占用2个字节,英文占一个字节
for (byte b : bytes2) {
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
byte[] bytes3 = s.getBytes("utf-8");
//utf-8编码中文占用3个字节,英文占一个字节
for (byte b : bytes3) {
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
//java是双字节编码utf-16be
System.out.println();
byte[] bytes4 = s.getBytes("utf-16be");
//utf-16be编码中文占用2个字节,英文占2个字节
for (byte b : bytes4) {
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
String str1 = new String(bytes4);//项目默认的编码
System.out.println(str1);
String str2 = new String(bytes4, "utf-16be");
System.out.println(str2);
}
输出:
e6 88 91 e7 88 b1 4a 61 76 61
ce d2 b0 ae 4a 61 76 61
e6 88 91 e7 88 b1 4a 61 76 61
62 11 72 31 0 4a 0 61 0 76 0 61
br1 J a v a
我爱Java
File类只用于表示文件目录的信息(名称大小),不能用于文件内容的访问
API:
File file = new File();
file.exists(); //是否存在
file.mkdir(); //创建目录
file.delete(); //删除目录
file.isDirectory(); //是不是目录
file.isFile //是不是文件
file.createNewFile();//创建文件
System.out.println(file,file.getAbsolutePath(),file.getName);//前两个结果一样都是返回目录加file名,后一个只有文件名
file.getParent();返回父目录
RandomAccessFile java提供的对文件内容的访问,既可以°文件,也可以写文件
RandomAccessFile支持随机访问文件,可以访问文件的任意位置
打开方式:
有rw 读写,r 只读
RandomAccessFile raf = new RandomAccessFile(file,"rw");
文件指针,打开文件时指针在开头pointer = 0;
写方法
raf.write(int) ,writeInt(); 只写一个字节(后8位),同时指针指向下一个位置
都方法
int b = raf.read() 读一个字节
文件读完后一定要关闭
57 111001
2^(6-1)+2^(5-1)+2^(4-1)+2^(1-1)
短除法:
除法 商 余数
57/2 28 1
28/2 14 0
14/2 7 0
7/2 3 1
3/2 1 1
1/2 0 1
将余数从下往上数,答案:111001
位运算:
& 两位全为1,结果才为1
用法:
清零,与0相与,结果为0
取一个数的指定位:设x=10101110,去x的低四位,用x&00001111=00001110
| 只要一个为1,结果就为1
用途:
将一个数的某些位置1
将x=10100000的低四位置1,x|00001111=10101111
^ 两个相应位为异,结果为1
用途:
使特定位反转
x=10101110,使x低四位反转,用x^00001111=10100001
利用与0相异,保留原值,可以实现值交换
x^00000000=10101110
a=a^b;b=a^b;a=a^b;将a,b的值交换
~取反运算
<<左移运算:
所有位全部左移,左边二进制丢弃,右边补0
若左移时舍弃的高位不包含1,则每左移一位,相当于该数的十进制乘以2
>>右移运算 正数左补1,负数左补0 除以2
无符号右移 >>>
左边不分正负,空出的位用0来填充
-14为例
负数以其正值的补码形式表示:
14的原码:0000 0000 0000 0000 0000 0000 0000 1110
反码: 1111 1111 1111 1111 1111 1111 1111 0001
补码: 1111 1111 1111 1111 1111 1111 1111 0010
<<2 1111 1111 1111 1111 1111 1111 1100 1000
然后只需将该补码的源码对应的正值,然后去相反数
补码减一:11000111 原码:00111000=56 去相反数既是-56
JAVA内置的进制转换:
8143 0000 0000 0000 0000 0001 1111 1100 1111
=>byte[] b = [-49,31,0,0]
第一个低端字节:8143>>0*8&0xff=(11001111) =-49
第二个低端字节:8143>>1*8&0xff=00011111=31
第三,第四个均为0
另还可以按高端字节[0,0,31,-49]
字符串转换字节:
String s; byte[] bs = s.getBytes();
字节转字符串
byte[] bs = new byte[int];
String s = new String(bs);或
String s = new String(bs,encode);编码方式
例子代码:
public class Test {
public static void main(String[] args) throws UnsupportedEncodingException {
int id = 8143;
byte[] arr = Test.int1(id);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
int id1 = Test.byte1(arr);
System.out.println(id1);
String str = "张三";
byte[] sarr = str.getBytes();
for (int i = 0; i < sarr.length; i++) {
System.out.print(sarr[i] + " ");
}
System.out.println();
String des = new String(sarr);
System.out.println(des);
}
public static int byte1(byte[] arr) {
int result = 0;
for (int i = 0; i < arr.length; i++) {
result += (int) ((arr[i] & 0xff) << i * 8);
}
return result;
}
public static byte[] int1(int id) {
byte[] arr = new byte[4];
for (int i = 0; i < arr.length; i++) {
arr[i] = (byte) ((int) (id >> i * 8) & 0xff);
}
return arr;
}
}
结果:
-49 31 0 0
8143
-43 -59 -56 -3
张三