FileInputStream fis = new FileInputStream("aaa.txt"); //建立一个文件输入流对象,并关联aaa.txt int b; //定义变量,记录每次读到的字节 while((b = fis.read()) != -1) { //将每次读到的字节赋值给b并判断是不是-1 System.out.println(b); //打印每个字节 } fis.close(); //关闭流释放资源
由于字节输入流能够操做任意类型的文件,好比图片音频等,这些文件底层都是以二进制形式的存储的,若是每次读取都返回byte,有可能在读到中间的时候遇到111111111 那么这11111111是byte类型的-1,咱们的程序是遇到-1就会中止不读了,后面的数据就读不到了,因此在读取的时候用int类型接收,若是11111111会在其前面补上 24个0凑足4个字节,那么byte类型的-1就变成int类型的255了这样能够保证整个数据读完,而结束标记的-1就是int类型
package com.heima.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Demo1_FileInputStream { /** * @param args * @throws IOException * read()方法读取的是一个字节,为何返回是int,而不是byte * * 00010100 00100100 01000001 11111111 0000000 * * 10000001 byte类型-1的原码 * 11111110 -1的反码 * 11111111 -1的补码 * * 00000000 00000000 00000000 11111111 */ public static void main(String[] args) throws IOException { //demo1(); FileInputStream fis = new FileInputStream("xxx.txt"); //建立流对象 int b; while((b = fis.read()) != -1) { System.out.println(b); } fis.close(); } public static void demo1() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("xxx.txt"); //建立流对象 int x = fis.read(); //从硬盘上读取一个字节 System.out.println(x); int y = fis.read(); System.out.println(y); int z = fis.read(); System.out.println(z); int a = fis.read(); System.out.println(a); int b = fis.read(); System.out.println(b); fis.close(); //关流释放资源 } }
FileOutputStream fos = new FileOutputStream("bbb.txt"); //若是没有bbb.txt,会建立出一个 //fos.write(97); //虽然写出的是一个int数,可是在写出的时候会将前面的24个0去掉,因此写出的是一个byte fos.write(98); fos.write(99); fos.close();
FileOutputStream fos = new FileOutputStream("bbb.txt",true); //若是没有bbb.txt,会建立出一个 //fos.write(97); //虽然写出的是一个int数,可是在写出的时候会将前面的24个0去掉,因此写出的一个byte fos.write(98); fos.write(99); fos.close();
package com.heima.stream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo2_FileOutputStream { /** * @param args * @throws IOException * FileOutputStream在建立对象的时候是若是没有这个文件会帮我建立出来 * 若是有这个文件就会先将文件清空 */ public static void main(String[] args) throws IOException { //demo1(); FileOutputStream fos = new FileOutputStream("yyy.txt",true); //若是想续写就在第二个参数传true fos.write(97); fos.write(98); fos.close(); } public static void demo1() throws FileNotFoundException, IOException { FileOutputStream fos = new FileOutputStream("yyy.txt"); //建立字节输出流对象,若是没有就自动建立一个 //fos.write(97); //虽然写出的是一个int数,可是到文件上的是一个字节,会自动去除前三个8位 //fos.write(98); //fos.write(99); fos.write(100); fos.close(); } }
FileOutputStream写出java
FileInputStream fis = new FileInputStream("致青春.mp3"); //建立输入流对象,关联致青春.mp3 FileOutputStream fos = new FileOutputStream("copy.mp3");//建立输出流对象,关联copy.mp3 int b; while((b = fis.read()) != -1) { fos.write(b); } fis.close(); fos.close();
package com.heima.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo3_Copy { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //demo1(); //demo2(); //demo3(); } public static void demo3() throws FileNotFoundException, IOException { //第二种拷贝,不推荐使用,由于有可能会致使内存溢出 FileInputStream fis = new FileInputStream("致青春.mp3"); //建立输入流对象,关联致青春.mp3 FileOutputStream fos = new FileOutputStream("copy.mp3"); //建立输出流对象,关联copy.mp3 //int len = fis.available(); //System.out.println(len); byte[] arr = new byte[fis.available()]; //建立与文件同样大小的字节数组 fis.read(arr); //将文件上的字节读取到内存中 fos.write(arr); //将字节数组中的字节数据写到文件上 fis.close(); fos.close(); } public static void demo2() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("致青春.mp3"); //建立输入流对象,关联致青春.mp3 FileOutputStream fos = new FileOutputStream("copy.mp3"); //建立输出流对象,关联copy.mp3 int b; while((b = fis.read()) != -1) { //在不断的读取每个字节 fos.write(b); //将每个字节写出 } fis.close(); //关流释放资源 fos.close(); } public static void demo1() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("双元.jpg"); //建立输入流对象,关联双元.jpg FileOutputStream fos = new FileOutputStream("copy.jpg"); //建立输出流对象,关联copy.jpg int b; while((b = fis.read()) != -1) { //在不断的读取每个字节 fos.write(b); //将每个字节写出 } fis.close(); //关流释放资源 fos.close(); } }
弊端:有可能会内存溢出设计模式
FileInputStream fis = new FileInputStream("致青春.mp3"); FileOutputStream fos = new FileOutputStream("copy.mp3"); byte[] arr = new byte[fis.available()]; //根据文件大小作一个字节数组 fis.read(arr); //将文件上的全部字节读取到数组中 fos.write(arr); //将数组中的全部字节一次写到了文件上 fis.close(); fos.close();
A:案例演示数组
FileInputStream fis = new FileInputStream("致青春.mp3");ui
FileOutputStream fos = new FileOutputStream("copy.mp3");加密
int len; byte[] arr = new byte[1024 * 8]; //自定义字节数组spa
while((len = fis.read(arr)) != -1) { //fos.write(arr); fos.write(arr, 0, len); //写出字节数组写出有效个字节个数 }设计
fis.close(); fos.close();code
package com.heima.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo4_ArrayCopy { /** * @param args * 第三种拷贝 * 定义小数组 * @throws IOException */ public static void main(String[] args) throws IOException { //demo1(); //demo2(); FileInputStream fis = new FileInputStream("致青春.mp3"); FileOutputStream fos = new FileOutputStream("copy.mp3"); byte[] arr = new byte[1024 * 8]; int len; while((len = fis.read(arr)) != -1) { //若是忘记加arr,返回的就不是读取的字节个数,而是字节的码表值 fos.write(arr,0,len); } fis.close(); fos.close(); } public static void demo2() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("xxx.txt"); FileOutputStream fos = new FileOutputStream("yyy.txt"); byte[] arr = new byte[2]; int len; while((len = fis.read(arr)) != -1) { fos.write(arr,0,len); } fis.close(); fos.close(); } public static void demo1() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("xxx.txt"); byte[] arr = new byte[2]; int a = fis.read(arr); //将文件上的字节读取到字节数组中 System.out.println(a); //读到的有效字节个数 for (byte b : arr) { //第一次获取到文件上的a和b System.out.println(b); } System.out.println("-----------------------"); int c = fis.read(arr); System.out.println(c); for (byte b : arr) { System.out.println(b); } fis.close(); } }
D.拷贝的代码视频
FileInputStream fis = new FileInputStream("致青春.mp3"); //建立文件输入流对象,关联致青春.mp3 BufferedInputStream bis = new BufferedInputStream(fis); //建立缓冲区对fis装饰 FileOutputStream fos = new FileOutputStream("copy.mp3"); //建立输出流对象,关联copy.mp3 BufferedOutputStream bos = new BufferedOutputStream(fos); //建立缓冲区对fos装饰 int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close(); //只关装饰后的对象便可 bos.close();
E.小数组的读写和带Buffered的读取哪一个更快?对象
package com.heima.stream; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo5_BufferCopy { /** * @param args * @throws IOException * close方法 * 具有刷新的功能,在关闭流以前,就会先刷新一次缓冲区,将缓冲区的字节全都刷新到文件上,再关闭,close方法刷完以后就能写了 * flush方法? * 具有刷新的功能,刷完以后还能够继续写 */ public static void main(String[] args) throws IOException { //demo1(); //flush和close方法的区别 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("致青春.mp3")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.mp3")); int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close(); bos.close(); } public static void demo1() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("致青春.mp3"); //建立输入流对象,关联致青春.mp3 FileOutputStream fos = new FileOutputStream("copy.mp3"); //建立输出流对象,关联copy.mp3 BufferedInputStream bis = new BufferedInputStream(fis); //建立缓冲区对象,对输入流进行包装让其变得更增强大 BufferedOutputStream bos = new BufferedOutputStream(fos); int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close(); bos.close(); } }
package com.heima.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo6_Chinese { /** * @param args * * 字节流读取中文的问题 * 字节流在读中文的时候有可能会读到半个中文,形成乱码 * 字节流写出中文的问题 * 字节流直接操做的字节,因此写出中文必须将字符串转换成字节数组 * 写出回车换行 write("\r\n".getBytes()); * @throws IOException */ public static void main(String[] args) throws IOException { //demo1(); FileOutputStream fos = new FileOutputStream("zzz.txt"); fos.write("我读书少,你不要骗我".getBytes()); fos.write("\r\n".getBytes()); fos.close(); } public static void demo1() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("yyy.txt"); byte[] arr = new byte[4]; int len; while((len = fis.read(arr)) != -1) { System.out.println(new String(arr,0,len)); } fis.close(); } }
try finally嵌套
FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("aaa.txt"); fos = new FileOutputStream("bbb.txt"); int b; while((b = fis.read()) != -1) { fos.write(b); } } finally { try { if(fis != null) fis.close(); }finally { if(fos != null) fos.close(); } }
package com.heima.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo7_TryFinally { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //demo1(); try( FileInputStream fis = new FileInputStream("xxx.txt"); FileOutputStream fos = new FileOutputStream("yyy.txt"); MyClose mc = new MyClose(); ){ int b; while((b = fis.read()) != -1) { fos.write(b); } } } public static void demo1() throws FileNotFoundException, IOException { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("xxx.txt"); fos = new FileOutputStream("yyy.txt"); int b; while((b = fis.read()) != -1) { fos.write(b); } }finally { try{ if(fis != null) fis.close(); }finally { //try fianlly的嵌套目的是能关一个尽可能关一个 if(fos != null) fos.close(); } } } } class MyClose implements AutoCloseable { public void close() { System.out.println("我关了"); } }
try close
try( FileInputStream fis = new FileInputStream("aaa.txt"); FileOutputStream fos = new FileOutputStream("bbb.txt"); MyClose mc = new MyClose(); ){ int b; while((b = fis.read()) != -1) { fos.write(b); } }
给图片加密
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.jpg")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.jpg")); int b; while((b = bis.read()) != -1) { bos.write(b ^ 123); } bis.close(); bos.close();
package com.heima.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Test1 { /** * @param args * @throws IOException * 将写出的字节异或上一个数,这个数就是密钥,解密的时候再次异或就能够了 */ public static void main(String[] args) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg")); int b; while((b = bis.read()) != -1) { bos.write(b ^ 123); } bis.close(); bos.close(); } }
在控制台录入文件的路径,将文件拷贝到当前项目下
Scanner sc = new Scanner(System.in); System.out.println("请输入一个文件路径"); String line = sc.nextLine(); //将键盘录入的文件路径存储在line中 File file = new File(line); //封装成File对象 FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getName()); int len; byte[] arr = new byte[8192]; //定义缓冲区 while((len = fis.read(arr)) != -1) { fos.write(arr,0,len); } fis.close(); fos.close();
package com.heima.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class Test2 { /** * 在控制台录入文件的路径,将文件拷贝到当前项目下 * * 分析: * * 1,定义方法对键盘录入的路径进行判断,若是是文件就返回 * 2,在主方法中接收该文件 * 3,读和写该文件 * @throws IOException */ public static void main(String[] args) throws IOException { File file = getFile(); //获取文件 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName())); int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close(); bos.close(); } /* * 定义一个方法获取键盘录入的文件路径,并封装成File对象返回 * 1,返回值类型File * 2,参数列表无 */ public static File getFile() { Scanner sc = new Scanner(System.in); //建立键盘录入对象 System.out.println("请输入一个文件的路径:"); while(true) { String line = sc.nextLine(); //接收键盘录入的路径 File file = new File(line); //封装成File对象,并对其进行判断 if(!file.exists()) { System.out.println("您录入的文件路径不存在,请从新录入:"); }else if(file.isDirectory()) { System.out.println("您录入的是文件夹路径,请从新录入:"); }else { return file; } } } }
将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出
Scanner sc = new Scanner(System.in); FileOutputStream fos = new FileOutputStream("text.txt"); System.out.println("请输入:"); while(true) { String line = sc.nextLine(); if("quit".equals(line)) break; fos.write(line.getBytes()); fos.write("\r\n".getBytes()); } fos.close();
package com.heima.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class Test3 { /** * 将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出 * * 分析: * 1,建立键盘录入对象 * 2,建立输出流对象,关联text.txt文件 * 3,定义无限循环 * 4,遇到quit退出循环 * 5,若是不quit,就将内容写出 * 6,关闭流 * @throws IOException */ public static void main(String[] args) throws IOException { //1,建立键盘录入对象 Scanner sc = new Scanner(System.in); //2,建立输出流对象,关联text.txt文件 FileOutputStream fos = new FileOutputStream("text.txt"); System.out.println("请输入数据:"); //3,定义无限循环 while(true) { String line = sc.nextLine(); //将键盘录入的数据存储在line中 //4,遇到quit退出循环 if("quit".equals(line)) { break; } //5,若是不quit,就将内容写出 fos.write(line.getBytes()); //字符串写出必须转换成字节数组 fos.write("\r\n".getBytes()); } //6,关闭流 fos.close(); } }