FileInputStream和FileReader

  这两个类均可以读入数据到缓冲区,FileInputStream在传递到buffer的时候要用byte定义buffer,否则报错。好比:java

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)spa

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

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

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编码或者其余编码,就会出现乱码。input

相关文章
相关标签/搜索