Java学习第十七天

1.字节流:

FileInputStream(字节输入流)
特有方法:
FileInputStream fis = new FileInputStream("temp.txt");
1.int num = fis.available();//返回文件的字节总个数
2.fis.read()//返回int型数字,如果没有读到字节,返回-1

 1 //使用byte数组进行优化
 2 public static void m3()throws IOException
 3 {
 4 
 5 FileInputStream fis = new FileInputStream("temp.txt");
 6 
 7 byte[] arr=new byte[10];
 8 int len=0;
 9 while((len=fis.read(arr))!=-1){
10 
11 System.out.print(new String(arr,0,len));
12 }
13 
14 fis.close();
15 
16 
17 }
 1 //使用字节流读取文件:一次对一个字节
 2 public static void m2()throws IOException
 3 {
 4 FileInputStream fis = new FileInputStream("temp.txt");
 5 
 6 int num;
 7 while((num=fis.read())!=-1){
 8 
 9 System.out.print((char)num);
10 }
11 
12 fis.close();
13 
14 }

2.FileOutputStream(字节输出流)

方法:
FileOutputStream fos = new FileOutputStream("temp.txt");

fos.write("abc".getBytes());//byte[] getBytes() 编码
注意:1.在输出时,要在每一次输入结束后都要进行flush(),如果不刷新,则不会输出
   2.在程序结尾都要close(),关闭文件,并且close()默认自动刷新


3.字节缓冲流:BufferedInputStream BufferedOutputStream
  这几个方法都会抛出异常。

 1 例:
 2 import java.io.*;
 3 class Demo4 
 4 {
 5 public static void main(String[] args) 
 6 {
 7 //字节缓冲流:BufferedInputStream BufferedOutputStream
 8 
 9 //复制图片
10 BufferedInputStream bis = null;
11 BufferedOutputStream bos = null;
12 
13 try{
14 
15 bis = new BufferedInputStream(new FileInputStream("tou1.jpg"));
16 bos = new BufferedOutputStream(new FileOutputStream("tou1_copy.jpg"));
17 
18 byte[] arr=new byte[1024];
19 int len=0;
20 while((len=bis.read(arr))!=-1){
21 
22 bos.write(arr,0,len);
23 }
24 
25 }catch(IOException e){
26 e.printStackTrace();
27 }finally{
28 
29 if(bis!=null)
30 try{
31 bis.close();
32 }catch(IOException e){
33 throw new RuntimeException("字节缓冲读取流关闭失败");
34 }
35 
36 if(bos!=null)
37 try{
38 bos.close();
39 }catch(IOException e){
40 throw new RuntimeException("字节缓冲输出流关闭失败");
41 }
42 }
43 
44 }
45 }

4.标准输入(和键盘关联):System.in(in是一个字节输入流对象(默认关联的设备是键盘))
  //int m = kk.read();//从键盘读数据 阻塞式方法,只要不输入,会一直等待
  //System.out.print((char)m);
  标准输出(已经和控制台设备关联)System.out;(out是一个字节输出流对象(默认关联的设备是控制台))//是一个字节输出流对象


  使用System.in实现键盘输入,太麻烦
  System.in实现键盘输入的代码类似于BufferedReader的一次读一行功能的代码(readLine())
  System.in ----字节读取流

  BufferedReader---- 字符读取流

5.转换流:

InputStreamReader:是从字节流到字符流的桥
InputStreamReader(InputStream in)
OutputStreamWriter :OutputStreamWriter(OutputStream out)

 1 例:
 2 class Demo6 
 3 {
 4 public static void main(String[] args) throws IOException
 5 {
 6 //定义标准的键盘字节输入流对象,或者定义FileInputStream()对象
 7 InputStream in=System.in;
 8 
 9 //使用转换流把字节流转成字符流
10 InputStreamReader isr = new InputStreamReader(in);
11 
12 //使用字符缓冲流
13 BufferedReader br = new BufferedReader(isr);
14 
15  
16 
17 //定义标准的字节输出流对象,或者定义FileOutputStream()对象
18 PrintStream out = System.out;//是一个字节输出流对象(已经和控制台设备关联了)
19 
20 //把字节输出流转成字符输出流
21 OutputStreamWriter osw = new OutputStreamWriter(out);
22 
23 //使用字符缓冲流
24 BufferedWriter bw = new BufferedWriter(osw);
25 
26 
27 String line = null; //从键盘读不到结束标志
28 while((line=br.readLine())!=null){
29 if("over".equals(line))
30 break;
31 bw.write(line);
32 bw.newLine();
33 // bw.flush();
34 //System.out.println(line);//字节流
35 }
36 
37 
38 br.close();
39 bw.close();
40 
41 }
42 }

6.File:java把路径或文件面向对象

实例化对象:
//1.把文件或文件夹封装成File类型的对象
File file=new File("E:\\java练习\\day17练习\\tt.txt");
//2.适合文件名变化的
File file=new File("E:\\java练习\\day17练习","tt.txt");
//3.分开定义
File ff=new File("E:\\java练习\\day17练习");
File file=new File(ff,"tt.txt");

方法:
getName()//得到文件名
getPath()//得到路径,定义的是相对的则得到相对的,定义的绝对的获取绝对的
getParent()//返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
getAbsolutePath()//得到绝对路径
lastModified()//获取文件最后的修改时间
delete()//删除文件或文件夹
listRoots()//得到根目录(得到系统盘)
list()//得到某个目录下的所有文件和文件夹的名字返回的是String[]
listFile()//返回File[] 把某个目录下的所有文件和文件夹都封装成File的对象
listFile(FilenameFilter filter)


7.递归:函数自己调用自己,当调用次数太多时,会内存溢出,所有要注意,调用次数不要太多
    注意:要有调用的结束条件

8.PrintStream:字节打印流,继承了OutputStream,字节输出流的功能都具备,增加了打印的功能

向哪些设备输出(目的):
1: File类型的文件
2:OutputStream类型的字节输出流
3:字符串类型的文件名
例:ps.write(353);// 00000000 00000000 00000001 01100001---》砍掉前三个字节 01100001


9.父类是Writer
  PrintWriter:字符打印流,字符输出流的基本功能都具备,增加了打印功能
    向哪些设备输出(目的):
    1:File类型的文件
    2:字节输出流对象
    3:字符输出流对象
    4:字符串类型的文件名

 1 例:
 2 
 3 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 4 
 5 // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 6 
 7 PrintWriter pw = new PrintWriter(System.out,true);//加上第二个参数,则会自动刷新
 8 
 9 String line = null;
10 while((line=br.readLine())!=null){
11 if("over".equals(line))
12 break;
13 pw.println(line);//会输出换行
14 //pw.write(line);
15 // pw.flush();
16 }

猜你喜欢

转载自www.cnblogs.com/demain/p/11409735.html