进阶day09-字节输出流的基本使用
This commit is contained in:
21
javaSE-day09/src/com/inmind/io01/Demo03.java
Normal file
21
javaSE-day09/src/com/inmind/io01/Demo03.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package com.inmind.io01;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//void write(int b) 将指定的字节写入此文件输出流。
|
||||||
|
/*FileOutputStream fos = new FileOutputStream("a.txt");
|
||||||
|
//输出int超出了256的值,将高位舍弃只留一个字节的内容,再按照ascii码表显示
|
||||||
|
//30000%256
|
||||||
|
fos.write('中');
|
||||||
|
fos.close();*/
|
||||||
|
|
||||||
|
System.out.println('中');
|
||||||
|
System.out.println('中'+0);
|
||||||
|
System.out.println(20000%256);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
48
javaSE-day09/src/com/inmind/io01/OutputSteamDemo02.java
Normal file
48
javaSE-day09/src/com/inmind/io01/OutputSteamDemo02.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package com.inmind.io01;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
在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 OutputSteamDemo02 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
/*
|
||||||
|
1.创建字节输出流对象
|
||||||
|
a.如果指定文件不存在,自动创建
|
||||||
|
b.如果指定文件存在,它会默认覆盖
|
||||||
|
c.将fos对象与指定的硬盘中的文件进行了绑定,操作fos就是在操作文件
|
||||||
|
*/
|
||||||
|
FileOutputStream fos = new FileOutputStream("a.txt");
|
||||||
|
//2.调用写方法
|
||||||
|
fos.write(97);//字节的取值范围是-128~127 ‘a’,但是对应应用程序使用ASCII码表进行展示
|
||||||
|
|
||||||
|
byte[] bytes = {48, 49, 50};
|
||||||
|
fos.write(bytes);
|
||||||
|
|
||||||
|
fos.write(bytes,1,2);
|
||||||
|
|
||||||
|
//3.资源释放
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user