- 浏览: 166710 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
tangyuanjian:
这个request怎么赋值啊!
rails技巧之得到web应用中的web服务器地址和端口号 -
wangbaocai:
asp.net导出邮箱联系人列表,支持QQ邮箱、163邮箱、G ...
rails 导出邮箱联系人 -
andrew.yulong:
他默认有个guest角色啊
rails 权限控制 -
liucuan:
如果想过滤这个插件怎么过滤,比如我的index页面我想登录不登 ...
rails 权限控制 -
ayaga:
pt.afterTextEl.el.innerHTML = S ...
extJS控件之每页显示N条记录
和大家见面了,本人言语突然很少,开门见山吧
package
cn.vetech.framework.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.apache.struts.upload.FormFile;
import cn.vetech.framework.source.Constants;
/**
* 用户文件的 读 写 目录的创建等
*
* @author zl
*
*/
public class FileOperate {
private String message;
private String FILESPARA = " / " ;
private String spath = Constants.APPPRTH;
public FileOperate() {
}
/**
* 强制用UTF-8 编码读 整个系统编码采用utf-8
*
* @param filePathAndName
* @return
*/
public String readTxt(String filePathAndName) {
return readTxt(filePathAndName, " UTF-8 " );
}
/**
* 读取文本文件内容
*
*/
public String readTxt(String filePathAndName, String encoding) {
encoding = encoding.trim();
StringBuffer str = new StringBuffer( "" );
String st = "" ;
try {
FileInputStream fs = new FileInputStream(filePathAndName);
InputStreamReader isr;
if (encoding.equals( "" )) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding);
}
BufferedReader br = new BufferedReader(isr);
try {
String data = "" ;
while ((data = br.readLine()) != null ) {
str.append(data + " " );
}
} catch (Exception e) {
str.append(e.toString());
}
st = str.toString();
} catch (IOException es) {
st = "" ;
}
return st;
}
/**
* 强制用UTF-8 编码保存 避免因为操作系统不同而编码不同。
*
* @param path
* @param filename
* @param fileContent
*/
public void writeTxt(String path, String filename, String fileContent) {
writeTxt(path, filename, fileContent, " UTF-8 " );
}
/**
* 有编码方式的文件创建
*/
public void writeTxt(String path, String filename, String fileContent, String encoding) {
try {
File file = new File(path);
if ( ! file.exists()) {
file.mkdirs();
}
file = new File(path + FILESPARA + filename);
PrintWriter pwrite = null ;
if (encoding != null && ! "" .equals(encoding)) {
pwrite = new PrintWriter(file, encoding);
} else {
pwrite = new PrintWriter(file);
}
pwrite.println(fileContent);
pwrite.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 拷贝目录树 把一个目录下的所有东西包括子目录 同时拷贝到 另外一个目录
*
* @param sourceDir
* @param targetDir
* @throws Exception
*/
public void copyDir(String sourceDir, String targetDir) throws Exception {
String url1 = sourceDir;
String url2 = targetDir;
if ( ! ( new File(url2)).exists()) {
( new File(url2)).mkdirs();
}
File[] file = ( new File(url1)).listFiles();
for ( int i = 0 ; i < file.length; i ++ ) {
if (file[i].isFile()) {
file[i].toString();
FileInputStream input = new FileInputStream(file[i]);
FileOutputStream output = new FileOutputStream(url2 + System.getProperty( " file.separator " )
+ (file[i].getName()).toString());
byte [] b = new byte [ 1024 * 5 ];
int len;
while ((len = input.read(b)) != - 1 ) {
output.write(b, 0 , len);
}
output.flush();
output.close();
input.close();
} else if (file[i].isDirectory()) {
String url_2_dir = url2 + System.getProperty( " file.separator " ) + file[i].getName();
copyDir(file[i].getPath(), url_2_dir);
}
}
}
/**
* 新建目录
*
* @param folderPath
* 目录
* @return 返回目录创建后的路径
*/
public String createFolder(String folderPath) {
String txt = folderPath;
try {
java.io.File myFilePath = new java.io.File(txt);
txt = folderPath;
if ( ! myFilePath.exists()) {
myFilePath.mkdirs();
}
} catch (Exception e) {
message = " 创建目录操作出错 " ;
}
return txt;
}
/**
* 删除文件
*
* @param filePathAndName
* 文本文件完整绝对路径及文件名
* @return Boolean 成功删除返回true遭遇异常返回false
*/
public boolean delFile(String filePathAndName) {
boolean bea = false ;
try {
String filePath = filePathAndName;
File myDelFile = new File(filePath);
if (myDelFile.exists()) {
myDelFile.delete();
bea = true ;
} else {
bea = false ;
message = (filePathAndName + " <br>删除文件操作出错 " );
}
} catch (Exception e) {
message = e.toString();
}
return bea;
}
/**
* 删除文件夹
*
* @param folderPath
* 文件夹完整绝对路径
* @return
*/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 删除空文件夹
} catch (Exception e) {
message = ( " 删除文件夹操作出错 " );
}
}
/**
* 删除指定文件夹下所有文件
*
* @param path
* 文件夹完整绝对路径
* @return
* @return
*/
public boolean delAllFile(String path) {
boolean bea = false ;
File file = new File(path);
if ( ! file.exists()) {
return bea;
}
if ( ! file.isDirectory()) {
return bea;
}
String[] tempList = file.list();
File temp = null ;
for ( int i = 0 ; i < tempList.length; i ++ ) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + " / " + tempList[i]); // 先删除文件夹里面的文件
delFolder(path + " / " + tempList[i]); // 再删除空文件夹
bea = true ;
}
}
return bea;
}
/**
* 复制单个文件
*
* @param oldPathFile
* 准备复制的文件源
* @param newPathFile
* 拷贝到新绝对路径带文件名
* @return
*/
public void copyFile(String oldPathFile, String newPathFile) {
try {
int bytesum = 0 ;
int byteread = 0 ;
File oldfile = new File(oldPathFile);
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPathFile); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPathFile);
byte [] buffer = new byte [ 1444 ];
while ((byteread = inStream.read(buffer)) != - 1 ) {
bytesum += byteread; // 字节数 文件大小
// // System.out.println(bytesum);
fs.write(buffer, 0 , byteread);
}
inStream.close();
}
} catch (Exception e) {
message = ( " 复制单个文件操作出错 " );
}
}
/**
* 移动文件
*
* @param oldPath
* @param newPath
* @return
*/
public void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 移动目录
*
* @param oldPath
* @param newPath
* @return
* @throws Exception
*/
public void moveFolder(String sourceDir, String targetDir) throws Exception {
copyDir(sourceDir, targetDir);
delFolder(sourceDir);
}
public String getMessage() {
return this .message;
}
/**
* ybfjm@163.com 扩充
*/
/** 以下是保存文件,读取文件需要用到的方法* */
public String getStringFilePath(String id) {
String strPath = "" ;
// 生成一个目录,生成年度、月份目录
String sfolder_year = id.substring( 0 , 4 );
String sfolder_month = id.substring( 4 , 6 );
strPath = spath + " /updownFiles/ " + sfolder_year + FILESPARA + sfolder_month + FILESPARA;
return strPath;
}
public String getFileByPathId(String pathFile) {
File myFile = new File(pathFile);
if (myFile.exists())
return new FileOperate().readTxt(pathFile);
else
return "" ;
}
public void saveFileByPathId(String path, String id, String nr) {
new FileOperate().writeTxt(path, id + " .txt " , nr);
}
public void delFileByPathId(String pathFile) {
File myFile = new File(pathFile);
if (myFile.exists())
new FileOperate().delFile(pathFile);
}
/*
* 用于上传文件保存 如果saveFileName 为空那么将以FormFile 文件名保存 不包括扩展名,扩展名自动引用 返回保存的文件名
* 如果返回为空表示有错误
*/
public String uploadFileSave(FormFile file, String savePath, String saveFileName) {
String slowdamc = "" ;
if (file == null ) {
return "" ;
}
try {
InputStream stream = file.getInputStream(); // 把文件读入
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
String fileName = file.getFileName().toLowerCase();
if (fileName == null || "" .equals(fileName)) {
return "" ;
}
if (saveFileName == null || "" .equals(saveFileName)) {
slowdamc = fileName;
} else {
if (fileName.indexOf( " . " ) != - 1 ) {
slowdamc = saveFileName + " . " + getFileExt(fileName);
} else {
slowdamc = saveFileName;
}
}
// long isize = file.getFileSize();
// 如果目录不存在就创建目录
createFo
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.apache.struts.upload.FormFile;
import cn.vetech.framework.source.Constants;
/**
* 用户文件的 读 写 目录的创建等
*
* @author zl
*
*/
public class FileOperate {
private String message;
private String FILESPARA = " / " ;
private String spath = Constants.APPPRTH;
public FileOperate() {
}
/**
* 强制用UTF-8 编码读 整个系统编码采用utf-8
*
* @param filePathAndName
* @return
*/
public String readTxt(String filePathAndName) {
return readTxt(filePathAndName, " UTF-8 " );
}
/**
* 读取文本文件内容
*
*/
public String readTxt(String filePathAndName, String encoding) {
encoding = encoding.trim();
StringBuffer str = new StringBuffer( "" );
String st = "" ;
try {
FileInputStream fs = new FileInputStream(filePathAndName);
InputStreamReader isr;
if (encoding.equals( "" )) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding);
}
BufferedReader br = new BufferedReader(isr);
try {
String data = "" ;
while ((data = br.readLine()) != null ) {
str.append(data + " " );
}
} catch (Exception e) {
str.append(e.toString());
}
st = str.toString();
} catch (IOException es) {
st = "" ;
}
return st;
}
/**
* 强制用UTF-8 编码保存 避免因为操作系统不同而编码不同。
*
* @param path
* @param filename
* @param fileContent
*/
public void writeTxt(String path, String filename, String fileContent) {
writeTxt(path, filename, fileContent, " UTF-8 " );
}
/**
* 有编码方式的文件创建
*/
public void writeTxt(String path, String filename, String fileContent, String encoding) {
try {
File file = new File(path);
if ( ! file.exists()) {
file.mkdirs();
}
file = new File(path + FILESPARA + filename);
PrintWriter pwrite = null ;
if (encoding != null && ! "" .equals(encoding)) {
pwrite = new PrintWriter(file, encoding);
} else {
pwrite = new PrintWriter(file);
}
pwrite.println(fileContent);
pwrite.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 拷贝目录树 把一个目录下的所有东西包括子目录 同时拷贝到 另外一个目录
*
* @param sourceDir
* @param targetDir
* @throws Exception
*/
public void copyDir(String sourceDir, String targetDir) throws Exception {
String url1 = sourceDir;
String url2 = targetDir;
if ( ! ( new File(url2)).exists()) {
( new File(url2)).mkdirs();
}
File[] file = ( new File(url1)).listFiles();
for ( int i = 0 ; i < file.length; i ++ ) {
if (file[i].isFile()) {
file[i].toString();
FileInputStream input = new FileInputStream(file[i]);
FileOutputStream output = new FileOutputStream(url2 + System.getProperty( " file.separator " )
+ (file[i].getName()).toString());
byte [] b = new byte [ 1024 * 5 ];
int len;
while ((len = input.read(b)) != - 1 ) {
output.write(b, 0 , len);
}
output.flush();
output.close();
input.close();
} else if (file[i].isDirectory()) {
String url_2_dir = url2 + System.getProperty( " file.separator " ) + file[i].getName();
copyDir(file[i].getPath(), url_2_dir);
}
}
}
/**
* 新建目录
*
* @param folderPath
* 目录
* @return 返回目录创建后的路径
*/
public String createFolder(String folderPath) {
String txt = folderPath;
try {
java.io.File myFilePath = new java.io.File(txt);
txt = folderPath;
if ( ! myFilePath.exists()) {
myFilePath.mkdirs();
}
} catch (Exception e) {
message = " 创建目录操作出错 " ;
}
return txt;
}
/**
* 删除文件
*
* @param filePathAndName
* 文本文件完整绝对路径及文件名
* @return Boolean 成功删除返回true遭遇异常返回false
*/
public boolean delFile(String filePathAndName) {
boolean bea = false ;
try {
String filePath = filePathAndName;
File myDelFile = new File(filePath);
if (myDelFile.exists()) {
myDelFile.delete();
bea = true ;
} else {
bea = false ;
message = (filePathAndName + " <br>删除文件操作出错 " );
}
} catch (Exception e) {
message = e.toString();
}
return bea;
}
/**
* 删除文件夹
*
* @param folderPath
* 文件夹完整绝对路径
* @return
*/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 删除空文件夹
} catch (Exception e) {
message = ( " 删除文件夹操作出错 " );
}
}
/**
* 删除指定文件夹下所有文件
*
* @param path
* 文件夹完整绝对路径
* @return
* @return
*/
public boolean delAllFile(String path) {
boolean bea = false ;
File file = new File(path);
if ( ! file.exists()) {
return bea;
}
if ( ! file.isDirectory()) {
return bea;
}
String[] tempList = file.list();
File temp = null ;
for ( int i = 0 ; i < tempList.length; i ++ ) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + " / " + tempList[i]); // 先删除文件夹里面的文件
delFolder(path + " / " + tempList[i]); // 再删除空文件夹
bea = true ;
}
}
return bea;
}
/**
* 复制单个文件
*
* @param oldPathFile
* 准备复制的文件源
* @param newPathFile
* 拷贝到新绝对路径带文件名
* @return
*/
public void copyFile(String oldPathFile, String newPathFile) {
try {
int bytesum = 0 ;
int byteread = 0 ;
File oldfile = new File(oldPathFile);
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPathFile); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPathFile);
byte [] buffer = new byte [ 1444 ];
while ((byteread = inStream.read(buffer)) != - 1 ) {
bytesum += byteread; // 字节数 文件大小
// // System.out.println(bytesum);
fs.write(buffer, 0 , byteread);
}
inStream.close();
}
} catch (Exception e) {
message = ( " 复制单个文件操作出错 " );
}
}
/**
* 移动文件
*
* @param oldPath
* @param newPath
* @return
*/
public void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 移动目录
*
* @param oldPath
* @param newPath
* @return
* @throws Exception
*/
public void moveFolder(String sourceDir, String targetDir) throws Exception {
copyDir(sourceDir, targetDir);
delFolder(sourceDir);
}
public String getMessage() {
return this .message;
}
/**
* ybfjm@163.com 扩充
*/
/** 以下是保存文件,读取文件需要用到的方法* */
public String getStringFilePath(String id) {
String strPath = "" ;
// 生成一个目录,生成年度、月份目录
String sfolder_year = id.substring( 0 , 4 );
String sfolder_month = id.substring( 4 , 6 );
strPath = spath + " /updownFiles/ " + sfolder_year + FILESPARA + sfolder_month + FILESPARA;
return strPath;
}
public String getFileByPathId(String pathFile) {
File myFile = new File(pathFile);
if (myFile.exists())
return new FileOperate().readTxt(pathFile);
else
return "" ;
}
public void saveFileByPathId(String path, String id, String nr) {
new FileOperate().writeTxt(path, id + " .txt " , nr);
}
public void delFileByPathId(String pathFile) {
File myFile = new File(pathFile);
if (myFile.exists())
new FileOperate().delFile(pathFile);
}
/*
* 用于上传文件保存 如果saveFileName 为空那么将以FormFile 文件名保存 不包括扩展名,扩展名自动引用 返回保存的文件名
* 如果返回为空表示有错误
*/
public String uploadFileSave(FormFile file, String savePath, String saveFileName) {
String slowdamc = "" ;
if (file == null ) {
return "" ;
}
try {
InputStream stream = file.getInputStream(); // 把文件读入
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
String fileName = file.getFileName().toLowerCase();
if (fileName == null || "" .equals(fileName)) {
return "" ;
}
if (saveFileName == null || "" .equals(saveFileName)) {
slowdamc = fileName;
} else {
if (fileName.indexOf( " . " ) != - 1 ) {
slowdamc = saveFileName + " . " + getFileExt(fileName);
} else {
slowdamc = saveFileName;
}
}
// long isize = file.getFileSize();
// 如果目录不存在就创建目录
createFo
发表评论
-
ckeditor使用+上传
2011-10-29 22:59 1860ckeditor是FCK的升级版。 首先下载包就不说了,大家 ... -
java 环境变量
2010-09-17 16:30 941Classpath .;%Java_Home%\l ... -
正则只替换URL字符串里面的汉字部分(转载CSDN)
2008-09-17 10:10 1758URL为 http://192.168.1.1:8080/ ... -
转换字节码
2007-09-25 12:59 681import java.io.UnsupportedEncod ... -
过滤器写法
2007-09-25 12:59 1049package com.accp.util; import ... -
Jamil的一个小列子
2007-09-25 13:06 797package com.zhangyulong.util; i ... -
子页面操作后关父页面刷新
2007-09-29 08:35 2143首先写一个jsp,主要用于跳转!主要有3个参数,url,r ... -
JAVA如何实现HTTP的断点续传
2007-10-06 16:06 994(一)断点续传的原理 其实断点续传的原理很简单,就是在Http ... -
jsMd5加密
2007-12-07 16:58 1235<input id=test value=blueid ... -
抓取网页中指定节点的内容(java版本)
2008-03-27 15:53 3696package atest;import org.htmlp ... -
过滤html标签(java)
2008-03-28 12:45 1711public String html2Text(Strin ... -
java 联动
2008-04-10 17:01 1554郁闷死了,刚才文章快写完了,浏览器死了......在写一遍吧, ... -
java 日期 操作
2008-04-12 09:34 1056这是一个java的日期操作工具类,希望能对大家有帮助 pac ...
相关推荐
Java IO commons-io-2.5.jar。 commons-io-2.5.jar 是Java IO的增强版,功能很强大。里面封装了很多实用方便的函数,文件操作、目录操作的。 Java IO
封装javamail、利用谷歌在线翻译编写的jar包、 封装文件IO操作、封装dom4j,进行xml文件的操作。 压缩包内附doc文档,容易操作。
Java commons-io-2.11.0,第三方封装用于处理文件IO读写。 Java原生的读写处理并没有那么方便,使用上需要开发者自己注意和手写处理许多问题。 commons-io的封装可以屏蔽这些繁杂的处理,让开发者开箱即用。 注意本...
连接池的初始化、创建等仔细看构造函数的doc文档,就知道如何操作。我自己用着很方便。 有两个初始化方法,分别用于初始化多个数据库对象,或者单个数据库对象。 String[] dbPoolName = { "default", "sec" }; ...
封装了JavaIO和NIO的接口,使得变得简单易用,在这基础上封装了一个HTTP客户端,使发送Http报文变得容易,支持http和https,用来做微信的开发最适合不过来
第 1 部分 Java基础程序设计 •Java语言介绍 •简单的 Java 程序 ...--第 10 章 文件(IO)操作 --第 11 章 Java Applet 程序 --第 12 章 Java 常用类库 --第十三章 Java 网络程序设计 附录 JAVA定义格式
封装好的java io实例,简单,可做学习参考,也可用于工具调用!
您可以使用入项目中 repositories { maven { url " https://jitpack.io " }}dependencies { compile " com.github.objectpartners:rancher-java-sdk:master-SNAPSHOT "}例子该库的入口点是io.rancher.Rancher类。...
commons-io-2.5.jar 是ava IO的增强版,功能很强大。里面封装了很多实用方便的函数,文件操作、目录操作的。
dmhub-java-sdkDM Hub OpenAPI 的 Java 封装如何使用Gradlerepositories { jcenter() maven { url 'https://jitpack.io' }}将 加入仓库列表。dependencies { ... implementation '...
JAVA连接FTP服务器,并上传/下载文件的,使用commons-net包实现ftp服务器的访问,commons-net包封装了一些常见的网络包:ftp,smtp,pop3等..相关包:commons-net-1.4.1.jar ; commons-io-1.4.jar;jakarta-oro-2.0.8.jar
项目开发中经常使用的工具包commons-io-2.4.jar,功能很...里面封装了很多实用方便的函数,文件操作、目录操作的。另外Andriod的HttpClient并不直接支持multipart,我们需要额外的jar来支持,这是其中重要的一个jar包。
几个小文件,都是一些代码,很基础的东西,刚学java的可以看看,里面还有个对IO的封装
封装java常用的类,其中有邮件发送实现带jsp,xml,导出excel,加密,时间类,数据库连接池,验证码,gui,io,SQLserver,StringUtil等等等
Java发展史 面向对象基础 面向对象三大特征(封装、继承和多态) static和final 抽象类和接口 异常处理 集合框架 IO 并发编程 Java SE5中其它的重要新特性
NULL 博文链接:https://skyfar666.iteye.com/blog/1922052
Java SE编程入门教程 java IO(共28页).pptx Java SE编程入门教程 java Math(共11页).pptx Java SE编程入门教程 java object(共9页).pptx Java SE编程入门教程 java static final(共24页).pptx Java SE编程...
包封装。能够让JAR包里面的文件依赖于统一版本的类文件。 可移植性,能够在各种平台上直接使用。 把一个JAR文件添加到系统的classpath环境变量之后,java通常会把这个JAR文件当做一个路径来处理。通常使用jar命令...
Java SE编程入门教程 java IO(共28页).pptx Java SE编程入门教程 java Math(共11页).pptx Java SE编程入门教程 java object(共9页).pptx Java SE编程入门教程 java static final(共24页).pptx Java SE编程...
-类方法、封装 -访问修饰符.重载.覆盖 -约瑟夫问题 -多态 -抽象类、接口 -final.作业评讲 -作业、测试题 -数组 -排序、查找 -二进制、位运算、位移运算 -集合 -集合 -集合 -集合补充 -异常 -面试题评讲 -布局管理器 ...