字节流;
FileOutputStream
FileInputStream
很多用法和字符流类似效果。。。
FileOutputStream
字节流简单写的操作;
package io2;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Angus
* 字节流
* FileOutputStream
* FileInputStream
*
* 写的方法
* void write(byte[] b) 将 b.length 个字节从指定 byte 数组写入此文件输出流中。
void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
void write(int b) 将指定字节写入此文件输出流。
*
*/
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fo = new FileOutputStream("a.txt");
// FileOutputStream fo = new FileOutputStream("a.txt",true); //是否追加数据
fo.write(97);
fo.write(98);
// fo.flush(); 字节流基本不用
byte[] bys = {97,98,99,100,101};
byte[] bys2 = {'a',98,99,100,101}; //这样也可以
fo.write(bys);
fo.write(bys2, 0, 2);
fo.close();
}
}
FileInputStream
读取数据
package io2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author Angus
* 字节流读取数据
*
* read() 从此输入流中读取一个数据字节。
int read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
int read(byte[] b, int off, int len) 从此输入流中将最多 len 个字节的数据读入一个
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
//创建字节流对象
FileInputStream fi = new FileInputStream("d.txt");
//读取数据
//方式一
int by = 0;
// while((by=fi.read()) != -1){
// System.out.print((char)by); //这样每次一个字节读取的话,汉字会出现问题
// }
//方式二
byte[] bys = new byte[1024];
int len = 0;
while((len = fi.read(bys)) != -1){
System.out.print(new String(bys,0,len)); //但是这样也有部分问题1025出现汉字也有问题
//建议字符流读取
}
//释放资源
fi.close();
}
}
复制文件操作:
package io2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Angus
* 复制文件
* 数据源;a.txt
* 目的地:e.txt
*/
public class CopyFile {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("e.txt");
//读取
//方式一
// int by = 0;
// while((by = fis.read()) != -1){
// fos.write(by);
// }
//方式二
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1){
fos.write(bys,0,len);//如果不够1024会对出字节,需要限制写的大小
}
fos.close();
fis.close();
}
}
字符流和字节流结合复制图片:
package io2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author Angus
* 复制图片
*
* 二进制流数据:图片,音频,视频
*/
public class CopyImage {
public static void main(String[] args) throws IOException {
// method();
method2();
}
/**
* 字节流读取
*/
private static void method2() throws IOException{
FileInputStream fis = new FileInputStream("111.jpg");
FileOutputStream fos = new FileOutputStream("333.jpg");
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1){
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
/**
* 用字符流操作
* @throws IOException
*/
private static void method() throws IOException {
FileReader fr = new FileReader("111.jpg");
FileWriter fw = new FileWriter("222.jpg");
int ch = 0;
while((ch= fr.read()) != -1){
fw.write(ch); //会发现复制的图片大小发生变化了,多数据了。
//由于二进制数据合并的时候没有对应的数据,这样就会造成数据的丢失
//二进制流只能用字节流操作
}
fw.close();
fr.close();
}
}
最后总结:
凡是可以记事本打开的字符流,其它字节流
最后附上JDK使用文档API 下载
转载请注明原文地址: https://ju.6miu.com/read-14220.html