缓冲流,交换流,输入输出流,打印流

常见的缓冲流:
BufffferedInputStream : 缓冲字节输入流
BufffferedOutputStream : 缓冲字节输出流
BufffferedReader : 缓冲字符输入流
BufffferedWriter : 缓冲字符输出流
缓冲字节流
try (BufferedInputStream bufferedInputStream = new
BufferedInputStream(new FileInputStream("file\\day26\\source"))) {
// 1. 实例化一个字节数组
byte[] array = new byte[1024];
// 2. 声明一个整型变量,用来记录每次读取了多少个字节数据
int length = 0;
// 3. 循环读取
while ((length = bufferedInputStream.read(array)) != -1) {
// 4. 将读取到的数据转成字符串输出到控制台
String msg = new String(array, 0, length);
System.out.println(msg);
}
}
catch (IOException e) {
e.printStackTrace();
}
// 1. 实例化一个缓冲字节输出流对象
try (BufferedOutputStream bufferedOutputStream = new
BufferedOutputStream(new FileOutputStream("file\\day26\\target"))) {
// 2. 将数据写入到输出流中
bufferedOutputStream.write("hello world".getBytes());
bufferedOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
缓冲字符流
try (BufferedReader bufferedReader = new BufferedReader(new
FileReader("file\\day26\\src"))) {
// 从流中读取数据
char[] array = new char[100];
int length = 0;
while ((length = bufferedReader.read(array)) != -1) {
System.out.print(new String(array, 0, length));
}
} catch (IOException e) {
e.printStackTrace();
}
// 借助一个字符输出流,实例化一个缓冲字符输出流对象
try (BufferedWriter bufferedWriter = new BufferedWriter(new
FileWriter("file\\day26\\dst"))) {
bufferedWriter.write("hello world");
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
BufffferedReader 类中多了一个方法 readLine()
try (BufferedReader reader = new BufferedReader(new
FileReader("file\\day26\\src"))) {
// 1. 定义一个字符串,用来接收每一行读取到的数据
String line = "";
// 2. 循环读取数据
while ((line = reader.readLine()) != null) {
// 3. 将读取到的数据输出
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
BufffferedWriter 类中多了一个方法 newLine()
try (BufferedWriter bufferedWriter = new BufferedWriter(new
FileWriter("file\\day26\\dst"))) {
bufferedWriter.write("hello world");
bufferedWriter.newLine();
bufferedWriter.write("你好,世界");
bufferedWriter.newLine();
bufferedWriter.write("end");
} catch (IOException e) {
e.printStackTrace();
}
Scanner
扫描器, 这个类最主要的作用, 是从一个文件中或者从
一个流中浏览数据。 在这个类中封装了若干个方法, 方便了数据的读取。
next()
读取一个单词,遇到空格或者换行符就不再读取了。
hasNext()
判断是否还有下一个单词可以读取。
nextLine()
读取一行内容,遇到换行符就不再读取了。
hasNextLine()
判断是否还有下一行可以读取。
// 其实,Scanner在使用结束之后,也是需要进行关闭的。 调用close方法。
try (Scanner scanner = new Scanner(new File("file\\day26\\src"))) {
// 读取文件中的内容
while (scanner.hasNextLine()) {
System.out.println(scanner.hasNextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
标准输入流
try (BufferedInputStream bis = new BufferedInputStream(System.in))
{
byte[] array = new byte[128];
int length = 0;
while ((length = bis.read(array)) != -1) {
String str = new String(array, 0, length);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
标准输出流
PrintStream original = System.out;
// PrintStream: 是一个打印流,可以将数据输出到指定位置。
try (PrintStream ps = new PrintStream(new
FileOutputStream("file\\day26\\logs", true))) {
// ps.println("hello world!");
// 重定向标准输出流
System.setOut(ps);
System.out.println("123");
}
 
转换流
输入流
private static void read() {
// 当前的项目是 utf-8, 读取的文件是 GBK
// 如果需要以指定的字符集进行文件的读取,需要使用
InputStreamReader(InputStream inputStream, String charsetName)
try (InputStreamReader reader = new InputStreamReader(new
FileInputStream("file\\day26\\src"), "GBK")) {
char[] array = new char[128];
int length = 0;
while ((length = reader.read(array)) != -1) {
System.out.println(new String(array, 0, length));
}
} catch (IOException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} finally {
System.setOut(original);
}
输出流
private static void write() {
// 以指定的字符集写数据
try (OutputStreamWriter writer = new OutputStreamWriter(new
FileOutputStream("file\\day26\\dst", true), "GBK")) {
writer.write("hello world");
writer.write("你好,世界");
} catch (IOException e) {
e.printStackTrace();
}
}
打印流
字节打印流
字节打印流支持的设备:
1.File类型的文件
2.字符串类型的文件
3.字节输出流
PrintStream p3 = new PrintStream("test7.txt");
p3.write(String.valueOf(353).getBytes());
p3.close();
字符打印流
字符打印流支持的设备:
1.File类型的文件
2.字符串类型的文件
3.字节输出流
4.字符写出流
注意点:
方法:public PrintWriter(Writer out, boolean autoFlush)
autoFlush - boolean 变量;如果为 true,则 println、printf 或 format 方法将自动刷新
输出缓冲区
但是执行print方式时需要手动刷新
PrintWriter pWriter = new PrintWriter(new FileWriter("test5.txt"));
pWriter.write("bingbing");
pWriter.close();
String s1 = "你好吧";
byte[] s1b = s1.getBytes("utf-8");
String sr1b = new String(s1b,"GBK");
System.out.println("GBK解码:"+sr1b);//浣犲ソ鍙?
//再编码
byte[] s1bb = sr1b.getBytes("GBK");
//再解码
System.out.println("utf8再编码:"+new String(s1bb,"utf-8"));//你好??
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值