Java中的IO流(二)
扫描二维码
随时随地手机看文章
1. IO流的异常处理方式
try 外声明,try内new 对象
关闭动作,必须写在finally
防止空指针异常,进行判断
如果开启了多个流对象,分别进行关闭,独立写try catch
eclipse中,不写路径,直接写文件名,文件保存在工程根目录下
追加写入,不会覆盖源文件,但是构造方法中,写true
换行,需要换行的位置rn r回车符 n 换行符
/*
IO流中的异常处理
IO异常处理中。关闭资源close()方法,必须单独写try.. catch
*/
import java.io.*;
class FileWriterDemo2
{
public static void main(String[] args)
{
//try外声明变量,try内建立对象
FileWriter fw = null;
try{
fw = new FileWriter("Exception.txt");//fw = null
fw.write("异常");
fw.flush();
fw.write("没有异常");
fw.flush();
}catch(IOException e){
//将异常信息打印出来
e.printStackTrace();
//如果真的发生IO异常,必须将程序停止
throw new RuntimeException("文件写入失败");
}finally{
//执行关闭流对象
try{
if(fw!=null)//说明对象建立成功,IO流开始使用Windows功能了
fw.close();
}catch(IOException e){
e.printStackTrace();
throw new RuntimeException("关闭资源失败");
}
}
}
} //=
2. 字符流复制文本文件
将C:\r.html复制到D盘去
C盘上的文件,数据源,读取的
D盘上的文件,数据目的,输出的
第一种,读取一个字符,写一个字符 难以忍受
第二种,读取一个数组,写一个数组 最快
第三种,读取一行,写一行,写一个换行 其次
/*
* 字节流复制任意文件,异常处理
* 读写单个字节
*/
import java.io.*;
public class CopyFile {
public static void main(String[] args) {
long s = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("e:\apache.exe");
fos = new FileOutputStream("d:\apache.exe");
int len = 0 ;
while((len = fis.read())!=-1){
fos.write(len);
}
}catch(IOException e){
e.printStackTrace();
throw new RuntimeException("文件复制失败");
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException e){
throw new RuntimeException("文件写入关闭失败");
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException e){
throw new RuntimeException("文件读取关闭失败");
}
}
}
long e = System.currentTimeMillis();
System.out.println(e-s);
}
}
//==============================
/*
* 字节流复制文件,数组缓冲
*/
import java.io.*;
public class CopyFile1 {
public static void main(String[] args) {
long s = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("c:\apache.exe");
fos = new FileOutputStream("d:\apache.exe");
int len = 0 ;
byte[] bytes = new byte[1024];
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
}catch(IOException e){
e.printStackTrace();
throw new RuntimeException("文件复制失败");
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException e){
throw new RuntimeException("文件写入关闭失败");
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException e){
throw new RuntimeException("文件读取关闭失败");
}
}
}
long e = System.currentTimeMillis();
System.out.println(e-s);
}
}
//==============================
package cn.itcast.iostream;
/*
* 利用字节流的缓冲区对象复制
*/
import java.io.*;
public class CopyFile2 {
public static void main(String[] args) {
long s = System.currentTimeMillis();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream("c:\apache.exe"));
bos = new BufferedOutputStream(new FileOutputStream("d:\apache.exe"));
int len = 0 ;
while((len = bis.read())!=-1){
bos.write(len);
}
}catch(IOException e){
e.printStackTrace();
throw new RuntimeException("复制失败");
}finally{
try{
if(bos!=null)
bos.close();
}catch(IOException e){
throw new RuntimeException("文件写入关闭失败");
}finally{
try{
if(bis!=null)
bis.close();
}catch(IOException e){
throw new RuntimeException("文件读取关闭失败");
}
}
}
long e = System.currentTimeMillis();
System.out.println(e-s);
}
}
//
3. 字符流的缓存区对象
字符流的缓存区对象
BufferedWriter
提高字符流对象,输出流的写入效率
构造方法 BufferedWriter(Writer out) 传递的是Writer子类
这个构造方法中,可以传递一切字符输出流
传递的字符输出流,这个缓冲区就会提高我们传递的流对象的效率
FileWriter
new BufferedWriter(FileWriter)
特殊的方法
void newLine() 写入一个新行
我写的时候,rn同样换行
为什么在缓冲区对象中,提供一个换行的方法呢
原因在于Windows Linux换行符号不一样
Windows rn Linux n
newLine()方法,具有跨平台性
安装的JDK是Windows版本的,newLine方法中,写的就是rn
安装的JDK是Linux版本的, newLine方法中,写的就是n
记事本rn
editplus rn n
BufferedReader
提高字符输入流对象,提高读取效率
构造方法BufferedReader(Reader in) 传递一个字符输入流对象
FileReader
new BufferedReader(FileReader)
提供一个新的方法,读一行
String readLine() 读取一个新行,按行读取
每次读取一行,不管一行有多长
返回一行的内容
读取到文件末尾,返回null;
readLine()方法,读取完以后之后,返回的有没有换行符号,没有换行符号
只有有效字符,并没有rn
//==