s-day09-字节输出流的api介绍&基本使用
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
package com.inmind.io_out01;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
IO流的分类以及所有的顶层父类
|
||||||
|
IO流通过2种方式分类:
|
||||||
|
1.按流向分:
|
||||||
|
输入流
|
||||||
|
输出流
|
||||||
|
2.按传输的数据类型分:
|
||||||
|
字节流
|
||||||
|
字符流
|
||||||
|
在java中,io流分类:
|
||||||
|
字节输入流: InputStream
|
||||||
|
字节输出流:OutputStream
|
||||||
|
|
||||||
|
字符输入流:Reader
|
||||||
|
字符输出流:Writer
|
||||||
|
-----------------------------------------------------------
|
||||||
|
字节输出流的介绍以及相关的API
|
||||||
|
在java中有一个抽象父类OutputStream表示字节输出
|
||||||
|
常用子类:FileOutputStream
|
||||||
|
构造方法:
|
||||||
|
FileOutputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。
|
||||||
|
FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
|
||||||
|
常用方法:
|
||||||
|
void write(int b) 将指定的字节写入此文件输出流。
|
||||||
|
void write(byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流。
|
||||||
|
void write(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流。
|
||||||
|
void close() 释放任何系统资源。
|
||||||
|
|
||||||
|
字节输出流的作用:将内存中的字节数据写出到硬盘的文件中
|
||||||
|
|
||||||
|
字节输出流的使用步骤:
|
||||||
|
1.创建io流对象
|
||||||
|
2.调用写方法
|
||||||
|
3.资源释放
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
/*
|
||||||
|
FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
|
||||||
|
1.创建io流对象
|
||||||
|
a.如果指定文件不存在,自动创建
|
||||||
|
b.如果指定文件存在,它会默认覆盖
|
||||||
|
c.将fos对象与指定的硬盘中的文件进行了绑定,操作fos就是在操作文件
|
||||||
|
*/
|
||||||
|
FileOutputStream fos = new FileOutputStream("a.txt");
|
||||||
|
/*
|
||||||
|
2.调用写方法
|
||||||
|
void write(int b) 将指定的字节写入此文件输出流。
|
||||||
|
*/
|
||||||
|
fos.write(98);// 48--“0” 97---“a”字节数据,在展示时,以ASCII码表,将十进制的值转换为对应的字符
|
||||||
|
/*
|
||||||
|
char先提升为int,写出字节数据到文件中【字节byte的取值范围:-128~127】,此处写出20013是不对的,
|
||||||
|
它会转换为字节输出45
|
||||||
|
*/
|
||||||
|
// fos.write('中');
|
||||||
|
//3.资源释放
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.inmind.io_out01;
|
||||||
|
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println('中');
|
||||||
|
System.out.println('中'+0);//对应的十进制值20013
|
||||||
|
System.out.println('中'%256);//45
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user