Collus

          惜秦懷古

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            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);
          ????}
          ????
          /**
          ?????*?從輸入流讀取數據為二進制字節數組.
          ?????*?
          @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 良帥 閱讀(275) 評論(0)  編輯  收藏 所屬分類: JAVA Code Share

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


          網站導航:
          相關文章:
           
          主站蜘蛛池模板: 合阳县| 台东市| 永吉县| 开封县| 靖安县| 西和县| 蒙城县| 鹤山市| 丹凤县| 本溪| 嵩明县| 东丰县| 社会| 台东县| 凉山| 石家庄市| 濉溪县| 包头市| 时尚| 江川县| 三亚市| 天全县| 永嘉县| 图木舒克市| 黑山县| 玉屏| 松阳县| 商南县| 穆棱市| 马尔康县| 浑源县| 蒙阴县| 柯坪县| 桐城市| 确山县| 息烽县| 皋兰县| 鹤庆县| 石阡县| 麻江县| 桂阳县|