站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题关于读取文件

关于读取文件

摘要: 关于读取文件


现在需求是这样,用户通过form表单上传一个txt文件,我要把这个文件逐行读取出来,有两个问题,第一:如何确认读取文件的位置?第二:怎么把这个文件逐行读取?


基础啊,推荐看编程思想


能不能告诉一下阿?我基础太差了:(


一时半伙没法讲,文件操作这块是java中比较难的地方,涉及到缓存与流处理,


哦,又没有代码给看看阿,谢谢啦:)


就是bufferreader inputstreanreader 来回套用



文件已经通过表单上传上来了,直接读取就行了,为什么还要确定它的位置?
再说,文件是在远程机器上吧,你想确定它在远程机器上的位置?


这里是struts开源项目中的一个读取上传文件的类,可以参考一下:

package org.apache.struts.webapp.upload;


import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;



/**
* This class takes the UploadForm and retrieves the text value
* and file attributes and puts them in the request for the display.jsp
* page to display them
*
* @author Mike Schachter
* @version $Revision: 1.8 $ $Date: 2003/02/28 02:18:23 $
*/


public class UploadAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

if (form instanceof UploadForm) {

//this line is here for when the input page is upload-utf8.jsp,
//it sets the correct character encoding for the response
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=utf-8");
}

UploadForm theForm = (UploadForm) form;

//retrieve the text data
String text = theForm.getTheText();

//retrieve the query string value
String queryValue = theForm.getQueryParam();

//retrieve the file representation
FormFile file = theForm.getTheFile();

//retrieve the file name
String fileName= file.getFileName();

//retrieve the content type
String contentType = file.getContentType();

boolean writeFile = theForm.getWriteFile();

//retrieve the file size
String size = (file.getFileSize() + " bytes");

String data = null;

try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
if (!writeFile) {
//only write files out that are less than 1MB
if (file.getFileSize() < (4*1024000)) {

byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = new String(baos.toByteArray());
}
else {
data = new String("The file is greater than 4MB, " +
" and has not been written to stream." +
" File Size: " + file.getFileSize() + " bytes. This is a" +
" limitation of this particular web application, hard-coded" +
" in org.apache.struts.webapp.upload.UploadAction");
}
}
else {
//write the file to the file specified
OutputStream bos = new FileOutputStream(theForm.getFilePath());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data = "The file has been written to /"" + theForm.getFilePath() + "/"";
}
//close the stream
stream.close();
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}

//place the data into the request for retrieval from display.jsp
request.setAttribute("text", text);
request.setAttribute("queryValue", queryValue);
request.setAttribute("fileName", fileName);
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("data", data);

//destroy the temporary file created
file.destroy();

//return a forward to display.jsp
return mapping.findForward("display");
}

//this shouldn't happen in this example
return null;
}
}


那代码太复杂了,看不懂啊?


既然提交上来了就不用确定位置了 ,行处理 用BufferedReader


下面这几句就是有关读取上传文件的语句:


//retrieve the file representation
//取得上传文件的句柄
FormFile file = theForm.getTheFile();

//retrieve the file data
//读取文件流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();

while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
{
... ...
}




sorry,下面这句不要:
ByteArrayOutputStream baos = new ByteArrayOutputStream();

这是用来把文件输出到本地的。



↑返回目录
前一篇: jni 在web下是否可以使用
后一篇: ArrayList问题