读取文件:<%@ page contentType="text/html;charset=gb2312"%><%@ page import ="java.util.*,java.io.*"%>
<%
String fSrcName="c:\\test.txt";
String sTemp = ""; 
try
{
FileReader fr = new FileReader(fSrcName) ;
        BufferedReader br = new BufferedReader(fr) ;
        String sLine = br.readLine() ;
        while (sLine!=null){
                sTemp = sLine.toString() ;
out.print("<br>"+sTemp) ;
                sLine = br.readLine() ;
        }
            fr.close() ;
}catch (Exception e){
e.printStackTrace(System.err) ;
}
%>写入数据自己搞定。

解决方案 »

  1.   

    一,bean:package beansfile;import java.util.Vector;
    import java.io.*;
    public class myRead
    {
    String path="";
    String path1="";
    String path2="";
    String filename="";
    FileReader fr=null;
    BufferedReader br=null;
    String Line="";
    int i=0; public myRead(String myFile){
    try{
    fr=new FileReader(myFile);
    br=new BufferedReader(fr);
    }catch(Exception e){
    System.err.println("file: " + e.getMessage());
    }
    } public int getCount(){
    i=1;
    try{
    Line=br.readLine();
    while(Line!=null){
    i++;
    }
    }catch(java.io.IOException ae){
    System.err.println("readFile: " + ae.getMessage());
    }
    return i;
    } public String[] getRow(){
    try{
    }catch(java.io.IOException ce){
    System.err.println("getRow: " + ce.getMessage());
    }
    } public void closeFile(){
    try{
    br.close();
    fr.close();
    }catch(java.io.IOException ae){
    System.err.println("readFile: " + ae.getMessage());
    }
    }}________________________________________________二,jsp文件:......
    ........
    .........
    ....
    <jsp:useBean id="reader" class="beansfile.DelimitedDataFile" scope="request"/>
    <%
    String path=request.getRealPath("");
    int n=0;//第一行
    String No1="";
    String No2="";
    String No3="";
    String No4="";
    for(int i=1;i<=3;i++){
      reader.setPath(path + "/_txt/" + i + ".txt");
      while(reader.nextRecord()!=-1){//如果至少还有一行
        if(n==0) No1=reader.returnRecord();
        if(n==1) No2=reader.returnRecord();
        if(n==2) No3=reader.returnRecord();
        if(n==3) No4=reader.returnRecord();
        n++;
      }
      strSql="insert into 表名 (列1,列2,列3,列4) values ('No1','"No2"','"No3"','+No4+')";
      sqlbean.executeInsert(strSql);//提交sql语句,换成你自己的bean
    }
    %>
      

  2.   

    少写了一行,在jsp文件的
    reader.setPath(path + "/_txt/" + i + ".txt");
    下面加一行:
    n=0;