15_IO流
![]() |
![]() |
![]() |
![]() |
![]() |
|
package cn.sxt01.fileinputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) {
// 需求:读取c.txt中的内容
File file = new File("d:\\javatest\\c.txt");
// 【1】构建输入流(管道)
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
System.out.println(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 【2】读取一个字节
int r = 0;
try {
r = fis.read();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println((char)r);
// 【3】关闭流
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|
package cn.sxt01.fileinputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class Test03 {
public static void main(String[] args) {
// 需求:读取c.txt中的内容
File file = new File("d:\\javatest\\c.txt");
// 【1】构建输入流(管道)
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
System.out.println(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 【2】读取一个字节
int len = 0; // 读取到缓冲区中的字节个数
byte[] buf = new byte[2]; // 字节缓冲区
StringBuilder sb = new StringBuilder();
try {
while( (len=fis.read(buf)) != -1 ) {
String tmp = new String(buf,0,len);
sb.append(tmp);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sb);
// 【3】关闭流
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|
package cn.sxt01.fileouputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) {
// 需求:写入helloworld 到d.txt中的内容
File file = new File("d:\\javatest\\d.txt");
// 【1】创建输出流(管道)
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//【2】写入信息到输出流
try {
fos.write('h');
fos.write('e');
fos.write('l');
fos.write('l');
fos.write('o');
} catch (IOException e) {
e.printStackTrace();
}
// 【3】刷新缓冲区
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|
package cn.sxt01.fileouputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) {
// 需求:写入helloworld 到d.txt中的内容
File file = new File("d:\\javatest\\d.txt");
// 【1】创建输出流(管道)
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//【2】写入信息到输出流
try {
String str = "hello world中国";
// 默认gbk编码
/*byte[] buf = str.getBytes();
fos.write(buf);*/
byte[] buf = str.getBytes("utf8");
fos.write(buf);
} catch (IOException e) {
e.printStackTrace();
}
// 【3】刷新缓冲区
try {
fos.flush();
// 【4】关闭文件
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|
package cn.sxt03.filereader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) {
File file = new File("d:\\javatest\\d.txt");
// 【1】建立字符输入流
FileReader fr = null;
try {
fr = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 【2】一次读取一个字符
int r = 0;
try {
/*r = fr.read();
r = fr.read();
r = fr.read();
r = fr.read();
r = fr.read();
r = fr.read();
System.out.println(r);*/
/*
中国abc\r\n
中国你好你好
*/
StringBuilder sb = new StringBuilder();
while( (r=fr.read()) != -1 ) {
sb.append((char)r);
}
System.out.println(sb);
} catch (IOException e) {
e.printStackTrace();
}
// 【3】关闭输入流
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|
package cn.sxt03.filereader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class Test01 {
public static void main(String[] args) {
File file = new File("d:\\javatest\\d.txt");
// 【1】建立字符输入流
FileReader fr = null;
try {
fr = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 【2】一次读取多个字符
int len = 0;
char[] cbuf = new char[2];
try {
/*
len = fr.read(cbuf);
len = fr.read(cbuf);
len = fr.read(cbuf);
System.out.println(len);
System.out.println(Arrays.toString(cbuf));
*/
StringBuilder sb = new StringBuilder();
while( (len=fr.read(cbuf)) != -1) {
sb.append(cbuf, 0, len);
}
System.out.println(sb);
} catch (IOException e) {
e.printStackTrace();
}
// 【3】关闭输入流
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|
package cn.sxt03.filewriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) {
File file = new File("d:\\javatest\\e.txt");
FileWriter fw = null;
// 【1】建立输出流管道
try {
/*
* append:表示写入文件的方式
* true:追加 false:覆盖
*/
fw = new FileWriter(file,false);
} catch (IOException e) {
e.printStackTrace();
}
// 【2】写入
try {
// 写入一个字符
/*fw.write('中');
fw.write('国');*/
// 写入一个字符数组
/*char[] cbuf = {'中','国','\r','\n','a','b','c'};
fw.write(cbuf);*/
// 写入一个字符串
fw.write("中国abc");
} catch (IOException e) {
e.printStackTrace();
}
// 【3】刷新
try {
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
![]() |
|
package cn.sxt04.outputstramwriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class Test01 {
public static void main(String[] args) throws FileNotFoundException,IOException {
// 需求:写入 “中国abc” 以utf8编码写入
File file = new File("d:\\javatest\\g.txt");
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(out, "utf8");
osw.write('中');
char[] cbuf = {'国','a'};
osw.write(cbuf);
osw.write("中国abc");
osw.flush();
osw.close();
}
}
|
|
public class Test02 {
public static void main(String[] args) throws FileNotFoundException,IOException {
// 需求:读取g.txt的内容
File file = new File("d:\\javatest\\g.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "utf8");
/*int r = isr.read();
System.out.println((char)r);*/
char[] cbuf = new char[2];
int len = 0;
StringBuilder sb = new StringBuilder();
while( (len=isr.read(cbuf)) != -1) {
sb.append(cbuf,0,len);
}
System.out.println(sb);
}
}
|
|
public class Test03 {
public static void main(String[] args) throws FileNotFoundException,IOException {
// 需求:读取win手动创建的utf8编码的h.txt的内容
File file = new File("d:\\javatest\\h.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "utf8");
/*int r = isr.read();
System.out.println((char)r);*/
char[] cbuf = new char[2];
int len = 0;
StringBuilder sb = new StringBuilder();
while( (len=isr.read(cbuf)) != -1) {
sb.append(cbuf,0,len);
}
System.out.println(sb);
}
}
|
|
package cn.sxt01.bufferedreader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws FileNotFoundException,IOException {
File file = new File("d:\\javatest\\i.txt");
// ctrl+t:查看类继承关系
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
// 一次读取一行
/*
String str = br.readLine();
str = br.readLine();
str = br.readLine();
str = br.readLine();
str = br.readLine();
System.out.println(str);
*/
String line;
while( (line=br.readLine() ) != null) {
System.out.println(line);
}
br.close();
reader.close();
}
}
|
|
package cn.sxt01.bufferedwriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test01 {
public static void main(String[] args) throws FileNotFoundException,IOException {
// 需求:以utf8存入一首诗
File file = new File("d:\\javatest\\k.txt");
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(out, "utf8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write("床前明月光,");
bw.newLine();
bw.write("疑似地上霜。");
bw.newLine();
bw.flush();
bw.close();
osw.close();
out.close();
}
}
|
![]() |
|
package cn.sxt02.inout;
import java.io.IOException;
import java.io.InputStream;
public class Test01 {
public static void main(String[] args) throws IOException {
// 从控制台输入一个字符并打印
InputStream in = System.in;
// 【1】一次读取一个字节:(输入/数据源是键盘)
// int r = in.read();
// System.out.println((char)r);
// 【2】一次读取多个字节
byte[] buf = new byte[1024];
int len = 0;
len = in.read(buf);
// 默认控制台是gbk编码
String str = new String(buf, 0, len);
System.out.println(str);
}
}
|
|
package cn.sxt02.inout;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
public class Test02 {
public static void main(String[] args) throws IOException {
// 思考:为什么会乱码?
File file = new java.io.File("d:\\javatest\\i.txt");
FileInputStream fis = new FileInputStream(file);
// 标准输出流(gbk)
PrintStream ps = System.out;
int len = 0;
byte[] buf = new byte[2];
while( (len=fis.read(buf)) != -1 ) {
ps.write(buf, 0, len);
}
fis.close();
}
}
|
|
package cn.sxt02.inout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class Test05 {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
// 通过打印流写入数据到一个文件(gbk)
/*File file = new File("d:\\javatest\\m.txt");
PrintStream ps = new PrintStream(file);
ps.write('a');
ps.write('b');
ps.close();*/
// 通过打印流写入一个utf8编码的文件
File file = new File("d:\\javatest\\m1.txt");
PrintStream ps = new PrintStream(file,"utf8");
ps.println("abc中国");
ps.close();
}
}
|
|
package cn.sxt02.inout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class Test04 {
public static void main(String[] args) {
PrintStream ps = System.out;
ps.write('a');
ps.write('b');
ps.flush();
}
}
|
|
package cn.sxt02.inout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class Test04 {
public static void main(String[] args) throws IOException {
PrintStream ps = System.out;
/*ps.write('a');
ps.write('b');*/
// 自动调用flush方法
/*byte[] buf = {'a','b'};
ps.write(buf);*/
// 自动调用flush方法
/*ps.write('a');
ps.write('b');
ps.write('\n');*/
// 自动调用flush方法
ps.println("ab");
// sps.flush();
}
}
|
|
public class Test01 {
public static void main(String[] args) {
// 字符输出流
PrintWriter pw = new PrintWriter(System.out);
pw.println("hello");
pw.println("中国");
pw.flush();
pw.close();
}
}
|
|
package cn.sxt04.serializable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Test01 {
public static void main(String[] args) throws IOException {
User user = new User("001", "二狗", "123", 20);
/**
* 思路:
* 序列化:把对象个各个属性按照有规律的格式拼接成字符串,把字符串写入文件。
* 反序列化:把文件中的字符串读取为内存中,按序列化格式把字符串拆开得到很多属性值,然后初始化对象。
*/
// String info = user.getId()+"-"+user.getName()+"-"+user.getPwd()+"-"+user.getAge();
// System.out.println(info);
File file = new File("d:\\javatest\\n1.sxt");
FileOutputStream out = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(user);
oos.close();
out.close();
}
}
|
|
package cn.sxt04.serializable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Test02 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file = new File("d:\\javatest\\n1.sxt");
FileInputStream in = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(in);
User user = (User) ois.readObject();
System.out.println(user);
}
}
|
|
Exception in thread "main" java.io.InvalidClassException: cn.sxt04.serializable.User; local class incompatible: stream classdesc serialVersionUID = 4281284299154400224, local class serialVersionUID = 8687762707351138232
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:687)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1876)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1745)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2033)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
at cn.sxt04.serializable.Test02.main(Test02.java:19)
|
![]() |
|
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private transient String pwd;
private int age;
private String phone;
|
15_IO流的更多相关文章
- day20<IO流>
IO流(IO流概述及其分类) IO流(FileInputStream) IO流(read()方法返回值为什么是int) IO流(FileOutputStream) IO流(FileOutputStre ...
- 【Java基础】【22IO(其他流)&Properties】
22.01_IO流(序列流)(了解) 1.什么是序列流 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推. 2.使用方 ...
- 【Java基础】【21IO(字符流)&字符流其他内容&递归】
21.01_IO流(字符流FileReader) 1.字符流是什么 字符流是可以直接读写字符的IO流 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写 ...
- java-IO流-其他流
###22.01_IO流(序列流)(了解) * 1.什么是序列流 * 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, ...
- 阶段01Java基础day22IO流03
22.01_IO流(序列流) 1.什么是序列流 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推. 2.使用方式 整合 ...
- 阶段01Java基础day21IO流02
21.01_IO流(字符流FileReader) 1.字符流是什么 字符流是可以直接读写字符的IO流 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写 ...
- day22<IO流+>
IO流(序列流) IO流(序列流整合多个) IO流(内存输出流) IO流(内存输出流之黑马面试题) IO流(对象操作流ObjecOutputStream) IO流(对象操作流ObjectInputSt ...
- day21<IO流+&FIle递归>
IO流(字符流FileReader) IO流(字符流FileWriter) IO流(字符流的拷贝) IO流(什么情况下使用字符流) IO流(字符流是否可以拷贝非纯文本的文件) IO流(自定义字符数组的 ...
- 【Java基础】【20IO(字节流)】
20.01_IO流(IO流概述及其分类) 1.概念 IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的类都在IO包中 流按流向分为两种:输入流,输出流. 流按操 ...
随机推荐
- cross-env 使用方式
cross-env能跨平台设置及使用环境变量 大多数情况下,在windows平台下使用类似于: NODE_ENV=production的命令行指令会卡住,windows平台与POSIX在使用命令行时有 ...
- 微信小程序之圆形进度条(自定义组件)
思路 使用2个canvas 一个是背景圆环,一个是彩色圆环. 使用setInterval 让彩色圆环逐步绘制. 在看我的文章前,必须先看 ,下面转的文章,因为本文是在它们基础上修改的. 它们的缺点为: ...
- Hive SQL NVL 相关函数
1.NVL函数 NVL函数的格式如下:NVL(expr1,expr2)含义是:如果oracle第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第一个参数本来的值. NVL(exp ...
- Android资源文件命名规范学习手册
[推荐] 资源文件需带模块前缀.[推荐] layout 文件的命名方式. Activity 的 layout 以 module_activity 开头 Fragment 的 layout 以 modu ...
- ISO/IEC 9899:2011 条款6.2.6——类型的表示
6.2.6 类型的表示 6.2.6.1 通用类型 1.所有类型的表示都是未指定的,除了在本小节所描述的之外. 2.除了位域(bit-field),对象由连续的一个或多个字节序列构成,这些字节序列的字节 ...
- 24 Flutter官方推荐的状态管理库provider的深入使用、初始化修改状态、父子组件同步状态
加群452892873 下载对应24课文件,运行方法,建好项目,直接替换lib目录,在往pubspec.yaml添加上一下扩展. cupertino_icons: ^0.1.2 flutter_swi ...
- java调用科大讯飞流式(websocket)语音识别接口
要使用讯飞的能力,需先注册讯飞开发平台账号(讯飞官网参见https://www.xfyun.cn/). 再创建应用,点击右上角的控制台 -> 创建新应用: 每个应用都有一个appId,由这个ap ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_12-SpringSecurityOauth2研究-JWT研究-生成私钥和公钥
3.6.3 JWT入门 Spring Security 提供对JWT的支持,本节我们使用Spring Security 提供的JwtHelper来创建JWT令牌,校验JWT令牌 等操作. 3.6.3. ...
- Qt编写控件属性设计器8-网络采集
一.前言 上一篇文章已经打通了数据源之一的串口采集,这次要说的是网络采集,网络通信目前用的最多的是三种,TCP/UDP/HTTP,其中tcp通信又包括了客户端服务端两种,tcp通信才用了多次握手机制不 ...
- 123457123457#0#-----com.yuming.YiZhiFanPai01--前拼后广--益智早教游戏记忆翻牌cym
com.yuming.YiZhiFanPai01--前拼后广--益智早教游戏记忆翻牌cym







