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

相關文章
相關標籤/搜索