字节输出流(output):写文件
OutPutStream是抽象类(不能实例化对象)并且是所有输出流的父类
一次写入一个字节(一字节是8个二进制位,1byte=8bit)
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
/*
* 写入文件的流程:
* 1.绑定要写入的文件 或 文件路径
* 2.使用write方法写入
* 3.关闭资源
*/
public class output_exmaple {
public static void main(String[] args) throws IOException {
// 该路径下没有该文件会帮你创建出这个文件
// 如果已经有了该文件文件会被覆盖
// 绑定数据的目的地(绑定要写入的文件)
File file=new File("C:\\Users\\youda\\Desktop\\output.txt");
FileOutputStream oStream=new FileOutputStream(file);
// 写入数据 按ASCII表的值输入的
oStream.write(48);
// 用字节数组写入
byte[] b= {65,66,67,68};
oStream.write(b);
// 按字节数组的索引和长度写入
oStream.write(b,0,3);
//简单的写法
oStream.write("hello".getBytes());
oStream.write("world".getBytes());
oStream.write("Hello world".getBytes());
//关闭资源
oStream.close();
}
}
运行结果:output.txt文件中依次写入上面的字符
文件的续写和换行:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
public class Xuxie {
public static void main(String[] args) throws IOException {
File file=new File("C:\\Users\\youda\\Desktop\\output.txt");
FileOutputStream oStream=new FileOutputStream(file,true);
oStream.write("hello navy Lu\n".getBytes());
oStream.close();
}
}
异常处理
IO发生异常,都需要停止程序,修改代码
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Output_example {
public static void main(String[] args) {
File file=new File("C:\\Users\\youda\\Desktop\\output.txt");
// 增加作用域
FileOutputStream oStream=null;
try {
// 创建了一个流
oStream=new FileOutputStream(file);
oStream.write("hello".getBytes());
}catch(FileNotFoundException e) {
// 抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("文件找不到");
}catch (IOException e) {
// 文件编写失败 直接停止掉程序
throw new RuntimeException("文件编写失败");
}finally {
//关闭资源
try {
// 非空判断
// 如果是空的 说明流创建失败 失败就不需要关闭
if(oStream!=null) {
// 只要是你手动创建的流你就需要手动关闭
oStream.close();
}
}catch(IOException e) {
// 关闭资源失败 直接停止掉程序
throw new RuntimeException("关闭失败");
}
}
}
}
字节输入流(Input):读文件
InputStream是抽象类不能实例化对象并且是所有输入流的父类
读取也是一个字节一个字节的读
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
/*
* 读取文件流程
1.绑定数据源文件
2.使用read方法读
3.关闭资源
*
*/
public class Input_example {
public static void main(String[] args) throws IOException {
//input.txt文件中写着hello这几个字母
File file=new File("C:\\Users\\youda\\Desktop\\input.txt");
FileInputStream iStream=new FileInputStream(file);
//这段代码会把input.txt文件中的内容一个字节一个字节的读出来
int i1=iStream.read();
//转化为字符
System.out.println((char)i1);
i1=iStream.read();
System.out.println((char)i1);
i1=iStream.read();
System.out.println((char)i1);
i1=iStream.read();
System.out.println((char)i1);
i1=iStream.read();
System.out.println((char)i1);
i1=iStream.read();
System.out.println((char)i1);
i1=iStream.read();
System.out.println((char)i1);
// 当读取到文件末尾的时候 会返回-1
// 相当于 你读到-1 文件都读完了
i1=iStream.read();
System.out.println(i1);
// 关闭资源
iStream.close();
}
}
输出结果:
h
e
l
l
o
-1
循环读取:
注意:read方法每调用一次就读取一个字节,循环的时候read方法只能出现一次
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Xuhuanduqu {
public static void main(String[] args) throws IOException {
File file=new File("C:\\Users\\youda\\Desktop\\input.txt");
FileInputStream iStream=new FileInputStream(file);
int num=0;
while((num=iStream.read())!=-1) {
System.out.println((char)num);
}
iStream.close();
}
}
使用字节数组读取文件:(这个例子没看懂)
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
public class byteshuzu {
public static void main(String[] args) throws IOException {
//input.txt中的内容为hello
// 使用字节数组读取文件
File file=new File("C:\\Users\\youda\\Desktop\\input.txt");
FileInputStream iStream=new FileInputStream(file);
// 声明了一个长度为2的 空的 字节数组
// 读取时 会把读出来的内容放进字节数组中 存储
byte[] b=new byte[2];// 2 有效读取个数
// 利用字符串的构造方法直接打印字符串
int i1=iStream.read(b);
System.out.println(i1);
// 利用字符串的构造方法直接打印字符串
System.out.println(new String(b));
i1=iStream.read(b);
System.out.println(i1);
// 利用字符串的构造方法直接打印字符串
System.out.println(new String(b));
i1=iStream.read(b);
System.out.println(i1);
// 利用字符串的构造方法直接打印字符串
System.out.println(new String(b));
i1=iStream.read(b);
System.out.println(i1);
// 利用字符串的构造方法直接打印字符串
System.out.println(new String(b));
iStream.close();
}
}
运行结果:
2
he
2
ll
1
ol
-1
ol
InputStream类中三个read()方法的区别:
1.read
这个方法是对这个流一个一个字节的读,返回的int就是这个字节的int表示方式
2.read(byte[] b)
这个方法是先规定一个数组长度,将这个流中的字节缓冲到数组b中,返回的这个数组中的字节个数,这个缓冲区没有满的话,则返回真实的字节个数,到未尾时都返回-1
3.read(byte[] b, int off, int len)
此方法其实就是多次调用了read()方法
字节的输入输出流练习:
使用字节的输入输出流进行文件的复制:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class lianxi01 {
public static void main(String[] args) throws IOException {
long start=System.currentTimeMillis();
FileInputStream iStream=null;
FileOutputStream oStream=null;
try {
iStream=new FileInputStream("C:\\Users\\youda\\Desktop\\input.txt"); //Input中的内容是hello word!
oStream=new FileOutputStream("C:\\Users\\youda\\Desktop\\output.txt"); //Output中的内容是hello
// 读一个字节 写一个字节
/*
int len=0;
while((len=iStream.read())!=-1){
// 直接把读出来的写进文件
oStream.write(len);
}*/
// 用字节数组的方式读写 利用缓冲数组读写 效率较高
byte[] b=new byte[1024];
int num=0;
while((num=iStream.read(b))!=-1) {
oStream.write(b,0,num);
}
}catch(FileNotFoundException e) {
throw new RuntimeException("文件找不到");
}catch(IOException e) {
throw new RuntimeException("文件复制失败");
}finally {
// 这时读取和写入都完事了 先关哪个都可以
try {
if(iStream!=null) {
iStream.close();
}
}catch(IOException e) {
throw new RuntimeException("关闭资源失败");
}finally {
// 写一起的话 如果第一个报了异常 第二个流会无法关闭
try {
if(oStream!=null) {
oStream.close();
}
}catch(IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
// 获取结束时间
long end=System.currentTimeMillis();
System.out.println(end-start);
//程序运行结束input和output中的内容都变成了hello word!
}
}
将一个文件夹复制到另一个文件夹: