黑马程序员---IO流(二)

来源:互联网 发布:软件下载源码 编辑:程序博客网 时间:2024/05/18 10:39

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

一、字节流

1,字节流的基本对象:InputStream,OutputStream。

BufferedInputStream,BufferedOutputStream。

package 博客9_IO流二;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class 字节流读写Demo {public static void main(String[] args) throws IOException{//WriteFile();//ReadFile_1();//ReadFile_2();ReadFile_3();}public static void ReadFile_3() throws IOException{FileInputStream fis = new FileInputStream("Write.txt");byte [] by = new byte[fis.available()];//定义一个大小刚好的缓冲区,但是这样做容易内存溢出,需要小心fis.read(by);System.out.println(new String(by));fis.close();}public static void ReadFile_2() throws IOException{FileInputStream fis = new FileInputStream("Write.txt");byte [] by = new byte[1024];int len = 0;while((len=fis.read(by))!=-1){System.out.println(new String(by,0,len));}fis.close();}public static void ReadFile_1()throws IOException{FileInputStream fis = new FileInputStream("Write.txt");int ch = 0;while((ch=fis.read())!=-1){System.out.println((char)ch);}fis.close();}public static void WriteFile()throws IOException{FileOutputStream fops = new FileOutputStream("Write.txt");fops.write("abcdefg".getBytes());//注意:字节流在写入的时候和字符流不同,即便是不刷新,写入文档里也会有内容。fops.close();}}
2,自定义字节流缓冲区。
<pre name="code" class="java" style="font-size: 14px;">package 博客9_IO流二;import java.io.*;class MyBufferedInputStream{private InputStream in;private int count=0,pos=0;private byte[] buf = new byte[1024];MyBufferedInputStream(InputStream in){this.in = in;}public int myRead() throws IOException{//通过in对象读取数据,并存储到硬盘中if(count == 0){count = in.read(buf);if(count<0)return -1;pos=0;byte b = buf[pos];count--;pos++;return b&255;//在返回值的前面补0,否则会复制失败}else if(count>0){byte b = buf[pos];count--;pos++;return b&255;}return -1;}public void Myclose() throws IOException {// TODO Auto-generated method stubin.close();}}class MyBufferedInputStreamText{public static void main(String[] args)throws IOException{copy();}public static void copy() throws IOException{MyBufferedInputStream mbr = new MyBufferedInputStream(new FileInputStream("HashSetDemo.java"));BufferedOutputStream bufs = new BufferedOutputStream(new FileOutputStream("Writer_2.java"));int b = 0;while((b=mbr.myRead())!=-1){bufs.write(b);}mbr.Myclose();bufs.close();}}

<strong><span style="font-size:18px;">二、读取键盘录入</span></strong>
1,读取键盘录入:

System.out:对应的是标准的输出设备,控制台。

System.in:对应的是标准输入设备,键盘。

package 博客9_IO流二;import java.io.IOException;import java.io.InputStream;/* * 需求:当录入一行数据后,就将该数据打印,如果录入的是“over”的话,则程序停止 * */public class 键盘录入{public static void main(String[] args)throws IOException{InputStream in = System.in;StringBuilder sb = new StringBuilder();while(true){int ch = in.read();if(ch=='\r'){continue;}if(ch=='\n'){String s = sb.toString();if("over".equals(s))break;System.out.println(s.toUpperCase());sb.delete(0,sb.length());}elsesb.append((char)ch);}}}
2,读取转换流:用转换流的方式简化上述代码。
package 博客9_IO流二;import java.io.*;public class 转换流 {public static void main(String[] args)throws IOException {// TODO Auto-generated method stub//获取键盘录入对象,使用转换流,并将字符串存入缓冲区BufferedReader bufr= new BufferedReader(new InputStreamReader(System.in));String line = null;while((line=bufr.readLine())!=null){if("over".equals(line))break;System.out.println(line.toUpperCase());}bufr.close();}}
3,流操作的基本规律:通过两个明确来完成。

a,明确源和目的。

源:输入流。InputStream Reader。

目的:输出流。OutputStream Writer。

b,操作的数据是否是纯文本。

是:字符流

不是:字节流

c:当体系明确后,再明确要使用哪个具体的对象,并通过设备来区分。

源设备:内存,硬盘,键盘。

目的设备:内存,硬盘,控制台。
三、File类简述

File类:用来将文件或者文件夹封装成对象的类,方便对文件与文件夹的属性信息进行操作。File对象可以作为参数传递给流的构造函数。

File类的特点:可以将已有的或者还没有的文件或者文件夹封装成对象。

创建File类对象:File f = new File("文件名“)或者File f = new File(”目录“,”文件名“)

目录分隔符:File.separator。

File类的常见方法:

1,创建:File.createNewFile();在指定位置创建文件,如果该文件已经存在,则创建失败,返回false。

mkdir();创建单级目录的文件夹。

mkdirs();创建多级目录的文件夹。

2,删除:Boolean delete();deleteOnExit();程序结束时删掉。

3,判断:canExecute();判断文件是否能执行。

boolean exists();判断文件是否存在。

isDirectory();判断是不是目录。

isFile();判断是不是文件。

注意:在判断文件对象是否是文件或者目录时,必须要 先判断该文件对象封装的内容是否存在。

4,获取信息:getName();getPath();lastModified();--->最后一次修改的时间。length();

getParent();该方法返回的是绝对路径中的父目录,如果获取的是相对路径,则返回为空。

四、递归

需求:列出指定目录下文件或者文件夹,要求包含子目录中的内容。

因为目录中还有目录,所以只要使用同一个列出目录功能的函数完成即可。在列出过程种出现的还是目录的话,可以再次调用本功能,也就是函数自身调用自身,这种编程手法,即成为递归

使用递归时应注意:

1,限定递归条件。

2,要注意递归的次数,避免内存溢出。

package 博客9_IO流二;import java.io.*;public class 递归 {public static void main(String[] args)throws IOException{File dir = new File("f:\\java学习");showDir(dir);}public static void showDir(File dir){System.out.println(dir);File [] files = dir.listFiles();for(int x = 0 ; x < files.length;x++){if(files[x].isDirectory())showDir(files[x]);//递归,再次调用showDir函数来获取子目录elseSystem.out.println(files[x]);}}}










0 0
原创粉丝点击