/* 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