当前位置: 编程技术>移动开发
本页文章导读:
▪资料的导入 文件的导入
jsp: <s:form action="/blog_article/import.action" method="post" enctype="multipart/form-data" theme="simple">
<table border="1">
<tr>
<td>文件上传</td>
</tr>
<tr>
<td>
.........
▪ Java 操作 资料 File Java 操作 文件 File
写文件
public static void stream(String path) throws FileNotFoundException, IOException {
Long startTime = System.currentTimeMillis();
BufferedReader reader = getReader(new File(path));
.........
▪ NIO按行读取资料 NIO按行读取文件
要解析文件入库,通过NIO方式直接读取日志文件一行一行的处理
日志是通过log4j写入的,通过log4j的方法将日志信息写入到文件,每行有一个换行符
结论:
jdk1.6暂时.........
[1]资料的导入
来源: 互联网 发布时间: 2014-02-18
文件的导入
jsp:
action:上传文件的文件如果为uploadFile,那么文件名一定要为uploadFileFileName,也就是在文件后加入FileName,不然不能识别
1:导入导出jar包的区别‘
2: private File uploadFile=null;
private String uploadFileFileName=null;
为什么不加“=null”值就传不进来
3:String directory="/upload/role";
String targetDirectory=
ServletActionContext.getServletContext().getRealPath(directory);
--》targetDirectory = D:\lxq\zhuanyi\jar+js\apache-tomcat-5.5.23\webapps\TinyBee\upload\role
jsp:
<s:form action="/blog_article/import.action" method="post" enctype="multipart/form-data" theme="simple"> <table border="1"> <tr> <td>文件上传</td> </tr> <tr> <td> <s:file name="uploadFile" label="文件位置" size="80"/> </td> </tr> <tr><td> <s:submit value="submit"></s:submit> <s:reset value="reset"></s:reset> </td></tr> </table> </s:form>
action:上传文件的文件如果为uploadFile,那么文件名一定要为uploadFileFileName,也就是在文件后加入FileName,不然不能识别
package excl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import test.BaseAction;
import test.User;
public class Import extends BaseAction{
private static final String SUCCESS = "success";
private File uploadFile=null;
private String uploadFileFileName=null;
public String loadRoleFile() {
String directory="/upload/role";
String targetDirectory=
ServletActionContext.getServletContext().getRealPath(directory);
String file=targetDirectory+"\\"+uploadFileFileName;
//生成上传的文件对象
File target = new File(file);
//如果文件已经存在,则删除源文件
if(target.exists()){
target.delete();
}
System.out.println(uploadFile);
System.out.println(uploadFileFileName);
//复制file对象,实现上传
try {
FileUtils.copyFile(uploadFile,target);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List roleList = new ArrayList();
try {
FileInputStream fi = new FileInputStream(target);
Workbook wb = new HSSFWorkbook(fi);
Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum()+1;
for(int i=1;i<rowNum;i++){
User user = new User();
Row row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for(int j=0;j<cellNum;j++){
Cell cell = row.getCell(j);
String cellValue=null;
switch(cell.getCellType()){
//判断 excel单元格内容的格式,并对其进行转换,以便插入数据库
case 0:cellValue=String .valueOf((int)cell.getNumericCellValue());break;
case 1 : cellValue = cell.getStringCellValue(); break;
case 2 : cellValue = String.valueOf(cell.getDateCellValue()); break;
case 3 : cellValue = ""; break;
case 4 : cellValue = String.valueOf(cell.getBooleanCellValue()); break;
case 5 : cellValue = String.valueOf(cell.getErrorCellValue()); break;
}
switch(j){//通过列数来判断对应插如的字段
case 0 : user.setId(Integer.parseInt(cellValue));break;
case 1 : user.setPassword(cellValue);break;
case 2 : user.setUsername(cellValue);break;
}
}
roleList.add(user);
}
} catch (IOException e) {
e.printStackTrace();
}
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
Transaction tx = null;
try {
tx=null;
tx=session.beginTransaction();
if(roleList.size()>0){
int roleNum=roleList.size();
for(int i=0;i<roleNum;i++){
session.save(roleList.get(i));
}
tx.commit();
}
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
return SUCCESS;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
}
1:导入导出jar包的区别‘
2: private File uploadFile=null;
private String uploadFileFileName=null;
为什么不加“=null”值就传不进来
3:String directory="/upload/role";
String targetDirectory=
ServletActionContext.getServletContext().getRealPath(directory);
--》targetDirectory = D:\lxq\zhuanyi\jar+js\apache-tomcat-5.5.23\webapps\TinyBee\upload\role
[2] Java 操作 资料 File
来源: 互联网 发布时间: 2014-02-18
Java 操作 文件 File
写文件
public static void stream(String path) throws FileNotFoundException, IOException {
Long startTime = System.currentTimeMillis();
BufferedReader reader = getReader(new File(path));
String line;
while ((line = reader.readLine()) != null) {
// 空转
System.out.println(line);
}
Long estimatedTime = System.currentTimeMillis() - startTime;
System.out.printf("stream Diff: %d ms\n", estimatedTime);
}
读文件
public static void stream(String file) throws FileNotFoundException, IOException {
Long startTime = System.currentTimeMillis();
BufferedReader reader = getReader(new File(path));
String line;
while ((line = reader.readLine()) != null) {
// 空转
System.out.println(line);
}
Long estimatedTime = System.currentTimeMillis() - startTime;
System.out.printf("stream Diff: %d ms\n", estimatedTime);
}
public static BufferedReader getReader(File f) throws FileNotFoundException, IOException {
BufferedReader reader = null;
if (f.getName().endsWith(".gz")) {
reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(f))));
} else {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
}
return reader;
}
[3] NIO按行读取资料
来源: 互联网 发布时间: 2014-02-18
NIO按行读取文件
要解析文件入库,通过NIO方式直接读取日志文件一行一行的处理
日志是通过log4j写入的,通过log4j的方法将日志信息写入到文件,每行有一个换行符
结论:
jdk1.6暂时没有找到合适的方法(只能使用传统方法吧)
jdk1.7可以按行读取文件,jdk1.7的api中有一个Files类可以处理 readAllLines()可以处理
参考资料:http://www.kodejava.org/examples/813.html
package org.kodejava.example.nio;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class ReadFileAsListDemo {
public static void main(String[] args) {
String fileName = "core-sample/src/main/resources/data.txt";
try {
List<String> lines = Files.readAllLines(Paths.get(fileName),
Charset.defaultCharset());
for (String line : lines) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
刚才在StackOverFlow中看到一个BT的问题,15G的日志文件。
最新技术文章: