一 、字符流
以文件操作为主来演示
需求:在硬盘上,创建一个文件并写入一些文字数据。
找到一个专门用于操作文件的Writer子类对象。FileWriter。
import java.io.*;
class FileWriterDemo {
public static void main(String[] args) throws IOException{
//创建一个FileWriter对象,该对象一被初始化就必须要明确被操作的文件。
//而且该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。
//该部要明确数据要存放的目的地.
FileWriter fw = new FileWriter("demo.txt");
//调用write方法,将字符串写入流中
fw.write("abcde");
//将数据刷入目的地中
//fw.flush();
//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据。
//将数据刷到目的地中。
//和flush区别:flush刷新后,流可以继续使用,close刷新后,流将会关闭.
fw.close();
}
}
二、IO异常的处理方式
import java.io.*;
class FileWriterDemo2 {
public static void main(String[] args) {
FileWriter fw = null;
try{
fw = new FileWriter("demo.txt");
fw.write("abcdefghijkmle");
}
catch(IOException e) {
System.out.println(e.toString());
} finally{
try{
if(fw != null){
fw.close();
}
}
catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
三、文件的续写
import java.io.*;
class FileWriterDemo3 {
public static void main(String[] args){
//传递一个true参数,代表不覆盖已有的文件,并在已有的文件末尾出进行续写。
FileWriter fw = null;
try{
fw = new FileWriter("demo.txt",true);
fw.write("haha");
}catch(IOException e){
System.out.println(e.toString());
}finally{
try{
if(fw != null){
fw.close();
}
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
四、文本文件的读取方式
import java.io.*;
class FileReaderDemo {
public static void main(String[] args) throws IOException{
//创建一个文件读取对象,和指定名称的文件相关联。
//要保证该文件是已存在的,如果不存在,会发生异常FileNotFoundException
FileReader fr = new FileReader("demo.txt");
//调用读取对象的read方法.
//read():一次读一个字符,而且会自动往下读。
//int ch1 = fr.read();
//System.out.println("ch1="+(char)ch1);
//int ch2 = fr.read();
//System.out.println("ch2="+(char)ch2);
while(true){
int ch = fr.read();
if(ch == -1)
break;
System.out.println(ch = +(char)ch);
}
/* int ch = 0;
while((ch = fr.read())!= -1){
System.out.println((char)ch)
} */
fr.close();
}
}
小练习
思考:
- 读取一个文本文件,并打印在控制台上
代码:
import java.io.*;
class FileReaderTest
{
public static void main(String[] args) throws {
FileReader fr = null;
try
{
fr = new FileReader("demo.txt");
char[] buf = new char[1024];
int num = 0;
while ((num = fr.read(buf)) != -1)
{
System.out.print(new String(buf,0,num));
}
}
catch (IOException e)
{
System.out.print(e.toString());
}finally{
try
{
if(fr != null){
fr.close();
}
}
catch (IOException e)
{
System.out.print(e.toString());
}
}
}
}
- 将某个文件数据复制到另一个文件夹下
代码:
(方法一:读取一个字符就写入一个字符)
import java.io.*;
class CopyTest
{
public static void main(String[] args){
copy_1();
}
//读取一个字符就写入一个字符
public static void copy_1(){
FileWriter fw = null;
FileReader fr = null;
try
{
//创建一个目的地
fw = new FileWriter("FileReaderDemo2_copy.txt");
//与已有文件关联。
fr = new FileReader("FileReaderDemo2.java");
int ch = 0;
while((ch = fr.read()) != -1)
{
fw.write(ch);
}
}
catch (IOException e)
{
System.out.print(e.toString());
}finally{
try
{
if(fw != null){
fw.close();
}
if(fr !=null){
fr.close();
}
}
catch (IOException e)
{
System.out.print(e.toString());
}
}
}
}
(方法二:)
public static void copy_2(){
FileWriter fw = null;
FileReader fr = null;
try
{
fw = new FileWriter("FileWriterDemo2_copy.txt");
fr = new FileReader("FileWriterDemo2.java");
char[] buf = new char[1024];
int len = 0;
while((len =fr.read(buf)) != -1)
{
fw.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
try
{
if(fw != null)
{
fw.close();
}
if(fr != null)
{
fr.close();
}
}
catch (IOException e)
{
System.out.print(e.toString());
}
}
}