首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请教读入文件很简单的有关问题

2011-12-03 
请教读入文件很简单的问题。staticpublicStringgetContents(FileaFile){//...checksonaFileareelidedString

请教读入文件很简单的问题。
static   public   String   getContents(File   aFile)   {
        //...checks   on   aFile   are   elided
        StringBuffer   contents   =   new   StringBuffer();

        //declared   here   only   to   make   visible   to   finally   clause
        BufferedReader   input   =   null;
        try   {
            //use   buffering,   reading   one   line   at   a   time
            //FileReader   always   assumes   default   encoding   is   OK!
            input   =   new   BufferedReader(   new   FileReader(aFile)   );
            String   line   =   null;   //not   declared   within   while   loop
            /*
            *   readLine   is   a   bit   quirky   :
            *   it   returns   the   content   of   a   line   MINUS   the   newline.
            *   it   returns   null   only   for   the   END   of   the   stream.
            *   it   returns   an   empty   String   if   two   newlines   appear   in   a   row.
            */
            while   ((   line   =   input.readLine())   !=   null){
                String   []   temp   =   null;
            temp   =   line.split( "| ");
contents.append(line);
                contents.append(System.getProperty( "line.separator "));
            }
        }
        catch   (FileNotFoundException   ex)   {
            ex.printStackTrace();
        }
        catch   (IOException   ex){
            ex.printStackTrace();
        }
        finally   {
            try   {
                if   (input!=   null)   {
                    //flush   and   close   both   "input "   and   its   underlying   FileReader
                    input.close();
                }
            }
            catch   (IOException   ex)   {
                ex.printStackTrace();
            }
        }
        return   contents.toString();


    }

上面这段是读取一个文件的内容,我不太明白其中的两句话。
1、temp   =   line.split( "| ");
      contents.append(line);
2、contents.append(System.getProperty( "line.separator "));
能否给解释一下?这三句什么意思?
谢谢

[解决办法]
1 将字符串line以 | 为分割符,分成temp数组。
2 将line内容追加到contents中。
3 System.getProperty( "line.separator "));得到系统的分隔符,追加到contents末尾。

热点排行