JAVA Tool class file Manipulation tool class __java

Source: Internet
Author: User
FileUtils
Package com.lh.wx.utils;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.nio.channels.FileChannel;

Import java.util.List;

Import Org.apache.commons.lang3.StringUtils; public class FileUtils {//Data flow public static InputStream getinputstreambyget (String url) {T of Reader response data through GET requests
          ry {HttpURLConnection conn = (httpurlconnection) new URL (URL). OpenConnection ();
          Conn.setreadtimeout (5000);
          Conn.setconnecttimeout (5000);

          Conn.setrequestmethod ("get"); if (conn.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {InputStream InputStream = conn.getInputStream ();
          return inputstream;
      } catch (IOException e) {e.printstacktrace ();
  return null; //inputstream go outputstream public static Bytearrayoutputstream parse (InputStream in) {try {Bytearr
          Ayoutputstream Swapstream = new Bytearrayoutputstream ();
          int ch;   
          while (ch = in.read ())!=-1) {swapstream.write (CH);
    return swapstream;
    catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
  return null; //outputstream go inputstream public static Bytearrayinputstream parse (OutputStream out) {try {Bytearr
      Ayoutputstream baos=new Bytearrayoutputstream ();
      Baos= (Bytearrayoutputstream) out;
      Bytearrayinputstream Swapstream = new Bytearrayinputstream (Baos.tobytearray ());
     return swapstream; catch (Exception e) {//TODO auto-generated catchBlock E.printstacktrace ();
  return null; public static void Main (string[] args) {InputStream is=getinputstreambyget ("Http://mmbiz.qpic.cn/mmbiz_png/KYOtWWib
    Vu2uiafnlhrormd5ofrmxydjmapwwqsr5fze7o99eoppfpxsvdecwkbibicgkkdk7suyt2ewuogsheggrg/0?wx_fmt=png ");
SaveData (is,new File ("d://ceshi.jpg")); ///Save the server response data stream to local file public static void SaveData (InputStream is, file file) {try (Bufferedinputstream bis
              = new Bufferedinputstream (IS); Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (file));)
          {byte[] buffer = new byte[1024];
          int len =-1;
              while (len = bis.read (buffer))!=-1) {bos.write (buffer, 0, Len);
          Bos.flush ();
      } catch (IOException e) {e.printstacktrace (); }/** * <p>Title:copyFileUsingFileChannels</p> * <p>description: File copy </p> * @param SOURCE * @param dest * @throws IOException/@SuppressWarnings ({"Resource"}) public synchronized stat  IC void Copyfileusingfilechannels (file source, file dest) throws IOException {FileChannel Inputchannel    
              = NULL;    
              FileChannel outputchannel = null;
                  try {inputchannel = new FileInputStream (source). Getchannel ();
                  Outputchannel = new FileOutputStream (dest). Getchannel ();
              Outputchannel.transferfrom (inputchannel, 0, Inputchannel.size ());
                  finally {inputchannel.close ();
              Outputchannel.close (); }/** * <p>Title:clearInfoForFile</p> * <p>description: File contents Empty </p> * PARAM fileName */public static void Clearinfoforfile (String fileName) {if Stringutils.isempty (Filena
            Me)) {return; } File File =new FilE (fileName);
              try {if (!file.exists ()) {file.createnewfile ();
              } FileWriter FileWriter =new FileWriter (file);
              Filewriter.write ("");
              Filewriter.flush ();
          Filewriter.close ();
          catch (IOException e) {e.printstacktrace (); }/** * <p>Title:writeFileByLine</p> * <p>description: Writing to file by line </p> * @t Hrows IOException */public static void Writefilebyline (String filename,list<string> parameter) {i
        F (Stringutils.isempty (fileName) | | parameter = = NULL | | parameter.size () = = 0) {return;
        } FileWriter FW =null;
            try {fw = new FileWriter (fileName);
            for (int i = 0; i < parameter.size (); i++) {Fw.write (Parameter.get (i) + "\ n");
    } catch (IOException e) {e.printstacktrace ();    }finally {try {fw.close ();
 The catch (IOException e) {}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.