JAVA基础
Java基础
短路逻辑
java里的 &&和||,是短路的。
短路与 (&&):当左侧操作数的计算结果为 false 时,整个表达式的结果必定为 false,此时右侧的操作数(可能是一个包含方法调用、自增运算等的表达式)将不会被执行[3][5][8][13]。
短路或 (||):当左侧操作数的计算结果为 true 时,整个表达式的结果必定为 true,此时右侧的操作数同样不会被执行
内部类、静态内部类、匿名内部类
强转
强转前后,引用是不变的。

IO基础
Java字符
String
JDK 8 及以前:使用 final char value[] 来存储字符,每个字符占用 2 个字节(UTF-16 编码)。
JDK 9 及以后:引入了紧凑字符串优化。默认情况下,如果字符串仅包含 Latin-1 字符(如英文字母、数字),则使用 final byte value[] 存储,每个字符占 1 个字节;如果包含其他字符(如中文),则使用 UTF-16 编码,每个字符通常占 2 个字节。同时,内部使用一个 coder 标志位来标识编码方式,以节省内存空间。
unicode码值转成字符。如下,106对应j
int a= 106;
char b = (char) a;
System.out.println(b);//输出j
字符串转字节数组,默认采用系统编码,现在的系统编码是utf-8,1个英文字母一个字节,1个中文三个字节
public class TestString3 {
public static void main(String[] args) {
String a= "cdc新年快乐";
byte[] bytes = a.getBytes();
for (byte b:bytes) {
System.out.print(" "+b);
//输出 99 100 99 -26 -106 -80 -27 -71 -76 -27 -65 -85 -28 -71 -112
}
}
}
字节流
FileOutputStream
- write(int b) ,将指定的字节(参数 b 的低8位)写入输出流。参数 b 是一个 int 类型的整数,但实际写入的是其最低的8位二进制值,对应 ASCII 表中的字符。fos.write(97); 会写入字符 'a',因为 97 是 'a' 的 ASCII 码。
注意:若传入的整数超出 0-255 范围,高位会被截断,仅保留低8位。 - write(byte[] b),将整个字节数组的内容写入文件。
- write(byte[] b, int off, int len),该方法将字节数组中从索引 off 开始的 len 个字节写入文件。
public class TestString {
public static void main(String[] args) throws IOException {
//写入a.txt,存在则会覆盖,不存在则新建,但是其上级目录要存在,就是javadev和temp文件夹要有
FileOutputStream fileOutputStream = new FileOutputStream("D:\\javadev\\temp\\a.txt");
fileOutputStream.write(97);
fileOutputStream.close();
FileOutputStream fileOutputStream1 = new FileOutputStream("D:\\javadev\\temp\\ab.txt");
byte[]bytes={100,101,102,103};
fileOutputStream1.write(bytes);
fileOutputStream1.close();
FileOutputStream fileOutputStream2 = new FileOutputStream("D:\\javadev\\temp\\ac.txt");
byte[]byteArray={100,101,102,103,104,105,106};
//读的是第2个到第4个
fileOutputStream2.write(byteArray,1,3);
fileOutputStream1.close();
}
}
FileInputStream

public class IOTest1 {
public static void main(String[] args) throws IOException {
//如果文件不存在,直接报错
InputStream inputStream =new FileInputStream("D:\\bo\\CS\\操作系统导论.pdf");
OutputStream outputStream =new FileOutputStream("D:\\bo\\CS\\操作系统导论112.pdf");
int a=0;
long begin = System.currentTimeMillis();
byte [] bytes = new byte[8192];
//inputStream.read(bytes),返回值是读取了多少字节
while ((a=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,a);
}
long end = System.currentTimeMillis();
long l = TimeUnit.MILLISECONDS.toSeconds(end - begin);
System.out.println(l);
}
}
释放资源


相关启示
- 输出流连接到文件,文件不存在,则创建,而输入流连接到文件,文件不存在则报错,因为输入流须要数据流到程序,而输出流,数据就在程序里。
- 用完后释放资源
字符集








将01101100 01001001依次填补到三字节编码的空位,即0110、110001、001001,塞进1110xxxx 10xxxxxx 10xxxxxx



示例

