jsp如何将oracle存储过程返回的结果集保存到txt文本中?请各位高手指点!谢谢!

解决方案 »

  1.   

    就跟一般的ResultSet一样啊。你取rs数据会取吧?
      

  2.   

    在页面显示我知道,就是不知道往TXT里怎么写,能不能给个代码演示一下?
      

  3.   

    写txt就是文件存取啊。
    给你玩玩。没去取rs数据,懒得弄了。其实这些网上很多的。
    package test;import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;public class FileRW { /**
     * 读文件并按行打印
     * 
     * @param fileName
     *            文件全路径名
     * @throws Exception
     */
    public static void readFile(String fileName) throws Exception {
    System.out.println("read file begin...");
    String s = null;
    File f = new File(fileName);
    BufferedReader br = null;
    try {
    // 读取文件对应的输出流buffer
    br = new BufferedReader(new InputStreamReader(
    new FileInputStream(f)));
    // 一行行读
    while ((s = br.readLine()) != null) {
    // 打印
    System.out.println(s);
    }
    System.out.println("read file finished");
    } catch (Exception e) {
    throw e;
    } finally {
    try {
    br.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } /**
     * 查找并打印文件中存在字符串str的行数及该行内容
     * @param fileName  文件全路径名
     * @param str 包含的字符串
     * @throws Exception
     */
    public static void searchFile(String fileName, String str) throws Exception {
    System.out.println("search file begin...");
    String s = null;
    File f = new File(fileName);
    BufferedReader br = null;
    try {
    // 读取文件对应的输出流buffer
    br = new BufferedReader(new InputStreamReader(
    new FileInputStream(f)));
    int i = 0;
    // 一行行读
    while ((s = br.readLine()) != null) {
    i++;
    if (s.indexOf(str) >= 0) {
    // 打印
    System.out.println("第 " + i + " 行存在输入字符串:\n" + s);
    }
    } System.out.println("search file finished");
    } catch (Exception e) {
    throw e;
    } finally {
    try {
    br.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } /**
     * 把字符串存储到文件
     * 
     * @param fileName
     *            文件全路径名
     * @param str
     *            字符串
     * @param isAppend
     *            true表示文件追加,false表示文件覆盖
     * @throws Exception
     */
    public static void saveFile(String fileName, String str, boolean isAppend)
    throws Exception {
    File f = new File(fileName);
    BufferedWriter bw = null;
    try {
    // 如果不存在
    if (!f.exists()) {
    // 建新文件
    if (!f.createNewFile()) {
    throw new IOException("file create failure...");
    }
    }
    // isAppend: true表示文件追加,false表示文件覆盖
    bw = new BufferedWriter(new FileWriter(f, isAppend));
    bw.write(str); } catch (Exception e) {
    e.printStackTrace();
    } finally {
    // 最后在finally关闭
    try {
    bw.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } /**
     * 组织需要存入到文件的字符串
     * 
     * @return
     */
    public static String getContent() {
    String str = ""; for (int i = 0; i < 1000; i++) {
    str += "A" + String.valueOf(i) + "\t" + "B"
    + String.valueOf(i * 10) + "\t" + "C"
    + String.valueOf(i * 100) + "\n";
    }
    return str;
    } public static void main(String[] args) {
    try { String fileName = "E:\\result1.txt";
    String str = getContent();
    // 写文件
    FileRW.saveFile(fileName, str, false);
    // 读文件
    FileRW.readFile(fileName);
    // 查找带有B100字样的行数并打印
    FileRW.searchFile(fileName, "B100");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}