[File] Io Common tool class Ioutils (Java read file, write file, zip package)

Source: Internet
Author: User

Blog from: http://blog.csdn.net/amosryan/article/details/6568783

Function directory: Convert input stream to byte stream read the file as a string to specify the encoding format to place the input stream into a list<string> in the GBK format to place the input flow into a list<string> in the form of a row to add the specified newline character to each line ( For example, "\ n", "</br>") converts a string out of a specified file to a zip package of multiple files

Source:[Java] View Plain copy print? package amosryan.utility.file;      import java.io.bufferedreader;   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.inputstreamreader;   import java.io.printwriter;   import java.util.arraylist;   import java.util.list;   import  java.util.zip.zipentry;   import java.util.zip.zipoutputstream;     /**   * io Common kits    *  @author  amosryan   *  @since  2010-06-03   */   public class ioutils {          /**   &NBSP;    *  input flow to byte stream        *  @param  input       *  @return        *  @throws  exception        */       public static byte[]  Tobytes (inputstream input)  throws Exception {            byte[] data = null;            try {                Bytearrayoutputstream byteout = new bytearrayoutputstream ();                byte[] b = new byte[1024];                int read = 0;    &NBSP;&NBSP;&NBsp;         while  ((Read = input.read (b))   > 0)  {                    byteout.write (b, 0, read);                }                data = byteout.tobytearray ();           }  catch  (exception e)  {                e.printstacktrace ();           }  finally {               input.close ( );           }            return data;       }          /**       *  read the file as a string        *         *  @param  input       *  @return         *  @throws  Exception       */        public static string tostring (File file)  throws exception {            return tostring (New fileinputstream (file );       }          /**        *  convert input stream to a string        *         *  @param  input       *  @return         *  @throws  Exception       */       public  static string tostring (inputstream input)  throws Exception {           return tostringwithlinebreak (input, null);        }          /**        *  Place the input stream into a list<string>       *     in the specified encoding format.     *  @param  input       *  @return         *  @throws  Exception       */        public static list<string> tolines (inputstream input, string  encoding)                throws  exception {           inputstreamreader insreader =  new inputstreamreader (input, encoding);            bufferedreader bin = new bufferedreader (insreader);            List<String> lines = new ArrayList<String> ();            String line;            while  ((Line = bin.readline ())  != null)  {                lines.add (line);           }            Bin.close ();           insreader.close ();            return lines;       }           /**       *  placing the input stream into a list<string>  in GBK format       *        *  @param  input        *  @return        *  @throws  Exception        */       public static List<String>  Tolines (inputstream input)  throws Exception {            return tolines (input,  "GBK");       }          /**       *  conversion adds specified line breaks for each line (for example: "/n", "</ Br> ")        *        *  @param   input       *  @param  lineBreak       *  @return        *  @throws  Exception       */        public static string tostringwithlinebreak (InputStream input ,               string linebreak)  throws Exception {           list<string > lines = tolines (input);            Stringbuilder sb = new stringbuilder (20480);            for  (string line : lines)  {                sb.append (line);                if  (linebreak != null)  {                    sb.append (linebreak);                }            }           return sb.tostring ();        }          /**        *  turn the string out to the specified file        *  @param  saveFile        *  @param  content       */        public static void tofile (file savefile, string content)   {           File parent =  Savefile.getparentfile ();           if  (!parent.exists ())  {                parent.mkdirs ();            }           printwriter out =  null;           try {                out = new printwriter (new  FileWriter (savefile));                Out.print (content);                Out.flush ();           } catch  (exception e)  {               e.printstacktrace () ;           } finally {                if  (out != null)  {                    out.close ();                }            }       }          /**        *  Zip packages to a group of files        *         *  @param  srcFiles       *  @param  targetfilename        *  @throws  IOException       */        public static void filestozip (list<file>  srcfiles, string&Nbsp;targetfilename)                throws  IOException {           string fileoutname  = targetFileName +  ". zip";            byte[] buf = new byte[1024];            fileinputstream in = null;            fileoutputstream fos = null;            zipoutputstream out = null;           try  {               fos = new  fileoutputstream (fileoutname);                out = new&nbsp Zipoutputstream (FOS);               for   (file file : srcfiles)  {                    in = new fileinputstream (file);                    Out.putnextentry (New zipentry (file.getname));                    int len;                    while  ((Len = in.read (BUF))  != -1)  {                        out.write (Buf, 0, len);                    }                    if  (in != null)  {                         in.close ();                    }               }            } catch  (exception e)  {               e.printstacktrace ();            } finally {                if  (in != null)  {                     in.close ();                }                if  (fos != null)  {                    out.closeentry ();                    out.close ();                    fos.close ();                }            }       }           public static void main (String[] args)  {            try {               file  doc1 = new file (                         "E://workspace//test//doc//1272531757100_1.doc");                ioutils.tostring (new  FileInputStream (Doc1));           } 

Related Article

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.