public class TestIO1 {
public static void main(String[] args) throws UnsupportedEncodingException {
String a= "网络边缘";
//默认UTF-8编码
byte[] bytes = a.getBytes();
System.out.println(Arrays.toString(bytes));
String b= "计算机操作系统";
//GBK编码
byte[] bytes2 = b.getBytes("GBK");
System.out.println(Arrays.toString(bytes2));
//解码,默认UTF-8
String decode = new String(bytes);
System.out.println(decode);
//解码,GBK
String decode2 = new String(bytes2,"GBK");
System.out.println(decode2);
}
}
字符流
FileReader

- FileReader 类在读取文件时,默认使用的是系统平台的默认字符集。
- 查看当前系统的字符集及编码方式,System.out.println(Charset.defaultCharset());
- 现在系统是UTF-8
示例1:
public class TestReader {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("D:\\javadev\\temp\\a.txt");
int read;
while ((read=fileReader.read())!=-1) {
//读的是数字,英文1个字节,中文3个字节
System.out.print((char) read);
}
fileReader.close();
}
}
输出
a哈哈哈a哈哈哈a哈哈哈a哈哈哈a哈哈哈
a哈哈哈a哈哈哈
a哈哈哈a哈哈哈a哈哈哈
示例2:
public class TestReader {
public static void main(String[] args) throws IOException {
//文件内容是:ab的哈你
FileReader fileReader = new FileReader("D:\\javadev\\temp\\a.txt");
int read;
while ((read=fileReader.read())!=-1) {
//读的是数字,英文1个字节,中文3个字节
System.out.println(read);
}
fileReader.close();
}
}
输出,Unicode字符集,utf-8编码方式,对应“ab的哈你”
97
98
30340
21704
20320
示例3
- Java 中的 char 类型使用 UTF-16 编码,每个字符占用两个字节。它能够表示 Unicode 基本多文种平面(BMP)中的所有字符,包括大部分常用的中文字符。例如,汉字“你”的 Unicode 编码是 U+4F60,在 UTF-16 中直接用一个 16 位的代码单元表示,即两个字节。
- 当需要将字符串写入文件或通过网络传输时,可以使用 UTF-8 编码。此时,Java 会将内部的 UTF-16 编码转换为 UTF-8 编码。例如,字符“你”在 UTF-8 中表示为三个字节:E4 B8 A5。
- 在 Java 程序中处理字符串时,会自动完成编码转换:
当从源文件读取字符串时,如果源文件是 UTF-8 编码,Java 会将其转换为内部的 UTF-16。
当写入文件或发送数据时,Java 会将 UTF-16 字符串转换为 UTF-8 字节流。
public class TestReader2 {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("D:\\javadev\\temp\\a.txt");
char []chars=new char[1024];
int read;
//String底层是char数组,Java程序自动将utf-8转成utf-16
while ((read=fileReader.read(chars))!=-1) {
System.out.println(new String(chars,0,read));
}
fileReader.close();
}
}
输出:
ab的哈你
原理


FileWriter



原理
先把数据写到缓冲区,三种情况下,才会写入文件,flush刷新后可以继续写数据。



缓冲流
字节缓冲流

示例代码1:一次读写一个字节
public class TestBuffered3 {
public static void main(String[] args) throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("D:\\javadev\\temp\\a.txt"));
BufferedOutputStream bufferedOutputStream =new BufferedOutputStream(new FileOutputStream("D:\\javadev\\temp\\f.txt"));
int a=0;
while ((a=bufferedInputStream.read())!=-1){
bufferedOutputStream.write(a);
};
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
示例代码2:读若干字节,然后放到字节数组,接着写出
public class TestBuffered {
public static void main(String[] args) throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("D:\\javadev\\temp\\a.txt"));
BufferedOutputStream bufferedOutputStream =new BufferedOutputStream(new FileOutputStream("D:\\javadev\\temp\\e.txt"));
byte []bytes=new byte[8192];
int length=0;
while ((length=bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,length);
};
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
- 关闭缓冲流即可,底层代码里会把FileInputStream和FileOutputStream也关闭。
原理
从数据源读取,尽量读满输入缓冲区,变量b作为中间商,将数据写到输出缓冲区

字符缓冲流
一行一行地读

一行一行地写

转换流
示例代码:
public class TestTransfer {
public static void main(String[] args) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("D:\\javadev\\temp\\g.txt"),"GBK");
int ch;
while ((ch=inputStreamReader.read())!=-1){
System.out.print((char) ch);
}
inputStreamReader.close();
}
}



浙公网安备 33010602011771号