File f=new File("文件所存放目录的绝对路径");
File[] fs=f.listFiles();//这个fs数组就是你要的文件对象组
之后用for就可以实现你的功能

解决方案 »

  1.   

    系统中有选择文件的对话框,加上去,然后就如楼上老兄所说的,取得文件的绝对路径,读取就行了。你可以去api文档中去查java.io包下的输入输出类,根据你的需要选择读去文件的类,调用相应的方法就行了。给你一个我以前写的东西,不过是用来输出的,但原理一样的。
    package net.htjs.util;import java.io.*;
    import java.sql.*;
    import java.util.*;
    import org.apache.log4j.Logger;
    import net.htjs.database.ExecuteSql;
    import net.htjs.util.*;public class MadeXML
    {
    private static Logger _logger=Logger.getLogger("MadeXML.java");
    private ExecuteSql es=new ExecuteSql();
    GetIni gi=new GetIni();//生成读取参数类的实例 private String strSql=null;//查询语句
    private String fileName=null;//要生成的文件名
    private String sTime=null;//查询时期
    private String file=null;//生成的文件名 //取得日期信息
    GregorianCalendar gc=new GregorianCalendar();
    private int year=gc.get(Calendar.YEAR);
    private int month=gc.get(Calendar.MONTH)+1;
    private int day=gc.get(Calendar.DATE);
    private int hour=gc.get(Calendar.HOUR_OF_DAY);
    private int minute=gc.get(Calendar.MINUTE); public MadeXML()
    {
    _logger.debug("准备生成导出数据的XML文件");
    //madeXmlFile();
    }
    public MadeXML(String strSql,String fileName,String sTime,String file)
    {
    _logger.debug("准备生成导出数据的XML文件");
    this.strSql=strSql;
    _logger.debug("查询的sql语句:"+strSql);
    this.fileName=fileName;
    _logger.debug("文件名称:"+fileName);
    this.sTime=sTime;
    _logger.debug("导出时期:"+sTime);
    this.file=file;
    _logger.debug("文件名称:"+file);
    //madeXmlFile();
    } public void setStrSql(String value)
    {
    this.strSql=value;
    }
    public String getStrSql()
    {
    return strSql;
    } public void setFileName(String value)
    {
    this.fileName=value;
    }
    public String getFileName()
    {
    return fileName;
    } public void setSTime(String value)
    {
    this.sTime=value;
    }
    public String getSTime()
    {
    return sTime;
    } public void setFile(String value)
    {
    this.file=value;
    }
    public String getFile()
    {
    return file;
    } public boolean madeXmlFile()
    {
    ResultSet rs1=null;
    ResultSet rs2=null;
    try
    {
    rs1=es.executeQuery(strSql);
    rs2=es.executeQuery(strSql);
    _logger.debug("成功查取数据");
    }
    catch(SQLException se)
    {
    _logger.debug("导出信息失败!");
    }

    //创建文件
    String xmlFile=file+".xml";
    _logger.debug("最后要生成的文件名称:"+xmlFile); try
    {
    //取得表的总列数及每列的名称
    ResultSetMetaData rsmd = null;
    String tableName=fileName;
    //String[] columnName=null;
    int columnCount=0;
    _logger.debug("准备取得表信息");
    while(rs1.next())
    {
    _logger.debug("准备取得明细信息对象");
    rsmd=rs1.getMetaData();
    _logger.debug("获得明细信息对象!"); columnCount=rsmd.getColumnCount();
    _logger.debug("获得总列数"+columnCount); /*
    columnName=new String[columnCount];
    for(int i=1;i<=columnCount;i++)
    {
    if(i==1)
    {
    tableName=rsmd.getTableName(i);
    _logger.debug("取得的表名:"+tableName);
    }

    columnName[i-1]=rsmd.getColumnName(i);
    }
    */
    break;
    }
    _logger.debug("成功取得表的信息");

    //创建文件对象
     BufferedWriter fosObject=new BufferedWriter(
    new OutputStreamWriter(
    new FileOutputStream(gi.getIni("SysDir")+"/xmlFile/"+xmlFile),"GBK"));

    _logger.debug("开始写文件"); fosObject.write("<?xml version=\"1.0\" standalone=\"no\"?>"+(char)13+(char)10+(char)13+(char)10);
    fosObject.write("<table>"+(char)13+(char)10);
    fosObject.write(" <name>"+tableName+"</name>"+(char)13+(char)10);
    fosObject.write(" <period-start>"+sTime+"</period-start>"+(char)13+(char)10);
    fosObject.write(" <time>"+year+"/"+month+"/"+day+"/ "+hour+":"+minute+"</time>"+(char)13+(char)10);

    while(rs2.next())
    {
    _logger.debug("准备读取行");
    fosObject.write(" <row>"+(char)13+(char)10);
    for(int i=1;i<=columnCount;i++)
    {
    fosObject.write(" <column>"+(char)13+(char)10);
    fosObject.write(" <columnName>"+i+"</columnName>"+(char)13+(char)10);
    fosObject.write(" <columnValue>"+rs2.getString(i)+"</columnValue>"+(char)13+(char)10);
    fosObject.write(" </column>"+(char)13+(char)10);
    }
    fosObject.write(" </row>"+(char)13+(char)10);
    _logger.debug("读取完毕行");
    } fosObject.write("</table>"+(char)13+(char)10);

    fosObject.close();//关闭文件流
    _logger.debug("成功导出数据"); rs1.close();
    rs2.close();
    es.closeConnection(); return true;
    }
    catch(SQLException se)
    {
    _logger.debug("导出信息失败"+se.getMessage());
    return false;
    }
    catch(IOException e)
    {
    _logger.debug("导出信息失败"+e.getMessage());
    return false;
    }
    }
    }
      

  2.   

    给你找到个读的:
    import java.io.*;public class MadeMethod
    {
    public static void main(String[] args) 
    {
    if (args.length!=1)//判断是否有源文件
    {
    System.out.println("你需要输入源文件名,且一次只能输入一个!");
    System.out.println("使用方法:java GetName *.name");
    System.exit(1);
    }

    File fSource=new File(args[0]);

    if (!fSource.exists())//判断源文件是否存在
    {
    System.out.println("源文件:" + args[0] + "不存在!");
    System.exit(1);
    } if(!fSource.isFile())//判断源文件是文件,而不是目录
    {
    System.out.println("源文件:" + args[0] + "是一个目录!");
    System.exit(1);
    } if (!fSource.canRead())//判断源文件是否可读
    {
    System.out.println("不能读取源文件:" + args[0] + "!");
    System.exit(1);
    } String strTemp=(String)args[0];
    int pointIndex;
    //int pointIndex=strTemp.indexOf('.');
    //String fileName=strTemp.substring(0,pointIndex);//取得源文件名除去类型符
    String fileName=strTemp+".method"; try{

    DataInputStream fisSource=new DataInputStream(
    new BufferedInputStream(
    new FileInputStream(args[0])));
    DataOutputStream fosObject=new DataOutputStream(
    new BufferedOutputStream(
    new FileOutputStream(fileName))); System.out.println("开始进行名称提取,请稍后......");
    System.out.println();

    while(fisSource.available()>0)
    {
    strTemp=fisSource.readLine().trim();

    if (strTemp.length()>0)
    {
    pointIndex=strTemp.indexOf("String")+7;
    strTemp=strTemp.substring(pointIndex);
    pointIndex=strTemp.indexOf(" ");
    strTemp=strTemp.substring(0,pointIndex);

    fosObject.writeBytes("public String ");
    fosObject.writeBytes("get");
    fosObject.writeBytes(strTemp.substring(0,1).toUpperCase()+strTemp.substring(1)+"() {"+(char)13+(char)10);
    fosObject.writeBytes("  return "+strTemp+";"+(char)13+(char)10);
    fosObject.writeBytes("}"+(char)13+(char)10);
    fosObject.writeBytes("public void set");
    fosObject.writeBytes(strTemp.substring(0,1).toUpperCase()+strTemp.substring(1));
    fosObject.writeBytes("(String "+strTemp+") {"+(char)13+(char)10);
    fosObject.writeBytes("  this."+strTemp+" = "+strTemp+";"+(char)13+(char)10);
    fosObject.writeBytes("}"+(char)13+(char)10);

    //fosObject.writeBytes(strTemp);
    //fosObject.writeBytes(" = null;//"+(char)13+(char)10);
    System.out.println("正在处理"+strTemp);
    }
    }

    fisSource.close();//关闭文件流
    fosObject.close();
    System.out.println();
    System.out.println("处理完毕!"); }catch(FileNotFoundException e){
    System.out.println("文件没有发现!");
    }catch(IOException e){
    System.out.println("IO错误!");
    }
    }
    }