简体   繁体   English

如何为每个sql记录创建单独的输出文件?

[英]How do I create separate output file for each sql record?

public void selectdata(Connection conn, String database){

    String table = "Mo";  // using Mo table,
    String comandoSql = "SELECT * FROM " + database + "."+"dbo"+ "."+ table;
    try {
        stmt = conn.createStatement();  

        rs = stmt.executeQuery(comandoSql);
        System.out.println("Command sql: ");
        while (rs.next()) {
            String DocumentName = rs.getString(2); 
            doc1 = DocumentName;
            try {

                File file = new File("///.txt");  // path to output folder.
                FileWriter fileWritter = new FileWriter(file,true);
                BufferedWriter out = new BufferedWriter(fileWritter);
                out.write(doc1);
                out.newLine();
                out.close();

                  } catch (IOException e1) {
              System.out.println("Could not write in the file");
            } 
          }
    } catch (SQLException e) {
            e.printStackTrace();
    }  

this generates the output file but contains all the records in one file, my problem is how to generate separate file for each record don't want to do the post process in order to generate the separate files? 这将生成输出文件,但将所有记录都包含在一个文件中,我的问题是如何为每个记录生成单独的文件,不想为了生成单独的文件而进行后期处理? any help?????? 任何帮助?????? not fancy for scanner, because don't know how many lines will be in one record(it is a kind of multiple documents which stored into database table!!!!) 扫描仪不喜欢,因为不知道一条记录中将有多少行(它是存储在数据库表中的一种多文档!!!)

Just create a new file for each record: 只需为每个记录创建一个新文件:

 int counter = 0;

 while (rs.next()) {
        String DocumentName = rs.getString(2); 
        doc1 = DocumentName;
        try {

            File file = new File("///" + counter + ".txt");  // path to output folder.
            FileWriter fileWritter = new FileWriter(file,true);
            BufferedWriter out = new BufferedWriter(fileWritter);
            out.write(doc1);
            out.newLine();
            out.close();
            counter++;

              } catch (IOException e1) {
          System.out.println("Could not write in the file");
        } 
      }

Just create a counter variable or something to differentiate each file. 只需创建一个计数器变量或用于区分每个文件的内容即可。 Append the counter to the file name so it will create a new file for each record... 将计数器添加到文件名,以便为每个记录创建一个新文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否必须为我创建的每个组件创建一个单独的ActionListener? - Do I have to create a separate ActionListener for each component I create? 连接到Android时,是否必须为每个CRUD方法创建一个单独的php文件? - Do I have to create a separate php file for each CRUD method when connecting to Android? 如何创建ArrayList,以便可以记录每个项目的位置? - How do I create an ArrayList, so that I can record the position of each of these items? 如何在Netbeans的Java文件中为每个类创建单独的文件 - How to create separate file for each class in a java file in Netbeans 如何阅读txt文件的每一列,并将它们放在单独的数组中? - How do I read each column of a txt file, and place them in separate arrays? 如何将每条JSON记录放在单独的行上? - How to have each record of JSON on a separate line? 如何为我在activemq中拥有的每个消费者创建单独的队列? - How to create separate queue for each consumer I have in activemq? 如何将每个AsyncTask类放在单独的文件中? - How do put each AsyncTask class in a separate file? Xpages xp:customValidator:是否需要为每个customValidator创建一个单独的bean? - Xpages xp:customValidator: do I need to create a separate bean for each customValidator? 为每个值获取单独的记录 - Get separate record for each value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM