---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
什么是IO流?
IO流用来处理设备之间的数据传输。Java通过流的方式来操作数据。
流的分类:
按操作数据的类型分为字符流和字节流,字节流的体现形式是字节,即二进制,字符流的体现形式主要是文本,融合了编码表。
按流向分为输入流和输出流,即读和写。
IO流的四个基类
字节流:InputStream OutputStream
字符流:Reader Writer
他们都是抽象类,由着四个类派生出来的子类名称都是以其父类名为后缀的,前缀名一般为其功能。
一、FileWriter对象
从名称可以看出该对象是操作文件的字符写入流。
FileWriterDemo.java演示FileWriter对象基本的写方法write()
import java.io.*;//导包,因为Java所有用于操作IO流的对象都在IO包中
class FileWriterDemo
{
public static void main(String[] args) throws IOException//流对象的方法会抛出异常,所以现在必须在主函数中也抛出异常
{
//创建FileWriter对象实例,必须在初始化时指定被操作的文件,该文件会在指定的目录下被创建,如果有同名文件存在则该同名文件会被覆盖
FileWriter fw = new FileWriter("demo.txt");
//调用write()方法,将字符串写入流中
fw.write("abcde");
//write()方法只是将数据写入到了缓冲区中,通过flush()方法才能将数据写入文件中
//fw.flush();
fw.write("nimei");
//fw.flush();
//IO流操作数据必须调用windows的底层资源,所以使用完毕后必须关闭流,释放资源,同时也会刷新缓冲的数据
//和flush()区别:flush刷新后,流可以继续使用,close刷新后,流将会关闭。
fw.close();
}
}
FileWriterDemo2.java演示IO异常的处理方式,try{}catch{}
import java.io.*;
class FileWriterDemo2
{
public static void main(String[] args)
{
FileWriter fw = null;
try
{
fw = new FileWriter("k://nimei.txt");
fw.write("woca");
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally //finally里的代码无论是否出现异常都会执行,所以将关闭流的代码放在这里
{
/*
在这里需要注意的是,如果有多个流需要关闭,则必须分开写在两个不同的try{}catch{}中。
因为如果写在一起,如果第一个流的close方法出现异常,第二个流的close方法就无法执行,有可能会造成无法释放资源的情况。
*/
if(fw!=null)//有可能fw在初始化时出现异常,所以必须判断
try
{
fw.close();//close()方法也会抛出异常,所以必须处理
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
FileWriterDemo3.java演示如何续写文件
import java.io.*;
class FileWriterDemo3
{
public static void main(String[] args)
{
FileWriter fw = null;
try
{
//重载构造函数FileWriter(String fileName,boolean append),传递一个true参数代表不覆盖已有的文件,并在已有文件的末尾进行数据续写
fw = new FileWriter("demo.txt",true);
fw.write("\r\nhalou\r\nhaojiubujian");
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
if(fw!=null)
try
{
fw.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
二、文本文件读取方式一:每次读取一个字符
import java.io.*;
class FileReaderDemo
{
public static void main(String[] args)
{
FileReader fr = null;
try
{
//创建一个文件读取流,和指定名称的文件相关联,要保证该文件是存在的,如果不存在会发生FileNotFoundException异常
fr = new FileReader("demo.txt");
int ch = 0;
//调用文件读取流对象的read方法,一次读取一个字符,而且会自动往下读,如果到达流的末尾会返回-1(循环条件)
//事实上每个文件末尾都有一个windows定义的结束标记,当流读到这个结束标记的时候就返回-1
while((ch=fr.read())!=-1)
System.out.println("ch="+(char)ch);
/*
while(true)
{
int ch = fr.read();
if(ch==-1)
break;
System.out.println("ch="+(char)ch);
}
*/
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
三、文本文件读取方式二:通过字符数组进行读取
import java.io.*;
class FileReaderDemo2
{
public static void main(String[] args)
{
//定义字符数组缓冲区,用于存储读到的字符,大小一般为1024的整数倍
char[] buf = new char[1024];
//定义返回值变量,获取读到的字符数量,以及判断是否到文件末尾。
int num = 0;
//创建文件读取流对象引用变量
FileReader fr = null;
//异常处理
try
{
fr = new FileReader("demo.txt");
//该read(char[])方法返回的是每次读到的字符的个数,如果到达流末尾则返回-1
while((num=fr.read(buf))!=-1)//判断是否到达流末尾
System.out.println(new String(buf,0,num));//只打印读取数量的字符长度
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
四、拷贝文本文件
/*
将C盘的文本文件复制到D盘
复制的原理:读取C盘下的文件数据写入到D盘的一个文件中。
步骤:
1.在D盘创建一个文件;
2.定义读取流和C盘文件关联;
3.通过不断的读写完成数据存储;
4.关闭资源。
*/
import java.io.*;
class CopyTextFile
{
public static void main(String[] args)
{
//copyMethod1();
copyMethod2();
}
//方法一:读取一个字符,写一个字符
public static void copyMethod1()
{
//创建文件读取流对象引用变量
FileReader fr = null;
//创建读取字符变量
int ch = 0;
//创建文件写入流对象引用变量
FileWriter fw = null;
//异常处理
try
{
fr = new FileReader("c:\\log_followvideo.txt");
fw = new FileWriter("d:\\log_followvideo_copy.txt");
while((ch=fr.read())!=-1)
fw.write(ch);
}
catch (IOException e)
{
//System.out.println(e.toString());
throw new RuntimeException("读写失败!");//新的打印异常情况的语句
}
finally
{
/*
try
{
if(fr!=null)
fr.close();
if(fw!=null)
fw.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
*/
//不能像上面这么写,因为如果第一个流出现异常,则第二个流关闭语句就无法执行了
if(fr!=null)
try
{
fr.close();
}
catch (IOException e)
{
throw new RuntimeException("文件读取流关闭失败!");
}
if(fw!=null)
try
{
fw.close();
}
catch (IOException e)
{
throw new RuntimeException("文件读取流关闭失败!");
}
}
}
//方法二:读入一个字符数组,写一个数组
public static void copyMethod2()
{
//创建文件读取流对象引用变量
FileReader fr = null;
//创建字符数组缓冲区
char[] buf = new char[1024];
int num = 0;
//创建文件写入流对象引用变量
FileWriter fw = null;
//异常处理
try
{
fr = new FileReader("c:\\log_followvideo.txt");
fw = new FileWriter("d:\\log_followvideo_copy.txt");
while((num=fr.read(buf))!=-1)
fw.write(buf,0,num);
}
catch (IOException e)
{
//System.out.println(e.toString());
throw new RuntimeException("读写失败!");
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch (IOException e)
{
throw new RuntimeException("文件读取流关闭失败!");
}
if(fw!=null)
try
{
fw.close();
}
catch (IOException e)
{
throw new RuntimeException("文件写入流关闭失败!");
}
}
}
}
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net