文件的操作java_文件相关操作——FileUtil

[java]代码库import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

/**

* 文件相关操作

*

*/

public class FileUtil {

/**

* 文件名,不带扩展名

*

* @param fileName

* @return

*/

public static String getFilePrefix(String fileName) {

int splitIndex = fileName.lastIndexOf(".");

return fileName.substring(0, splitIndex);

}

/**

* 文件扩展名

*

* @param fileName

* @return

*/

public static String getFileSuffix(String fileName) {

int splitIndex = fileName.lastIndexOf(".");

return fileName.substring(splitIndex + 1);

}

/**

* 复制文件

*

* @param inputFile

* @param outputFile

* @throws FileNotFoundException

*/

public static void copyFile(String inputFile, String outputFile) throws FileNotFoundException {

File sFile = new File(inputFile);

File tFile = new File(outputFile);

FileInputStream fis = new FileInputStream(sFile);

FileOutputStream fos = new FileOutputStream(tFile);

int temp = 0;

try {

while ((temp = fis.read()) != -1) {

fos.write(temp);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

fis.close();

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 创建目录

*

* @param dir

* @return

*/

public static boolean createDir(String dir) {

try {

boolean flag = true;

File file = new File(dir.toString());

if (!file.exists()) {

flag = file.mkdir();

}

return flag;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

/**

* 删除文件

*

* @param strFilePath

* @return

*/

public static boolean removeFile(String strFilePath) {

boolean result = true;

if (strFilePath == null || "".equals(strFilePath)) {

return result;

}

File file = new File(strFilePath);

if (file.isFile() && file.exists()) {

result = file.delete();

}

return result;

}

/**

* 从网络Url中下载文件

* @param urlStr

* @param fileName

* @param savePath

* @throws IOException

*/

public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{

URL url = new URL(urlStr);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

//设置超时间为3秒

conn.setConnectTimeout(3*1000);

//防止屏蔽程序抓取而返回403错误

conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

//得到输入流

InputStream inputStream = conn.getInputStream();

//获取自己数组

byte[] getData = readInputStream(inputStream);

//文件保存位置

File saveDir = new File(savePath);

if(!saveDir.exists()){

saveDir.mkdir();

}

File file = new File(saveDir+File.separator+fileName);

FileOutputStream fos = new FileOutputStream(file);

fos.write(getData);

if(fos!=null){

fos.close();

}

if(inputStream!=null){

inputStream.close();

}

}

/**

* 从输入流中获取字节数组

* @param inputStream

* @return

* @throws IOException

*/

public static byte[] readInputStream(InputStream inputStream) throws IOException {

byte[] buffer = new byte[1024];

int len = 0;

ByteArrayOutputStream bos = new ByteArrayOutputStream();

while((len = inputStream.read(buffer)) != -1) {

bos.write(buffer, 0, len);

}

bos.close();

return bos.toByteArray();

}

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值