Collus

          惜秦懷古

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            0 Posts :: 6 Stories :: 0 Comments :: 0 Trackbacks
          JAVA FileUtil

          import ?java.io. * ;

          /**
          *?FileUtil.?Simple?file?operation?class.
          *
          *?
          @author ?BeanSoft
          *
          */
          public ? class ?FileUtil?{
          ????
          /**
          ?????*?The?buffer.
          ?????
          */
          ????
          protected ? static ? byte ?buf[]? = ? new ? byte [ 1024 ];

          ????
          /**
          ?????*?Read?content?from?local?file.?FIXME?How?to?judge?UTF-8?and?GBK,?the
          ?????*?correct?code?should?be:?FileReader?fr?=?new?FileReader(new
          ?????*?InputStreamReader(fileName,?"ENCODING"));?Might?let?the?user?select?the
          ?????*?encoding?would?be?a?better?idea.?While?reading?UTF-8?files,?the?content
          ?????*?is?bad?when?saved?out.
          ?????*
          ?????*?
          @param ?fileName?-
          ?????*????????????local?file?name?to?read
          ?????*?
          @return
          ?????*?
          @throws ?Exception
          ?????
          */
          ????
          public ? static ?String?readFileAsString(String?fileName)? throws ?Exception?{
          ????????String?content?
          = ? new ?String(readFileBinary(fileName));

          ????????
          return ?content;
          ????}
          ????
          /**
          ?????*?讀取文件并返回為給定字符集的字符串.
          ?????*?
          @param ?fileName
          ?????*?
          @param ?encoding
          ?????*?
          @return
          ?????*?
          @throws ?Exception
          ?????
          */
          ????
          public ? static ?String?readFileAsString(String?fileName,?String?encoding)? throws ?Exception?{
          ????????String?content?
          = ? new ?String(readFileBinary(fileName),?encoding);

          ????????
          return ?content;
          ????}
          ????
          /**
          ?????*?讀取文件并返回為給定字符集的字符串.
          ?????*?
          @param ?fileName
          ?????*?
          @param ?encoding
          ?????*?
          @return
          ?????*?
          @throws ?Exception
          ?????
          */
          ????
          public ? static ?String?readFileAsString(InputStream?in)? throws ?Exception?{
          ????????String?content?
          = ? new ?String(readFileBinary(in));

          ????????
          return ?content;
          ????}

          ????
          /**
          ?????*?Read?content?from?local?file?to?binary?byte?array.
          ?????*
          ?????*?
          @param ?fileName?-
          ?????*????????????local?file?name?to?read
          ?????*?
          @return
          ?????*?
          @throws ?Exception
          ?????
          */
          ????
          public ? static ? byte []?readFileBinary(String?fileName)? throws ?Exception?{
          ????????FileInputStream?fin?
          = ? new ?FileInputStream(fileName);
          ????????
          return ?readFileBinary(fin);
          ????}
          ????
          /**
          ?????*?從輸入流讀取數(shù)據(jù)為二進制字節(jié)數(shù)組.
          ?????*?
          @param ?streamIn
          ?????*?
          @return
          ?????*?
          @throws ?IOException
          ?????
          */
          ????
          public ? static ? byte []?readFileBinary(InputStream?streamIn)? throws ?IOException?{
          ????????BufferedInputStream?in?
          = ? new ?BufferedInputStream(streamIn);

          ????????ByteArrayOutputStream?out?
          = ? new ?ByteArrayOutputStream( 10240 );

          ????????
          int ?len;
          ????????
          while ?((len? = ?in.read(buf))? >= ? 0 )
          ????????????out.write(buf,?
          0 ,?len);
          ????????in.close();

          ????????
          return ?out.toByteArray();???????
          ????}

          ????
          /**
          ?????*?Write?string?content?to?local?file.
          ?????*
          ?????*?
          @param ?fileName?-
          ?????*????????????local?file?name?will?write?to
          ?????*?
          @param ?content
          ?????*????????????String?text
          ?????*?
          @return ?true?if?success
          ?????*?
          @throws ?IOException
          ?????
          */
          ????
          public ? static ? boolean ?writeFileString(String?fileName,?String?content)
          ????????????
          throws ?IOException?{
          ????????FileWriter?fout?
          = ? new ?FileWriter(fileName);
          ????????fout.write(content);
          ????????fout.close();
          ????????
          return ? true ;
          ????}

          ????
          /**
          ?????*?Write?string?content?to?local?file?using?given?character?encoding.
          ?????*
          ?????*?
          @param ?fileName?-
          ?????*????????????local?file?name?will?write?to
          ?????*?
          @param ?content
          ?????*????????????String?text
          ?????*?
          @param ?encoding
          ?????*????????????the?encoding
          ?????*?
          @return ?true?if?success
          ?????*?
          @throws ?IOException
          ?????
          */
          ????
          public ? static ? boolean ?writeFileString(String?fileName,?String?content,
          ????????????String?encoding)?
          throws ?IOException?{
          ????????OutputStreamWriter?fout?
          = ? new ?OutputStreamWriter( new ?FileOutputStream(
          ????????????????fileName),?encoding);

          ????????fout.write(content);
          ????????fout.close();
          ????????
          return ? true ;
          ????}

          ????
          /**
          ?????*?Write?binary?byte?array?to?local?file.
          ?????*
          ?????*?
          @param ?fileName?-
          ?????*????????????local?file?name?will?write?to
          ?????*?
          @param ?content
          ?????*????????????binary?byte?array
          ?????*?
          @return ?true?if?success
          ?????*?
          @throws ?IOException
          ?????
          */
          ????
          public ? static ? boolean ?writeFileBinary(String?fileName,? byte []?content)
          ????????????
          throws ?IOException?{
          ????????FileOutputStream?fout?
          = ? new ?FileOutputStream(fileName);
          ????????fout.write(content);
          ????????fout.close();
          ????????
          return ? true ;
          ????}

          ????
          /**
          ?????*?檢查文件名是否合法.文件名字不能包含字符\/:*?"<>|
          ?????*
          ?????*?
          @param ?fileName文件名,不包含路徑
          ?????*?
          @return ?boolean?is?valid?file?name
          ?????
          */
          ????
          public ? static ? boolean ?isValidFileName(String?fileName)?{
          ????????
          boolean ?isValid? = ? true ;
          ????????String?errChar?
          = ? " \\/:*?\ " <>| " ;?//
          ???????? if ?(fileName? == ? null ? || ?fileName.length()? == ? 0 )?{
          ????????????isValid?
          = ? false ;
          ????????}?
          else ?{
          ????????????
          for ?( int ?i? = ? 0 ;?i? < ?errChar.length();?i ++ )?{
          ????????????????
          if ?(fileName.indexOf(errChar.charAt(i))? != ? - 1 )?{
          ????????????????????isValid?
          = ? false ;
          ????????????????????
          break ;
          ????????????????}
          ????????????}
          ????????}
          ????????
          return ?isValid;
          ????}

          ????
          /**
          ?????*?把非法文件名轉換為合法文件名.
          ?????*
          ?????*?
          @param ?fileName
          ?????*?
          @return
          ?????
          */
          ????
          public ? static ?String?replaceInvalidFileChars(String?fileName)?{
          ????????StringBuffer?out?
          = ? new ?StringBuffer();

          ????????
          for ?( int ?i? = ? 0 ;?i? < ?fileName.length();?i ++ )?{
          ????????????
          char ?ch? = ?fileName.charAt(i);
          ????????????
          // ?Replace?invlid?chars:?\\/:*?\"<>|
          ???????????? switch ?(ch)?{
          ????????????
          case ? ' \\ ' :
          ????????????
          case ? ' / ' :
          ????????????
          case ? ' : ' :
          ????????????
          case ? ' * ' :
          ????????????
          case ? ' ? ' :
          ????????????
          case ? ' \" ' :
          ????????????
          case ? ' < ' :
          ????????????
          case ? ' > ' :
          ????????????
          case ? ' | ' :
          ????????????????out.append(
          ' _ ' );
          ????????????????
          break ;
          ????????????
          default :
          ????????????????out.append(ch);
          ????????????}
          ????????}

          ????????
          return ?out.toString();
          ????}

          ????
          /**
          ?????*?Convert?a?given?file?name?to?a?URL(URI)?string.
          ?????*
          ?????*?
          @param ?fileName?-
          ?????*????????????the?file?to?parse
          ?????*?
          @return ?-?URL?string
          ?????
          */
          ????
          public ? static ?String?filePathToURL(String?fileName)?{
          ????????String?fileUrl?
          = ? new ?File(fileName).toURI().toString();
          ????????
          return ?fileUrl;
          ????}

          ????
          /**
          ?????*?Write?string?content?to?local?file.
          ?????*
          ?????*?
          @param ?fileName?-
          ?????*????????????local?file?name?will?write?to
          ?????*?
          @param ?content
          ?????*????????????String?text
          ?????*?
          @return ?true?if?success
          ?????*?
          @throws ?IOException
          ?????
          */
          ????
          public ? static ? boolean ?appendFileString(String?fileName,?String?content)
          ????????????
          throws ?IOException?{
          ????????OutputStreamWriter?fout?
          = ? new ?OutputStreamWriter( new ?FileOutputStream(
          ????????????????fileName,?
          true ),? " GBK " );

          ????????fout.write(content);
          ????????fout.close();
          ????????
          return ? true ;
          ????}
          ????
          public ? static ? void ?main(String[]?args)?{
          ????????System.out.println(replaceInvalidFileChars(
          " http://www.abc.com/ " ));
          ????}
          }



          posted on 2010-05-12 22:03 良帥 閱讀(276) 評論(0)  編輯  收藏 所屬分類: JAVA Code Share

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
          相關文章:
           
          主站蜘蛛池模板: 临颍县| 长岛县| 伽师县| 定州市| 黄陵县| 霍林郭勒市| 龙岩市| 青阳县| 桂平市| 大名县| 行唐县| 沁水县| 文安县| 九龙城区| 朝阳区| 榕江县| 上饶县| 平度市| 溧阳市| 房产| 彭水| 平安县| 阿克苏市| 象州县| 万载县| 纳雍县| 通州市| 南木林县| 广元市| 临洮县| 祁阳县| 宽城| 禹城市| 湘潭市| 巴青县| 上杭县| 文成县| 通城县| 大新县| 广州市| 县级市|