IO流--FileReader&&FileWriter

Posted excellencesy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO流--FileReader&&FileWriter相关的知识,希望对你有一定的参考价值。

(一)FileReader

(1)第一种读取方式

package com.songyan.fileReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * FileReader第一种读取方式
 * @author Administrator
 *
 */
public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileReader fileReader=new FileReader("demo.txt");
        int ch=0;
        while((ch=fileReader.read())!=-1)
        {
            System.out.print((char)ch);
        }
        fileReader.close();
    }

}

(2)第二种读取方式

package com.songyan.fileReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo2 {
public static void main(String[] args) throws IOException {
    FileReader reader=new FileReader("demo.txt");
    char[] arr=new char[8192];
    int len=0;
    while((len=reader.read(arr))!=-1)
    {
        System.out.print(new String(arr));
    }
    reader.close();
}
}

(二)FileWriter

package com.songyan.filewriter;

import java.io.FileWriter;
import java.io.IOException;
/**
 * 第一个demo
 * @author Administrator
 *
 */
public class Demo1 {
    public static void main(String[] args) {
        try {
            FileWriter fileWriter=new FileWriter("/src/com/songyan/fileReader/Demo2.java");
            fileWriter.write("hahha");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

}

异常处理

package com.songyan.filewriter;

import java.io.FileWriter;
import java.io.IOException;
/**
 * 标准IO处理异常的方式
 * @author Administrator
 *
 */
public class Demo2 {
public static void main(String[] args) {
    FileWriter fileWriter=null;
    try {
        fileWriter = new FileWriter("demo1.txt");
        fileWriter.write("hahahh");
        fileWriter.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        try {
            if(fileWriter!=null)
                fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    
}
}

换行符&&续写

package com.songyan.filewriter;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 
 * @author Administrator
 * 回车符:

 * 续写:
 */
public class Demo3 {
public static void main(String[] args) {
    FileWriter writer=null;
    try {
        writer = new FileWriter("demo1.txt");
        writer.write("dddd
");
        writer.write("dddd
");
        writer.write("dddd
");
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        try {
            if(writer!=null)
                writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    
    
}
}

 

(三)代码练习

打印一个java文件控制台

package com.songyan.test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 打印一个java文件到控制台
 * @author Administrator
 *
 */
public class Test1 {
public static void main(String[] args) throws IOException {
    FileReader reader=new FileReader("src/com/songyan/fileReader/Demo2.java");
    char[] arr=new char[8192];
    int len=0;
    while((len=reader.read(arr))!=-1)
    {
        System.out.print(new String(arr));
    }
    reader.close();
    
}
}

将c盘一个文件拷贝到D盘

package com.songyan.test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 将c盘一个文件拷贝到D盘
 * @author Administrator
 *
 */
public class Test2 {
    /**
     * 读一个写一个
     * @throws IOException 
     */
    public static void copy1() throws IOException
    {
        FileReader reader=new FileReader("C:\Users\langchao1\AppData\Local\IconCache.db");
        FileWriter writer=new FileWriter("D:\IconCache1.db");
        int sig=0;
        while((sig=reader.read())!=-1)
        {
            writer.write((char)sig);
        }
        reader.close();
        writer.close();
    }
    
    /**
     * 先读后写
     * @throws IOException 
     */
    public static void copy_my() throws IOException
    {
        //读取C盘文件
        FileReader reader=new FileReader("C:\Users\langchao1\AppData\Local\IconCache.db");
        char[] arr=new char[8192];
        int len=0;
        String str="";
        while((len=reader.read(arr))!=-1)
        {
            str=str+new String(arr);
        }
        //写入D盘
        FileWriter writer=new FileWriter("D:\IconCache.db");
        writer.write(str);
        writer.flush();
        writer.close();
    }
    
    /**
     * 一次读取arr【】
     */
    public static void copy2()
    {
        FileReader reader=null;
        FileWriter writer=null;
        
        try{
             reader=new FileReader("C:\Users\langchao1\AppData\Local\IconCache.db");
             writer=new FileWriter("D:\I.db");
             char[] arr=new char[8192];
             int len=0;
             while((len=reader.read(arr))!=-1)
             {
                 writer.write(arr);
             }
             
        }catch(IOException e)
        {
            e.printStackTrace();
        }finally{
            try {
                if(reader!=null)
                    reader.close();
                if(writer!=null)
                    writer.close();
            } catch (Exception e2) {
            }
        }
    }
    
    /**
     * 最优形式:只读取有效部分
     */
    public static void copy_final()
    {
        FileReader reader=null;
        FileWriter writer=null;
        
        try{
             reader=new FileReader("C:\Users\langchao1\AppData\Local\IconCache.db");
             writer=new FileWriter("D:\I.db");
             char[] arr=new char[8192];
             int len=0;
             while((len=reader.read(arr))!=-1)
             {
                 writer.write(arr,0,len);
             }
             
        }catch(IOException e)
        {
            e.printStackTrace();
        }finally{
            try {
                if(reader!=null)
                    reader.close();
                if(writer!=null)
                    writer.close();
            } catch (Exception e2) {
            }
        }
    }
public static void main(String[] args) throws Exception {
    
    copy2();
    
}
}

 

以上是关于IO流--FileReader&&FileWriter的主要内容,如果未能解决你的问题,请参考以下文章

IO流FileReader和FileWriter

Java连载98-FileReader和FileWriter详解

IO-字符流-FileReader

IO流入门-第四部分-FileReader

Java中的IO操作 字符流:FileReader和 FileWriter

小白学JavaD31》》》IO流 之 缓冲流 & 转换流