网站开发多钱,网络营销ppt模板,绚丽的网站欣赏,产品设计公司网站1.转换流
InputStreamReader: 把InputStream转换为Reader#xff0c;可以指定编码表 OutputStreamWriter: 把OutputStream转换为Writer#xff0c;可以指定编码表
//转换流[用于指定编码表读入或写出]
public class Demo1 {public static void main(String[] args) throws …1.转换流
InputStreamReader: 把InputStream转换为Reader可以指定编码表 OutputStreamWriter: 把OutputStream转换为Writer可以指定编码表
//转换流[用于指定编码表读入或写出]
public class Demo1 {public static void main(String[] args) throws IOException {//jdk11以前使用转化流[按照指定的编码表将字节流转化为字符流]method1();//jdk11之后提供了字符流指定编码表//Charset.forName()封装指定编码表//method2();}private static void method1() throws IOException {//1.创建转化输入输出流对象InputStreamReader isr new InputStreamReader(new FileInputStream(day12_myIoOtherStream\\a.txt), gbk);OutputStreamWriter osw new OutputStreamWriter(new FileOutputStream(day12_myIoOtherStream\\c.txt), utf-8);char []chars new char[1024];int len isr.read(chars);//2.边读边写while (len!-1){osw.write(chars,0,len);len isr.read(chars);}//3.释放资源isr.close();osw.close();}private static void method2() throws IOException {//1.创建字符输入输出流对象FileReader fr new FileReader(day12_myIoOtherStream\\a.txt, Charset.forName(GBK));FileWriter fw new FileWriter(day12_myIoOtherStream\\b.txt, Charset.forName(utf-8));char []chars new char[1024];int len fr.read(chars);//2.边读边写while (len!-1){fw.write(chars,0,len);len fr.read(chars);}//3.释放资源fr.close();fw.close();}
}
打印结果
------------------------------------------------------------------------------------------------
a.txt:我爱中国
b.txt:我爱中国
c.txt:Ұй2.对象流【隐藏对象不能被直接读取】
ObjectInputStream: 读取对象的 ObjectOutputStream: 写入对象
//对象操作流序列化流----------做了解
//objectOutputStream 序列化流 写对象
//objectInputStream 反序列化流 读对象
public class Demo1 {public static void main(String[] args) throws IOException, ClassNotFoundException {User user new User(zhangsan, 123456);//把对象写到文件中去【序列化】
// ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(day12_myIoOtherStream\\d.txt));
// oos.writeObject(user);
// oos.close();//从文件中读取对象【反序列化】ObjectInputStream ois new ObjectInputStream(new FileInputStream(day12_myIoOtherStream\\d.txt));Object o ois.readObject();System.out.println(o);}
}//用户类
//Serializable 标记接口表示可被序列化
public class User implements Serializable {private String name;private String password;//防止序列化前后属性变更可固定序列号解决报错//代码可在String源码中复制private static final long serialVersionUID 67710L;public User(String name, String password) {this.name name;this.password password;}//标准的Javabean类get、set方法Overridepublic String toString() {return User{ name name \ , password password \ };}
}打印结果
----------------------------------------------------------------------------
d.txt: sr com.itheima.objectStream.User ~ L namet Ljava/lang/String;L passwordq ~ xpt zhangsant 123456
控制台User{namezhangsan, password123456}3.标准的输入输出流【面试】
System.in: 标准的输入流读取键盘录入的数据 System.out: 标准的输出流往控制台输出数据
//标准输入输出流【扩展内容打印和键盘录入底层原理】
public class Demo1 {public static void main(String[] args) throws IOException {//System.in:标准的输入流读取键盘录入的数据[InputStream]//Scanner是对其的包装增加了对数据的操作//method1();//System.out:标准的输出流,往控制台输出数据//print底层用的输出流就是PrintStream 父类为[FilterOutputStream]method2();}private static void method2() throws IOException {PrintStream out System.out;//把字节流转化为字节流[转化流]OutputStreamWriter osw new OutputStreamWriter(out);//把字符流包装成缓冲流BufferedWriter bw new BufferedWriter(osw);//向控制台输出数据bw.write(asd);//释放资源bw.close();}private static void method1() throws IOException {Scanner sc new Scanner(System.in);//自定义[键盘录入]InputStream in System.in;//把字节流转化为字节流[转化流]InputStreamReader isr new InputStreamReader(in);//把字符流包装成缓冲流BufferedReader br new BufferedReader(isr);//读取键盘录入的数据String s br.readLine();System.out.println(s);//关闭流br.close();}
}打印结果
----------------------------------------------------------------------------
method1:1212
method2: asd