假设在文件夹txt下有100个txt文件
现在需要建立一个表,有两个列,一个储存文件路径,另一个储存txt文件的内容,每一条记录储存一个txt文件。
如何才能批量把这100个txt的路径和里面的内容导入mysql的一个表中

解决方案 »

  1.   

    这100 个文件照你的说法都在同一个目录下,则得出简单的方法是。1。 通过操作系统命令 windows的dir 或者 linux ls 行命令,把所有文件名列出,并复制到excel 中
    2。 利用EXCEL的公式生成 insert into table1 (pathName,fileCOntent) value ('/temp/txt/filename1.txt',LOAD_FILE('/temp/txt/filename1.txt'));
    3。 把这100行的命令直接贴到mysql行命令运行。一切OK。
      

  2.   

    如果是我的话,我就给这个txt建个类 假如叫 project,然后
    private String path;        //存路径
    private String content;       //存内容
    然后
    private void traverse(File file) throws Exception {

    String [] files = file.list();
    for (int i = 151; i < files.length; i++) {

    File productfile = new File(file, files[i]);
    String fname = productfile.getName();
    System.out.println(productfile);

    BufferedReader reader = new BufferedReader(new FileReader(productfile));

    String name = fname;
    String path= productfile.getPath();
    StringBuffer content = new StringBuffer();
    String line = reader.readLine();
    while (line != null && !line.equals(Extractor.SEPARATOR)){
    content.append(line).append("\r\n");
    line = reader.readLine();
    }
    // make the Product object
    Project p = new Project();
    然后 把这个Project 持久到数据库里,这个你会吗?不会再说
    }
      

  3.   

    哦,那个for的 int i是从0 开始,写错了
      

  4.   

    这个做法好像在windows系统下不能用啊?我看到书上也是这么讲究试了一下,结果不行。我的就是XP系统。
      

  5.   

    试一下这样:
    ls -l |awk '{print "insert into table1 (pathName,fileCOntent) value (/temp/txt/"$1",LOAD_FILE(/temp/txt/$1)")' >do.sql
    mysql dbname<do.sql