[Java] File operations

Source: Internet
Author: User


/* To change this template, choose Tools | Templates * and open the template in the editor. */import Java.io.bufferedoutputstream;import Java.io.bytearrayoutputstream;import Java.io.File;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.InputStream; Import Java.io.outputstream;import java.io.printstream;/** * * @author Highland */public class Fileutil {public static string[] Getutflines (String path) throws IOException {byte[] filebyte=readfile (path); String Filestr=new string (filebyte, "UTF-8"); String[] Rev=filestr.split ("\ n"); return rev;} public static string[] Getlines (String path) throws IOException {byte[] filebyte=readfile (path); String Filestr=new string (filebyte, "GBK"); String[] Rev=filestr.split ("\ n"); return rev;} /** * Read File * * @param path * file name * @return File contents * @throws IOException */Public Stati    C byte[] ReadFile (String path) throws IOException {return readFile (path, 0);   }    /**  * Read File * * @param path * file name * @param offset * Offset position * @return The contents of the file read from the offset position *        @throws IOException */public static byte[] ReadFile (String path, long offset) throws IOException {    return ReadFile (path, offset,-1);        }/** * Read File * * @param path * file name * @param offset * Offset position * @param size * Read size * @return reads the file contents of size from the offset position * @throws IOException */public static byte[] ReadFile (String path,        Long offset, int size) throws IOException {InputStream is = null;            try {bytearrayoutputstream BAOs = new Bytearrayoutputstream ();            is = new FileInputStream (path);            Is.skip (offset);            byte[] buff = new byte[4096];            int buflength =-1; while ((buflength = Is.read (buff)) >= 0) {if (Size > 0 && buflength > Size-b     Aos.size ())           {baos.write (buff, 0, size-baos.size ());                Break                } else {baos.write (buff, 0, buflength);        }} return Baos.tobytearray ();            } finally {if (is! = null) {is.close (); }}}/** * Write file * * @param path * filename * @param content * File contents * @re            Turn the actual absolute path to the server save file * @throws IOException */public static string WriteFile (string path, string content)    Throws IOException {byte[] matter=content.getbytes ("GBK");    return WriteFile (path, matter, false);     }/** * Write file * * @param path * file name * @param content * File contents * @param append * Append method * @return The actual absolute path of the server save file * @throws IOException */public static string WriteFile (string path , String Content,boolean Append) throws IOException {byte[] matter=content.getbytes ("GBK");    return WriteFile (path, matter, append);    public static string WriteToFile (string Savepath, string content) {file F = new File (Savepath);    if (f.exists ()) F.delete ();    try {printstream ps = new PrintStream (Savepath);    String tmp[] = content.split ("\ n");    for (int i = 0; i < tmp.length; i++) ps.println (Tmp[i]);    } catch (Exception e) {e.printstacktrace ();    } return ""; }/** * Write file * * @param path * file name * @param content * File contents * @return The server to save the file Inter Absolute path * @throws IOException */public static String WriteFile (string path, byte[] content) throws IO    Exception {return WriteFile (path, content, false);     }/** * Write file * * @param path * file name * @param content * File contents * @param append * Append method * @return The actual absolute path of the server save file * @throws IoexceptiOn */public static String WriteFile (String path, byte[] content, Boolean append) throws IOException {if (path = = NULL | | path.length () = = 0) {Path = File.createtempfile ("Writeserverfile", ". File"        ). GetAbsolutePath ();        } else {path = new File (path). GetAbsolutePath ();        } outputstream OS = null;            try {os = new Bufferedoutputstream (new FileOutputStream (path, append));            Os.write (content);        Os.flush ();            } finally {if (OS! = null) {os.close ();    }} return path; /** * Get a list of all filenames under this path * @param Url * @return */public static string[] Getfileurl (String Url) {File path = n   EW File (URL);   String[] list;   List=path.list (); return list;}}



[Java] File operations

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.