这里写目录标题
一、字节输出流使用步骤(内存到硬盘)
1、创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
2、调用FileOutputStream对象中的方法wreat,把数据写入到文件中
3、释放资源
代码1:
public class Demo01 {
public static void main(String[] args) throws IOException {
//1、创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
FileOutputStream fos =new FileOutputStream("E:\\A.txt");
//2、调用FileOutputStream对象中的方法write,把数据写入到文件中,一次写一个字节
fos.write(97);//文件中显示a
//3、释放资源
fos.close();
}
}
代码2:
public class Demo02 {
public static void main(String[] args) throws IOException {
//1、创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
FileOutputStream fos =new FileOutputStream("E:\\B.txt");
/*2、一次写多个字节
//注意:
1、如果写的第一个字节是整数(0~127),那么查询的时候会查询ASCII表
2、如果写的第一字节数负数。那么第一个字节会和第二个字节组成中文显示,查询系统默认码表
*/
// byte[] bytes={65,66,67,68};//文件中显示 ABCD
byte[] bytes={-65,-66,67,-68,79,69};//文件中显示 烤C糘E
fos.write(bytes);
fos.write(bytes,2,1);
}
}
代码3:
public class Demo03 {
public static void main(String[] args) throws IOException {
//不删除原来的内容,在原来基础上增加内容
FileOutputStream fos =new FileOutputStream("E:\\C.txt",true);
for (int i=1;i<=10;i++){
/*写入字符的方法:使用String类中的方法把字符串转换为字节数组
byte[] getBytes() 把字符串转换为字节数组
*/
fos.write("你好".getBytes());
fos.write("\r\n".getBytes());//输入换行符
}
}
}
二、字节输入流使用步骤(硬盘到内存)
1、创建FileInputStream对象,构造方法中绑定要读取的数据源
2、使用FileInputStream对象的方法read读取文件
3、释放资源
代码1(文件中内容为:abcde):
public class Demo04 {
public static void main(String[] args) throws IOException {
//1、创建FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("E:\\A.txt");
//使用FileInputStream对象的方法read读取文件中的一个字节并返回,读取到文件中的末尾返回-1
int len = fis.read();
System.out.println(len);
len =fis.read();
System.out.println(len);
len =fis.read();
System.out.println(len);
len =fis.read();
System.out.println(len);
len =fis.read();
System.out.println(len);
}
}
结果:
代码2:
public class Demo05 {
public static void main(String[] args) throws IOException {
/*上面读取文件时一个重复的过程,所以可以使用循环化
不知道文件中有多少字节所以使用while循环
while循环结束条件,读取到-1的时候结束
*/
FileInputStream fis = new FileInputStream("E://A.txt");
int len = 0;//记录到读取的字节
//一次读取一个字节
while((len = fis.read())!=-1){
System.out.println(len);
//变成字符
System.out.println((char) len);
}
fis.close();
}
}
结果:
代码3:
public class Demo06 {
public static void main(String[] args) throws IOException {
//一次读取多个字节(该文件内容为abcde)
FileInputStream fis = new FileInputStream("E://A.txt");
//int read(byte[] b)从输入流中读取一定数量的字节,把它们保存到参数b指定的字节数组中,返回的整数表示读取字节的数目。
byte[] bytes = new byte[2];
//第一次读取了两个数据a和b,返回了2,使得len变为2
int len = fis.read(bytes);
System.out.println(len);
System.out.println(new String(bytes));
//第二次依然读取了2个数据,返回了2,但是读取的c把a给覆盖掉,d把 b覆盖掉
len = fis.read(bytes);
System.out.println(len);
System.out.println(new String(bytes));
//第三次只剩下一个数据e,便只读了一个数据e,返回1,e 覆盖了c,d并没有被覆盖所以显示ed
len = fis.read(bytes);
System.out.println(len);
System.out.println(new String(bytes));
//第四次读取到了末尾所以返回-1
len = fis.read(bytes);
System.out.println(len);
System.out.println(new String(bytes));
}
}
结果:
三、文件复制(就是一读一写)
1、文件复制的步骤
1、创建一个字节输入流对象,构造方法中绑定要读取的数据源
2、创建一个字节输出流对象,构造方法中绑定要写入的目的地
3、使用字节输入对象中的read方法读取文件
4、使用字节输出流对象中的write方法,把读取到的字节写入目的地的文件中
5、释放资源
2、代码
一次读取一个字节写入一个字节的方式
public class Demo07 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\11.jpg");
FileOutputStream fos = new FileOutputStream("D:\\11.jpg");
int len=0;
while((len=fis.read())!=-1){
fos.write(len);//一次读取一个字节写入一个字节的方式
}
//先关闭写的再关闭读的
fos.close();
fis.close();
}
}
3、优化后的代码
public class Demo08 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\11.jpg");
FileOutputStream fos = new FileOutputStream("D:\\11.jpg");
//使用数组缓存读取多个字节,写入多个字节
byte[] bytes = new byte[1024];
int len=0;
while((len=fis.read(bytes))!=-1){
//使用字节输出流中的方法write,把读取到的字节写入到目的地文件中
fos.write(bytes,0,len);
}
}
}