jsp如何将oracle存储过程返回的结果集保存到txt文本中?请各位高手指点!谢谢!
解决方案 »
- 求救!!!我用DAO设计模式连接数据库,可是怎么搞数据也不能插进去.也不提示任何错误.
- 求助:不能进入到文件夹,却能打开servlet和html文件?
- 为什么我的jsp中request.getcontentlength等于 -1
- jsp 如何实现结贴,上一主题,下一主题的
- 如何用ADO方式把JSP和ACCESS连接上???
- session timeout 后,如何将页面重定向到一个登陆页面或弹出一个对话框
- 如果获取session消亡的时间
- 怎么才能求得数据库中某列的和?
- struts2或者js获取本地文件路径不对。
- 在Myeclipse中使用bool插件.pom报错
- 请教:数据库里面的数据如何读到jsp页面?(在线等)
- struts2方法过滤拦截器如何获取Action请求对应的方法
给你玩玩。没去取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();
}
}}