您的位置:首页 > 职场人生

黑马程序员——Java IO字符流、字节流(小练习)

2014-08-02 18:34 691 查看
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

                                   
 
                   一、字符流 
1、写(FileWriter) 
Writer基类的几种常用方法: 
a) public void write(String str) : 写入字符串 

b) public void flush():刷新该流的缓冲 

c) public  void close():关闭该流,但先刷新

2、读(FileReader) 
Reader有哪些常用的方法: 
a) public int read():读取单个字符 
b) public int read(char[] buf) :将字符读入数组。 
c) public void close():关闭该流并释放与之关联的所有资源,此处没有刷新。

下面是复制文件小练习:

import java.io.*;
class CopyText
{
public static void copyText()
{
FileWriter fw=null;
FileReader fr=null;
try
{
fr=new FileReader("d:\\1.txt");
fw=new FileWriter("d:\\2.txt");
int len=0;
char[] buf = new char[1024];
while ((len=fr.read(buf))!=-1)
{
fw.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
try
{
if (fr!=null)
{
fr.close();
}
}
catch (IOException e)
{
throw new RuntimeException("读失败");
}
try
{
if (fw!=null)
{
fw.close();
}
}
catch (IOException e)
{
throw new RuntimeException("写失败");
}
}
}
public static void main(String[] args)
{
copyText();
}
}
 
                       
 
                    二、字符流

字节流主要操作byte类型数据,以byte数组为准,主要操作类有InputStream(字节输入流)、OutputSteam(字节输出流)由于IputStream和OutputStream都是抽象类,所要要用这两个类的话,则首先要通过子类实例化对象。

 1、字节输出流:OutputStream

a)
public void write(int b): 将一个字节数据写入数据流
b)
public void write(byte[] b):将一个byte数组写入数据流
b)
public void write(byte[] b,int off,int len):将一个指定范围的byte数组写入数据流

d) public void flush():刷新该流的缓冲 
c) public  void close():关闭输出流
2、 字节输入流:InputStream

a)
public void available(): 可以取得输入文件的大小
b) public int read():读取内容,以数字的方式读取
c)
public int read(byte b):将内容读到byte数组,同时返回读入的个数
d) public  void close():关闭输出流

下面是复制图片小练习:
import java.io.*;
class CopyPic
{
public static void copyPic()
{
FileInputStream fis=null;
FileOutputStream fos=null;
try
{
fos=new FileOutputStream("d:\\2.jpg");
fis=new FileInputStream("d:\\1.jpg");
int len=0;
byte[] buf= new byte[1024];
while ((len=fis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("copy fail");
}
finally
{
try
{
if (fis!=null)
{
fis.close();
}
}
catch (IOException e)
{
throw new RuntimeException("read fail");
}
try
{
if (fos!=null)
{
fos.close();
}
}
catch (IOException e)
{
throw new RuntimeException("write fail");
}
}
}
public static void main(String[] args)
{
copyPic();
}
}
下面是复制MP3小练习:
import java.io.*;
class CopyMp3
{
public static void copyMp3()throws IOException
{
BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("d:\\2.mp3"));
BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("d:\\1.mp3"));
int len=0;
while ((len=bufis.read())!=-1)
{
bufos.write(len);
}
bufos.close();
bufis.close();
}
public static void main(String[] args) throws IOException
{
long startTime=System.currentTimeMillis();
copyMp3();
long endTime=System.currentTimeMillis();
System.out.println((endTime-startTime)+"毫秒");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息