public class Test {
public static void main(String[] args) {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
//1、指明输入输出文件
File inFile = new File(“IO\hi.txt”);
File outFile = new File(“IO\hello.txt”);
//2、提供字节节点流
FileInputStream fis = new FileInputStream(inFile);
FileOutputStream fos = new FileOutputStream(outFile);
//3、提供转换流
isr = new InputStreamReader(fis,“gbk”);
osw = new OutputStreamWriter(fos,“utf-8”);
//4、读写操作
char[] chars = new char[10];
int len = isr.read(chars);
while (len != -1){
osw.write(chars,0,len);
len = isr.read(chars);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、关闭外层流
try {
if (osw != null) osw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (isr != null) isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
System.in
和System.out
分别代表了系统标准的输入和输出设备
默认输入设备是:键盘,输出设备是:显示器(控制台)
System.in
的类型是InputStream
System.out
的类型是PrintStream,其是OutputStream的子类
重定向:通过System类的setIn()
,setOut()
方法对默认设备进行改变:
public static void setIn(InputStream in)
public static void setOut(PrintStream out)
import java.io.*;
/**
-
@Author: Yeman
-
@Date: 2021-09-26-20:13
-
@Description:
*/
public class Test {
public static void main(String[] args) {
BufferedReader bis = null;
try {
//1、提供转换流(System.in是字节流,将其转换为字符流)
System.out.println(“请输入信息(退出输入e或exit):”);
InputStreamReader isr = new InputStreamReader(System.in);
//2、提供缓冲流将输入的一行读取
bis = new BufferedReader(isr);
//3、读操作
String s = null;
while ((s = bis.readLine()) != null){
if (“e”.equalsIgnoreCase(s) || “exit”.equalsIgnoreCase(s)){
System.out.println(“程序结束,退出程序!”);
break;
}
System.out.println(“==” + s.toUpperCase());
System.out.println(“继续输入(退出输入e或exit):”);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、关闭外层流
try {
if (bis != null) bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
实现将基本数据类型的数据格式转化为字符串输出。
打印流:PrintStream
和PrintWriter
提供了一系列重载的print()
和println()
方法,用于多种数据类型的输出:
PrintStream和PrintWriter的输出不会抛出IOException异常,
PrintStream和PrintWriter有自动flush功能,
PrintStream打印的所有字符都使用平台的默认字符编码转换为字节
在需要写入字符而不是写入字节的情况下,应该使用PrintWriter类。
System.out返回的是PrintStream的实例。
常与System.out搭配使用,可以不在控制台输出,而是输出到指定位置:
import java.io.*;
/**
-
@Author: Yeman
-
@Date: 2021-09-26-20:13
-
@Description:
*/
public class Test {
public static void main(String[] args) {
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File(“IO\text.txt”));
// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 ‘\n’ 时都会刷新输出缓冲区)
ps = new PrintStream(fos, true);
if (ps != null) {// 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 输出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { // 每50个数据一行
System.out.println(); // 换行
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
}
}
}
}
为了方便地操作Java语言的基本数据类型和String的数据,可以使用数据流。
数据流有两个类:(用于读取和写出基本数据类型、String类的数据)
DataInputStream
和 DataOutputStream
分别“套接”在 InputStream 和 OutputStream 子类的流上。
import java.io.*;
/**
-
@Author: Yeman
-
@Date: 2021-09-26-20:13
-
@Description:
*/
public class Test {
public static void main(String[] args) {
//写
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream(“IO\test.txt”));
dos.writeUTF(“叶绿体”);
dos.writeInt(22);
dos.writeBoolean(true);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dos != null) dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(“IO\test.txt”));
//注意读的顺序要和写的顺序一样
String name = dis.readUTF();
int age = dis.readInt();
boolean isMan = dis.readBoolean();
System.out.println(name + age + isMan);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null) dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ObjectInputStream
和OjbectOutputSteam
用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
序列化:用ObjectOutputStream
类保存基本类型数据或对象的机制
反序列化:用ObjectInputStream
类读取基本类型数据或对象的机制
ObjectOutputStream
和ObjectInputStream
不能序列化static
和transient
修饰的成员变量。
实现Serializable
或者Externalizable
两个接口之一的类的对象才可序列化,关于对象序列化详见:序列化
可序列化对象:
import java.io.Serializable;
/**
-
@Author: Yeman
-
@Date: 2021-09-27-8:27
-
@Description:
*/
class pet implements Serializable {
public static final long serialVersionUID = 999794470754667999L;
private String name;
public pet(String name) {
this.name = name;
}
@Override
public String toString() {
return “pet{” +
“name='” + name + ‘’’ +
‘}’;
}
}
public class Person implements Serializable {
public static final long serialVersionUID = 6849794470754667999L;
private String name;
private int age;
private pet pet;
public Person(String name, int age, pet pet) {
this.name = name;
this.age = age;
this.pet = pet;
}
@Override
public String toString() {
return “Person{” +
“name='” + name + ‘’’ +
“, age=” + age +
“, pet=” + pet +
‘}’;
}
}
序列化(ObjectOutputStream):
import java.io.*;
/**
-
@Author: Yeman
-
@Date: 2021-09-26-20:13
-
@Description:
*/
public class Test {
public static void main(String[] args) {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(“IO\test.txt”));
oos.writeUTF(new String(“你好世界!”));
oos.flush();
oos.writeObject(new Person(“Lily”,20,new pet(“Xinxin”)));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
反序列化(ObjectInputStream):
import java.io.*;
/**
-
@Author: Yeman
-
@Date: 2021-09-26-20:13
-
@Description:
*/
public class Test {
public static void main(String[] args) {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(“IO\test.txt”));
String s = ois.readUTF();
Person o = (Person) ois.readObject();
System.out.println(o.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
那么如何才能正确的掌握Redis呢?
为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题
-
并发编程面试题汇总
-
JVM面试题汇总
-
Netty常被问到的那些面试题汇总
-
Tomcat面试题整理汇总
-
Mysql面试题汇总
-
Spring源码深度解析
-
Mybatis常见面试题汇总
-
Nginx那些面试题汇总
-
Zookeeper面试题汇总
-
RabbitMQ常见面试题汇总
JVM常频面试:
Mysql面试题汇总(一)
Mysql面试题汇总(二)
Redis常见面试题汇总(300+题)
ing());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
那么如何才能正确的掌握Redis呢?
为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题
-
并发编程面试题汇总
-
JVM面试题汇总
-
Netty常被问到的那些面试题汇总
-
Tomcat面试题整理汇总
-
Mysql面试题汇总
-
Spring源码深度解析
-
Mybatis常见面试题汇总
-
Nginx那些面试题汇总
-
Zookeeper面试题汇总
-
RabbitMQ常见面试题汇总
JVM常频面试:
[外链图片转存中…(img-uQp3j27a-1714798508454)]
Mysql面试题汇总(一)
[外链图片转存中…(img-hpDdoRCP-1714798508455)]
Mysql面试题汇总(二)
[外链图片转存中…(img-GZAlS9ju-1714798508455)]
Redis常见面试题汇总(300+题)
[外链图片转存中…(img-ErYZZl5p-1714798508456)]