黑马程序员——IO--Input和Output基础

来源:互联网 发布:linux安装多个jdk版本 编辑:程序博客网 时间:2024/05/21 11:27

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一 IO流:input和output

输入流和输出流相对于内存设备而言.

将外设中的数据读取到内存中:输入          将内存的数写入到外设中:输出。

字符流的由来:字节流读取文字字节数据后,不直接操作而是先查指定的编码表。获取对应的文字。

在对这个文字进行操作。简单说:字节流+编码表 =字符流

字节流的两个顶层父类:

1InputStream  2OutputStream.

字符流的两个顶层父类:

1Reader (输入)2Writer(输出)

这些体系的子类都以父类名作为后缀。 而且子类名的前缀就是该对象的功能。

记住:如果要操作文字数据,建议优先考虑字符流。

而且要将数据从内存写到硬盘上,要使用字符流中的输出流。Writer

二字符流:FileWeiter和FileReader

1、写入操作:

既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地)

如果文件不存在,则会自动创建。

如果文件存在,则会被覆盖。

如果构造函数中加入true,可以实现对文件进行续写!

   FileWriter fw=new FileWriter("demo.txt",true);

* 调用Writer对象中的write(string)方法,写入数据。

其实数据写入到临时存储缓冲区中。

 //进行刷新,将数据直接写到目的地中。           // fw.flush();

关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。   fw.close();

2、换行处理:private static final String LINE_SEPARATOR = 

        system.getProperty("line.separator");

3、异常处理:

<span style="font-size:18px;"><span style="font-size:18px;">FileWriter fr=null;try {fr=new FileWriter("d:\\1.txt");fr.write("ss");} catch (IOException e) {e.printStackTrace();} finally{if(fr!=null){  //需判断是否为空,否则空指针try {fr.close();} catch (IOException e) {e.printStackTrace(); //throw new RuntimeException(“不关”);}}}</span></span>

3、读取操作:(文件必须存在)

(1)第一种方式:

用一个读取流关联一个已存在文件。 

FileReader fr = new FileReader("demo.txt");

int ch = 0;

while((ch=fr.read())!=-1){

System.out.println((char)ch); //读到的字符

}

(2)第二种方式(推荐):

使用read(char[])读取文本文件数据。  * 先创建字符数组。

FileReader fd=new FileReader("demo.txt");

char[] buff=new char[1024];   //创建字符数组

int len=0;  

while ((len=fd.read(buff))!=-1) {

System.out.println(new String(buff,0,len));    //len  读到的字符数

}

4、复制练习:

<span style="font-size:18px;">FileReader fr= new FileReader("demo.txt");   //读取的源FileWriter fw= new FileWriter("copy.txt");   //目的char[] buff=new char[1024];      //读写int len=0;while((len=fr.read(buff))!=-1){fw.write(buff,0,len);}                                                //最后关闭</span>
5、字符流缓冲区 BufferedReader BufferedReader

<span style="font-size:18px;">//为了提高写入的效率。使用了字符流的缓冲区。FileWriter fw1=new FileWriter("demo.txt");BufferedWriter bufw1=new BufferedWriter(fw1);bufw1.write("wwwwwwwweee");bufw1.newLine();  //换行bufw1.flush();bufw1.close();   //关闭缓冲区</span>

=======================================================

<span style="font-size:18px;">FileReader fr=new FileReader("demo.txt");BufferedReader bufr=new BufferedReader(fr);String line=null;while ((line=bufr.readLine())!=null) { //一行数据,不含换行符System.out.println(line);}//readLine() 读取了缓冲区的read方法,将读取的字符缓冲并判断换行标记,将标记前的缓冲数据编程字符串返回。</span>
6、缓冲区复制
<span style="font-size:18px;">FileReader fr=new FileReader("demo.txt");BufferedReader bufr = new BufferedReader(fr);FileWriter fw1=new FileWriter("copy.txt");BufferedWriter bufw1 =new BufferedWriter(fw1);String line=null;while((line=bufr.readLine())!=null){bufw1.write(line);bufw1.newLine();bufw1.flush();}</span>

7.设置行号:

<span style="font-size:18px;">FileReader fr=new FileReader("demo.txt");LineNumberReader lnr=new LineNumberReader(fr);String line=null;lnr.setLineNumber(100); //设置行号while((line=lnr.readLine())!=null){System.out.println(lnr.getLineNumber()+":"+line);  //获取行号}</span>
三:字节流:
不仅可操作字符,还可操作其他媒体文件

1、 FileOutputStream和FileInputStream

<span style="font-size:18px;">//创建输出流对象FileOutputStream fos=newFileOutputStream("demo.txt");fos.write("sds".getBytes());  //写入数据,直接写到目的地fos.close();//创建读取流(输入流)对象 FileInputStream fis=new FileInputStream("demo.txt");</span><p><span style="font-size:18px;"><span style="color:rgb(127,0,85);">byte</span>[] buff=<span style="color:rgb(127,0,85);">new</span> <span style="color:rgb(127,0,85);">byte</span>[<span style="font-family: Arial, Helvetica, sans-serif;">1024</span><span style="font-family: Arial, Helvetica, sans-serif;">]; //字符数</span></span></p><p><span style="font-size:18px;"><span style="color: rgb(127, 0, 85);">int</span> len=0;</span></p><p><span style="font-size:18px;"><span style="color:rgb(127,0,85);">while</span> ((len=fis.read(buff))!=-1) {</span></p><p><span style="font-size:18px;">System.<span style="color:rgb(0,0,192);">out</span>.println(<span style="color:rgb(127,0,85);">new</span> String(buff,0,len));</span></p><p><span style="font-size:18px;">}</span></p>
2、复制练习:

(1)自定义缓冲区

<span style="font-size:18px;"><span style="white-space:pre"></span>FileInputStream fis=new FileInputStream("d://1.txt");FileOutputStream fos=new FileOutputStream("d://2.txt");byte[] buff = new byte[1024];  //自定义缓冲区int len=0;while ((<span style="color:#ff0000;">len=fis.read(buff))!=-1</span>) {fos.write(buff,0,len);}fos.close();  关闭流fis.close();}</span>
(2)bufferedInputStream和bufferedOutputStream

<span style="font-size:18px;"><span style="white-space:pre"></span>FileInputStream fis =new FileInputStream("d:\\1.txt");   FileOutputStream fos =new FileOutputStream("d:\\2.txt"); //缓冲 BufferedInputStream buffis=new BufferedInputStream(fis);BufferedOutputStream buffos=new BufferedOutputStream(fos); int ch=0;while ((<span style="color:#ff0000;">ch=buffis.read())!=-</span>1) {buffos.write(ch);}buffis.close();buffos.close();</span>










0 0
原创粉丝点击