java-字节流练习(新手)


参考手册:

 


关键字:

FileInputStream()  Input是从硬盘到内存
FileOutputStream() 而output是从内存到硬盘,所以实现了复制粘贴。
read()  调用方法读取
 换行

实例:
  1 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
  2 
  3 import java.io.*;
  4 
  5 public class ZJLlx {
  6     public static void main(String[] args) throws IOException {
  7         lx6();
  8     }
  9 
 10     private static void lx6() {
 11         //currentTimeMillis 返回以毫秒为单位的当前时间。
 12         //计算开始时间。
 13         long s = System.currentTimeMillis();
 14         FileInputStream fis = null;
 15         FileOutputStream fos = null;
 16         try {
 17             fis = new FileInputStream("C:\bxd.AVI");
 18             fos = new FileOutputStream("D:\bxd.AVI");
 19             //创建一个字节数组,他的字节传输速度是1024*10。
 20             byte[] b = new byte[1024*10];
 21             //定义一个数据类型。
 22             int len = 0;
 23             //写入判断语句。按照字节数组的传输速度进行传送。
 24             while ((len=fis.read(b))!=-1){
 25                 //按照b的速度,从0开始,一直到结束。
 26                 fos.write(b,0,len);
 27             }
 28         } catch (IOException e) {
 29             e.printStackTrace();
 30         }
 31         //最终执行的代码段。
 32         finally {
 33             //判断是否执行,如果执行过之后就结束释放资源。
 34             //如果没有执行过,就跳过If语句。
 35             if (fis!=null){
 36                 try {
 37                     fis.close();
 38                 } catch (IOException e) {
 39                     e.printStackTrace();
 40                 } finally{
 41                     if (fos!=null){
 42                         try {
 43                             fos.close();
 44                         } catch (IOException e) {
 45                             e.printStackTrace();
 46                         }
 47                     }
 48                 }
 49             }
 50             //计算结束时间。
 51             long e = System.currentTimeMillis();
 52             //打印。
 53             System.out.println(e-s);
 54         }
 55     }
 56 
 57     private static void lx5() {
 58         //设置对象。
 59         FileInputStream fis = null;
 60         FileOutputStream fos = null;
 61         try {
 62             //指定要复制的文件及路径。Input是从硬盘到内存。
 63             fis = new FileInputStream("C:\ja.txt");
 64             //指定要粘贴的文件及路径。而output是从内存到硬盘,所以实现了复制粘贴。
 65             fos = new FileOutputStream("D:\ja.txt");
 66             //定义一个数据类型。
 67             int len = 0;
 68             //定义判断条件。
 69             while ((len = fis.read())!=-1){
 70                 fos.write(len);
 71             }
 72         } catch (IOException e) {
 73             e.printStackTrace();
 74         }
 75         //最终执行的代码段。
 76         finally {
 77             //判断是否执行,如果执行过之后就结束释放资源。
 78             //如果没有执行过,就跳过If语句。
 79             if(fos!=null){
 80                 try {
 81                     fos.close();
 82                 } catch (IOException e) {
 83                     e.printStackTrace();
 84                 }
 85                 //最终执行的代码段。
 86                 finally {
 87                     //判断是否执行,如果执行过之后就结束释放资源。
 88                     //如果没有执行过,就跳过If语句,不进行判断。
 89                     if (fis!=null){
 90                         try {
 91                             fis.close();
 92                         } catch (IOException e) {
 93                             throw new RuntimeException("释放资源失败。");
 94                         }
 95                     }
 96                 }
 97             }
 98         }
 99     }
100 
101     public static void lx4() {
102         //FileInputStream 从硬盘到内存,进行显示。
103         try {
104             FileInputStream fis = new FileInputStream("D:\ja.txt");
105             byte[] b = new byte[1024];
106             int len = 0;
107             while ((len = fis.read(b))!=-1){
108                 System.out.println(new String(b,0,len));
109             }
110         } catch (IOException e) {
111             e.printStackTrace();
112         }
113 
114     }
115 
116     public static void lx3() {
117         //读写指定文件的内容。
118         //调用方法读取 read
119         try {
120             FileInputStream fis = new FileInputStream("D:\ja.txt");
121             int len = 0;
122             while((len=fis.read())!=-1){
123                 System.out.println((char)len);
124             }
125         } catch (IOException e) {
126             e.printStackTrace();
127         }
128 
129     }
130 
131     private static void lx2() throws IOException {
132         //FileOutputStream 从内存写入到硬盘,但是不会在控制台显示。
133         File file = new File("D:\ja.txt");
134         FileOutputStream fos = new FileOutputStream(file,true);
135         fos.write("Hello 
".getBytes());
136         fos.write("
qy
97".getBytes());
137         fos.close();
138     }
139 
140     public static void lx1() throws IOException {
141         FileOutputStream fos = new FileOutputStream("D:\ja.txt");
142         fos.write(97);
143         byte[] b = {66,9,45,12,48};
144         fos.write(b,1,4);
145         fos.write("Show Arrays".getBytes());
146         fos.close();
147     }
148 
149 }

打印结果:

lx1:


lx2:


lx3(读写指定文件的内容):

a
	
-

0
S
h
o
w
 
A
r
r
a
y
s
H
e
l
l
o
 






q
y



9
7

lx4:

a	-0Show ArraysHello 

qy
97

lx5:


lx6:

原文地址:https://www.cnblogs.com/lxr521/p/10596450.html