IO:
IO流用来处理设备之间的数据传输,java对数据的操作是通过流的方式,java用于操作流的对象都在IO包中
通用的是字节流,字符流也是字节流
字节流的基类:
InputStreamOutputStream
字符流的基类:
ReaderWriter
缓冲区:BufferedReader,BufferedWriter,BufferedInputStream,BufferedOutputStream提高效率
字符流:
操作文件的子类:FileReader FileWriter
int read():读取单个字符,返回作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1;
int read(char[] ch):将字符读入数组,返回读取的字符数,如果已到达流的末尾,则返回 -1
write():写入单个字符。
write(char[] ch):写入字符数组。
write(char[] ch,int off,int len):写入字符数组的某一部分。
将F盘的aaa.txt拷贝到D盘的www.txt
publicstaticvoid copy()
{
FileWriter fw =null;
FileReader fr =null;
try
{
fw =new FileWriter("D:\\www.txt");
fr =new FileReader("F:\\aaa.txt");
char [] arr =newchar [1024];
int len = 0;
while((len = fr.read(arr))!=-1)
{
fw.write(arr, 0, len);
}
}
catch(IOException e)
{
}
finally
{
try
{
if(fw!=null)
{
fw.close();
}
}
catch(IOException e)
{
}
try
{
if(fr!=null)
{
fr.close();
}
}
catch(IOException e)
{
}
}
}
运用缓冲区拷贝:
BufferedReader的readLine()方法:读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。返回包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
BufferedWriter的newLine()方法:写入一个行分隔符。
BufferedWriter的flush()方法:刷新该流的缓冲.通过刷新操作把缓冲区的数据存入到目标文件
publicstaticvoid copy()
{
FileWriter fw =null;
BufferedWriter bw =null;
FileReader fr =null;
BufferedReader br =null;
try
{
fw =new FileWriter("D:\\www.txt");
fr =new FileReader("F:\\javase.java");
bw =new BufferedWriter(fw);
br =new BufferedReader(fr);
String line =null;
while((line = br.readLine())!=null)
{
bw.write(line);
bw.newLine();
bw.flush();
}
}
catch(IOException e)
{
thrownew RuntimeException("读写失败");
}
finally
{
try
{
if(bw!=null)
{
bw.close();
}
}
catch(IOException e)
{
thrownew RuntimeException("读写失败");
}
try
{
if(br!=null)
{
br.close();
}
}
catch(IOException e)
{
thrownew RuntimeException("读写失败");
}
}
}
字节流:
对文件操作的子类:
FileInputStream,FileOutputStream
FileInputStream中的方法:
read():从此输入流中读取一个数据字节,返回下一个数据字节;如果已到达文件末尾,则返回-1
read(byte[] b)
从此输入流中将最多b.length
个字节的数据读入一个 byte 数组中。在某些输入可用之前,此方法将阻塞
available():返回文件中的字节数;
publicstaticvoid in()
{
FileInputStream fis =null;
try
{
fis =new FileInputStream("D:\\we.txt");
byte[] brr =newbyte[fis.available()];//在这里定义一个刚好的长度,不用循环
fis.read(brr);
System.out.println(new String(brr));
}
catch(IOException e)
{
thrownew RuntimeException("asfsasdfdsadfs");
}
finally
{
try
{
if(fis!=null)
{
fis.close();
}
}
catch(IOException e)
{
thrownew RuntimeException("asfsasdfdsadfs");
}
}
}
FileOutputStream中的方法:
wirite():将指定字节写入此文件输出流。实现OutputStream
的write
方法。
write(byte[]b):将b.length
个字节从指定 byte 数组写入此文件输出流中
write(byte[]b,int off,int len):将指定 byte 数组中从偏移量off
开始的len
个字节写入此文件输出流
缓冲区的方法
BufferedInputStream
read()
BufferedOutputStream
write(intb):将指定的字节写入此缓冲的输出流
write(byte[]b,int off,int len):将指定 byte 数组中从偏移量off
开始的len
个字节写入此缓冲的输出流
flush():刷新此缓冲的输出流
通过缓冲区拷贝一个MP3
publicstaticvoid demo()
{
FileInputStream fis =null;
FileOutputStream fos =null;
BufferedInputStream bis =null;
BufferedOutputStream bos =null;
try
{
fis =new FileInputStream("D:\\a.mp3");
fos =new FileOutputStream("D:\\b.mp3");
bis =new BufferedInputStream(fis);
bos =new BufferedOutputStream(fos);
int line = 0;
while((line = bis.read())!=-1)
{
bos.write(line);
}
}
catch(IOException e)
{
thrownew RuntimeException("异常");
}
finally
{
try
{
if(bis!=null)
{
bis.close();
}
}
catch(IOException e)
{
thrownew RuntimeException("异常");
}
try
{
if(bos!=null)
{
bos.close();
}
}
catch(IOException e)
{
thrownew RuntimeException("异常");
}
}
}
转换流:
InputStreamReader(InputStreamis),OutputStreamWriter(OutputStream os) 通过构造函数转换流
publicstaticvoid demo()throws IOException
{
/*
//获取键盘录入对象
InputStream in = System.in;
//将字节流对象转成字符流对象,使用转换流
InputStreamReader isr = new InputStream(in);
//为了提高效率,将字符串进行缓冲区技术高效操作
BufferedReaderbufr = new BufferedReader(isr);
*/
/*
//System.setIn(InputStream is)
System.setIn(newFileInputStream(“a.txt”));//改变输入设备
//System.setOut(PrintStream out)
System.setOut(new PrintStream(“a.txt”));//改变输入设备
*/
BufferedReader bis =new BufferedReader(new InputStreamReader(System.in));//读取键盘录入
BufferedWriter bos =new BufferedWriter(new OutputStreamWriter(System.out));
String line =null;
while((line = bis.readLine())!=null)
{
if("over".equals(line))
break;
bos.write(line.toUpperCase());
bos.newLine();
bos.flush();
}
bis.close();
}
流操作的基本规律:
1. 明确源和目的
源:输入流.InputStream Reader
目的:输出流.OutputStream Writer
2. 操作的数据是否是纯文本
如果是,用字符流
不是,用字节流
3. 当体系明确后,在明确要使用哪个具体对象
通过设备进行区分:
源设备:内存,硬盘,键盘
目的设备:内存,硬盘,控制台
指定编码表:
OutputStreamWriter(OutputStream out,String charseName)
charseName:编码表
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(newFileOutputStream(“a.txt”),”utf-8”));