[java]文件操作


/*
 * 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 海兰
 */
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;
	}

	/**
     * 读文件
     *
     * @param path
     *        文件名
     * @return 文件内容
     * @throws IOException
     */
    public static byte[] readFile(String path) throws IOException
    {
        return readFile(path, 0);
    }

    /**
     * 读文件
     *
     * @param path
     *        文件名
     * @param offset
     *        偏移位置
     * @return 从偏移位置开始读取的文件内容
     * @throws IOException
     */
    public static byte[] readFile(String path, long offset)
            throws IOException
    {
        return readFile(path, offset, -1);
    }

	/**
     * 读文件
     *
     * @param path
     *        文件名
     * @param offset
     *        偏移位置
     * @param size
     *        读取大小
     * @return 从偏移位置开始读取size大小的文件内容
     * @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 - baos.size())
                {
                    baos.write(buff, 0, size - baos.size());
                    break;
                } else
                {
                    baos.write(buff, 0, bufLength);
                }
            }
            return baos.toByteArray();
        } finally
        {
            if (is != null)
            {
                is.close();
            }
        }
    }

    /**
     * 写文件
     *
     * @param path
     *        文件名
     * @param content
     *        文件内容
     * @return 服务端保存文件的实际绝对路径
     * @throws IOException
     */
    public static String writeFile(String path, String content)
            throws IOException
    {
        byte[] matter=content.getBytes("GBK");
    	return writeFile(path, matter, false);
    }
    /**
     * 写文件
     *
     * @param path
     *        文件名
     * @param content
     *        文件内容
     * @param append
     *        追加方式
     * @return 服务端保存文件的实际绝对路径
     * @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 "";
    }

    /**
     * 写文件
     *
     * @param path
     *        文件名
     * @param content
     *        文件内容
     * @return 服务端保存文件的实际绝对路径
     * @throws IOException
     */
    public static String writeFile(String path, byte[] content)
            throws IOException
    {
        return writeFile(path, content, false);
    }

    /**
     * 写文件
     *
     * @param path
     *        文件名
     * @param content
     *        文件内容
     * @param append
     *        追加方式
     * @return 服务端保存文件的实际绝对路径
     * @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;
    }

    /**
   * 获得该路径下的所有文件名的列表
   * @param Url
   * @return
   */
public static String[] getFileUrl(String Url)
{
   File path = new File(Url);
   String[] list;
   list=path.list();
   return list;
}

}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值