0226 字节流
通过可控制台对文件进行读写操作
字节流
1、字节输出流OutputStream,该动作是对文件进行写入方法
该类是一个抽象类,不能创建类对象,所以我们要创建其子类对象 FileOutputStream
先看其构造方法
FileOutputStream(File file);
FileOutputStream(String name);
这两个构造方法就是用来明确文件夹的位置 ,要写入数据,先明确要写入数据的文件位置在哪。
该文件的文件路径 如果不存在该文件,那么执行一次就会创建一次该文件,并且覆盖已有的文件,写入数据时,也会覆盖数据内容,如果想续写,就要在构造方法传的参数后加一个布尔值true就是开启续写功能,默认不写就是不续写
常用类方法
write(int b)该方法是一个字节一个字节的写入到文件中
write(byte【】 byte)该方法是一个字节数组写入到文件中
write(byte【】int a,int b)该方法是将一个字节数组从a开始到b结束的数据写入到文件中
close()释放资源
代码展示 一个字节一个字节的写入文件,一个字节数组写入文件
public static void main(String[] args) throws InterruptedException, IOException {
//明确目的地
//只要没有就创建,只要有就覆盖
File file=new File("F:\\io1127\\demo11.txt");
FileOutputStream fos=new FileOutputStream(file,true);
//换行
fos.write("\r\n".getBytes());
//将字节写入文件
fos.write(49);
fos.write(48);
fos.write(48);
//将字节数组写入文件
byte[] bytes={65,66,67,68};
fos.write(bytes);
fos.write(bytes, 2,2 );
//换行
fos.write("\r\n".getBytes());
//字符串转字节数组
fos.write("你好".getBytes());
//释放资源
fos.close();
}
在上述代码中 我们写入的int值是字节,那么存到文件中是字符串,比如字节49代表的是十进制的1,字节65代表a,如果我们想写入字符“你好”,那我们需要将字符串“你好”转为字节用到getBytes()方法。字节转为字符串是用到 new String()
那么上述代码中我们将方法中抛的异常都上抛了,那我们解决异常
public static void main(String[] args){
//明确目的地
//只要没有就创建,只要有就覆盖
File file=new File("F:\\io1127\\demo11.txt");
FileOutputStream fos=null;
try {
fos = new FileOutputStream(file,true);
//换行
fos.write("\r\n".getBytes());
//将字节写入文件
fos.write(49);
fos.write(48);
fos.write(48);
//将字节数组写入文件
byte[] bytes={65,66,67,68};
fos.write(bytes);
fos.write(bytes, 2,2 );
//字符串转字节数组
fos.write("你好".getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//释放资源
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、字节输入流 InputStream 该动作是对文件中的数据进行读取操作
该类是一个抽象类,不能创建类对象,所以我们要创建其子类对象 FileInputStream
先看其构造方法
FileInputStream(File file);
FileInputStream(String name);
这两个构造方法就是用来明确文件夹的位置 ,要读取数据,先明确要读取数据的文件位置在哪。
常用类方法
read(int b)一个字节一个字节读取
read(byte【】byte)一个字节数组读取
代码展示一个字节一个字节读取
public static void main(String[] args) throws IOException {
//明确数据源
FileInputStream fis=new FileInputStream("F:\\io1127\\demo11.txt");
//读取 一个字节
int len=0;
while((len=fis.read())!=-1){
System.out.print((char)len);
}
System.out.println();
//释放资源
fis.close();
}
代码展示字节数组读取
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("F:\\io1127\\demo11.txt");
//创建字节数组
byte[] bytes=new byte[2];
//读取一个字节数组
int len=0;
while((len=fis.read(bytes))!=-1){
System.out.print(new String(bytes,0,len));
}
//释放资源
fis.close();
}
代码展示复制文件,先读取要被复制的文件 再写入到文件中
一个字节一个字节读取
public static void main(String[] args) throws IOException {
//明确读取位置
FileInputStream fis=new FileInputStream("F:\\io1127\\demo11.txt");
//明确目的地
FileOutputStream fos=new FileOutputStream("F:\\io1127\\a\\demo11.txt");
//开始复制 一个字节一个字节复制
int len=0;
while((len=fis.read())!=-1){
fos.write(len);
}
//释放资源
fis.close();
fos.close();
}
字节数组读取
public static void main(String[] args) throws IOException {
//明确读取位置
FileInputStream fis=new FileInputStream("F:\\io1127\\demo11.txt");
//明确目的地
FileOutputStream fos=new FileOutputStream("F:\\io1127\\a\\demo11.txt");
//字节数组复制
byte[] bytes=new byte[2];
int len=0;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
//释放资源
fis.close();
fos.close();
}

浙公网安备 33010602011771号