其他
Java 中什么是 IO 流,字节流、字符流两者区别,缓冲流原理代码剖析
>>号外:关注“Java精选”公众号,回复“2021面试题”关键词,领取全套500多份Java面试题文件。
public static void main(String[] args) throws IOException {
int a = System.in.read();
System.out.println(a);
char c = 'a';
System.out.println((int) c);
}
}
public static void main(String[] args) throws IOException {
char a = (char) System.in.read();//读取一个字节
System.out.println(a);
char c = '中';
System.out.println(c);
}
}
public static void main(String[] args) throws IOException {
InputStreamReader inputStreamReader1 = new InputStreamReader(System.in);
int b = inputStreamReader1.read();//只能读一个字符
System.out.println(b);
InputStreamReader inputStreamReader2 = new InputStreamReader(System.in);
char[] chars = new char[2];
int c = inputStreamReader2.read(chars);//读入到指定char数组,返回当前读取到的字符数
System.out.println("读取的字符数为:" + c);
System.out.println(chars[0]);
System.out.println(chars[1]);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()){
System.out.println(scanner.nextInt());
}
}
}
DataInputStream din = new DataInputStream(fin);
System.out.println(din.readInt());
FileInputStream fin = new FileInputStream("E:\\test.txt");
DataInputStream din = new DataInputStream(new BufferedInputStream(fin));
System.out.println(din.readInt());
public static void main(String[] args) throws IOException {
FileInputStream fin = new FileInputStream("E:\\test.txt");//文档内存储 abcd
PushbackInputStream pin = new PushbackInputStream(new BufferedInputStream(fin));
int a = pin.read();//读取到a
System.out.println(a);
if (a != 'b'){
pin.unread(a);//将 a 推回流中
}
System.out.println(pin.read());//再次读取到 a
System.out.println(pin.read());//读取到 b
System.out.println(pin.read());// 读取到 c
}
}
fout.write(1);
fout.write(2);
DataOutputStream dout = new DataOutputStream(fout);
dout.write(9);
dout.write(10);
public static void main(String[] args) throws Exception {
//字节流
FileInputStream fin = new FileInputStream("E:\\test.txt");//文本内容为“双子孤狼”
System.out.println(fin.read());//372
//字符流
InputStreamReader ir = new InputStreamReader(new FileInputStream("E:\\test.txt"));//文本内容为“双子孤狼”
System.out.println(ir.read());//21452
char s = '双';
System.out.println((int)s);//21452
}
}
public static void main(String[] args) throws Exception {
InputStreamReader ir = new InputStreamReader(new FileInputStream("E:\\test.txt"));//文本内容为“双子孤狼”
BufferedReader br = new BufferedReader(ir);
String s;
while (null != (s = br.readLine())){
System.out.println(s);//输出双子孤狼
}
}
}
public static void main(String[] args) throws Exception{
PrintWriter printWriter = new PrintWriter("E:\\test3.txt");
printWriter.write("双子孤狼");
printWriter.flush();
}
}
public static void main(String[] args) throws Exception {
//文档内容为 lonely wolf
RandomAccessFile inOut = new RandomAccessFile(new File("E:\\test.txt"),"rw");
System.out.println("当前指针在:" + inOut.getFilePointer());//默认在0
System.out.println((char) inOut.read());//读到 l
System.out.println("当前指针在:" + inOut.getFilePointer());
inOut.seek(7L);//指针跳转到7的位置
System.out.println((char) inOut.read());//读到 w
inOut.seek(7);//跳回到 7
inOut.write(new byte[]{'c','h','i','n','a'});//写入 china,此时 wolf被覆盖
inOut.seek(7);//继续跳回到 7
System.out.println((char) inOut.read());//此时因为 wolf 被 china覆盖,所以读到 c
}
}
作者:双子孤狼
cnblogs.com/lonely-wolf/p/14647514.html
MySQL 锁原理通过 6 个死锁案例,让你彻底理解 MySQL 锁机制,死锁的原因!
你可能忽视的 MyBatis 3.5.X 在 JDK8 中存在的性能问题
Spring Boot 框架中使用自定义注解 + 拦截器实现身份证等敏感数据加解密
Java 中处理 Exception 的 9 种实践,曾被很多团队认可采纳,值得收藏!
Java 中 ThreadPoolExecutor 线程池必备知识点:工作流程、常见参数、性能调优及监控