FileInputStream和FileReader

  这两个类都可以读入数据到缓冲区,FileInputStream在传递到buffer的时候要用byte定义buffer,不然报错。比如:

byte [] buffer = new byte[100];

  而用FileReader传递数据到buffer的时候只能用char定义buffer,不然报错。

char [] buffer = new char[100]

  下面的代码可以把目录下的一个文件里的内容传到另一个文件:

//transfer a huge file using a fileinputstream
import java.io.*;
class Test
{
    public static void main(String args[])
    {
        FileInputStream fis = null ; //要写在try外面,否则finally里无法找到fis和fos;并且一定要赋值null
        FileOutputStream fos = null ; 
        try
        {
            fis = new FileInputStream("C:/from.txt");
            fos = new FileOutputStream("C:/to.txt");
            byte [] buffer = new byte[100];//数组的定义方法                        
            while(true)
            {
                int temp = fis.read(buffer , 0 , buffer.length);//这句话一定要写在while里面,不然会生成一个巨大的txt
                if(temp == -1)//read完了之后返回-1
                    {break ;} 
                fos.write(buffer, 0 , temp);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            try
            {
                fis.close();
                fos.close();
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }
}

  

  用FileReader实现:

//transport a huge file using FileReader
import java.io.*;
class Test2
{
    public static void main(String args[])
    {
        FileReader fr = null ; 
        FileWriter fw = null ;
        try
        {
            fr = new FileReader("C:/from.txt");//此处不要忘了写参数
            fw = new FileWriter("C:/to.txt");
            char [] buffer = new char[100];//数组的定义方法
            // int temp = fr.read(buffer, 0 , buffer.length);
            // for(int i = 0 ; i < temp ; i++)
                // System.out.println(buffer[i]);            
            while(true)
            {
                int temp = fr.read(buffer , 0 , buffer.length);//这句话一定要写在while里面,不然会生成一个巨大的txt
                if(temp == -1)//read完了之后返回-1
                    {break ;} 
                fw.write(buffer, 0 , temp);                
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            try
            {
                fr.close();
                fw.close();
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }
}

 

需要注意的是用文件传递操作的时候一定要用到try..catch..finally,尤其要注意在finally里要再用一个try..catch语句调用close()语句把输入流和输出流关掉,否则FileReader甚至出现不工作的情况。

(END)

----------------Mar.5,2014更新-------------------------------

  今天想要用算法把txt中的文字加密,想要读取一整段的String。于是把while循环改了一下:

while (true) {
				int temp = fr.read(buffer, 0, buffer.length);//temp返回的是长度。 这句话一定要写在while里面,不然会生成一个巨大的txt
				if (temp == -1)// read完了之后返回-1
				{
					break;
				}
				contents = contents + String.valueOf(buffer);//char转成string
				char [] cts = contents.toCharArray();//string转成char
				fw.write(cts, 0, temp);
			}

  注意如果再用FileWriter输出,将会得到ANSI编码的txt,所以如果原来的txt是Unicode编码或者其他编码,就会出现乱码。

posted @ 2013-10-30 09:45  LarryLawrence  阅读(336)  评论(0编辑  收藏  举报