s-day09--字节输出流的基本使用
This commit is contained in:
@@ -1,4 +1,10 @@
|
|||||||
package com.inmind.io01;
|
package com.inmind.io01;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
IO流的分类以及所有的顶层父类
|
IO流的分类以及所有的顶层父类
|
||||||
IO流通过2种方式分类:
|
IO流通过2种方式分类:
|
||||||
@@ -14,6 +20,36 @@ package com.inmind.io01;
|
|||||||
|
|
||||||
字符输入流:Reader
|
字符输入流:Reader
|
||||||
字符输出流:Writer
|
字符输出流:Writer
|
||||||
|
--------------------------------------------------
|
||||||
|
3.字节输出流的介绍以及相关的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() 释放任何系统资源。
|
||||||
*/
|
*/
|
||||||
public class Demo01 {
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
/*
|
||||||
|
1.创建一个字节输出流对象
|
||||||
|
a.如果指定文件不存在,自动创建
|
||||||
|
b.如果指定文件存在,它会默认覆盖
|
||||||
|
c.将fos对象与指定的硬盘中的文件进行了绑定,操作fos就是在操作文件
|
||||||
|
*/
|
||||||
|
OutputStream os = new FileOutputStream("a.txt");
|
||||||
|
//2.调用写功能
|
||||||
|
//void write(int b) 将指定的字节写入此文件输出流。
|
||||||
|
// os.write(97);
|
||||||
|
//'中'是不是1个字节?char->int(字节),此处出现了精度丢失,只写出一个字节的数据45
|
||||||
|
os.write('中');
|
||||||
|
//3.释放资源
|
||||||
|
os.close();
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.io01;
|
||||||
|
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println('中'+0);//20013,超出了1个字节的取值范围的
|
||||||
|
System.out.println('中'%256);//45
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user