




已阅读5页,还剩24页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
这个是把文件夹下所有txt文件递归的读了,你改简单一下就OKimport java.sql.*;import java.io.*;public class txtdb_overpublic static void operate(String paths)File d = new File(paths);/取得当前文件夹下所有文件和目录的列表File lists = d.listFiles();/用另外一个String保存当前文件路径,以便递归String pathss = paths;int count = 1;/对当前目录下面所有文件进行检索for(int i = 0; i 0)dbtxtcontent = dbtxtcontent.replace(,“);/接着读取下一行Line = br.readLine();br.close();fr.close();catch(IOException e)System.err.println(Error Code: + e);boolean flag = false;/进行数据库操作try /驱动程序String driver = sun.jdbc.odbc.JdbcOdbcDriver;/数据库服务器地址及数据库名String url = jdbc:odbc:mysqltxtdb;Connection con = null; Statement st; ResultSet rs; try /声明数据库连接Class.forName(driver).newInstance(); System.out.println(驱动接口连接成功!); catch (Exception ex) /进行错误处理System.out.println(驱动连接失败!); try /连接数据库con = DriverManager.getConnection(url,root,); st = con.createStatement(); /声明SQLString SQL = Insert into txtmsg value (default, / + dbtxtname + / + , + / + dbtxtcontent + / + ); ;/进行插入操作tryrs = st.executeQuery(SQL);System.out.println(对数据库操作成功!);catch(SQLException e)/System.err.println(e); catch (Exception e) /输出连接错误信息e.printStackTrace(); /关闭连接finally if (con = null) return; try con.close(); catch (Exception e) e.printStackTrace(); flag = true;System.out.println(文件存储完毕!/n); catch (Exception e) System.err.println(Error CODE: + e); elseif(!pathss.equals(paths)pathss = paths;/进入下一级目录pathss = pathss + listsi.getName() + /;/递归遍历所有目录operate(pathss);public static void main(String args)/String paths = new String(D:/Work/2004.10.8/test system/test01/txt/);String paths = new String(D:/test/txt/);operate(paths);java.io.File file=new java.io.File(c:/a.txt);String file_str=;byte bytes = null;try java.io.BufferedInputStream bis = new java.io.BufferedInputStream(newjava.io.FileInputStream(file);int n = (int) file.length();bytes = new byten;bis.read(bytes);file_str = new String(bytes);catch (IOException ex) ex.printStackTrace();/读取文件完成,把文件写如数据库String sql=insert into table(file_content) values(+file_str+);执行SQL写入数据库人想做一个C/S系统,从本地硬盘读取一个.txt文件,并显示文件的路径,然后把内容写到数据库中,怎么做?请各位大虾给个思路,有源代码最好,谢谢!这个不难吧,数据库是在本地吗?网络的我还不会,还没学,哈哈!如果是本地的话,用File类来定义文件,FileInputStream或者BufferRead来读取文件内容。数据库JDBC-ODBC桥的话简单点,如果用纯JAVA连接的话,要倒入连接包,名字忘记了。呵呵,新手,才学到这么多1.读取用FileInputStream就行2.显示文件路径是JFileChooser控件有的功能3.内容写到数据库,最好文件中用分隔符把数据按字段隔开,先生成INSERT语句(注意重复数据),用JDBC操作就可以了这个是把文件夹下所有txt文件递归的读了,你改简单一下就OKimport java.sql.*;import java.io.*;public class txtdb_overpublic static void operate(String paths)File d = new File(paths);/取得当前文件夹下所有文件和目录的列表File lists = d.listFiles();/用另外一个String保存当前文件路径,以便递归String pathss = paths;int count = 1;/对当前目录下面所有文件进行检索for(int i = 0; i 0)dbtxtcontent = dbtxtcontent.replace(,“);/接着读取下一行Line = br.readLine();br.close();fr.close();catch(IOException e)System.err.println(Error Code: + e);boolean flag = false;/进行数据库操作try /驱动程序String driver = sun.jdbc.odbc.JdbcOdbcDriver;/数据库服务器地址及数据库名String url = jdbc:odbc:mysqltxtdb;Connection con = null; Statement st; ResultSet rs; try /声明数据库连接Class.forName(driver).newInstance(); System.out.println(驱动接口连接成功!); catch (Exception ex) /进行错误处理System.out.println(驱动连接失败!); try /连接数据库con = DriverManager.getConnection(url,root,); st = con.createStatement(); /声明SQLString SQL = Insert into txtmsg value (default, / + dbtxtname + / + , + / + dbtxtcontent + / + ); ;/进行插入操作tryrs = st.executeQuery(SQL);System.out.println(对数据库操作成功!);catch(SQLException e)/System.err.println(e); catch (Exception e) /输出连接错误信息e.printStackTrace(); /关闭连接finally if (con = null) return; try con.close(); catch (Exception e) e.printStackTrace(); flag = true;System.out.println(文件存储完毕!/n); catch (Exception e) System.err.println(Error CODE: + e); elseif(!pathss.equals(paths)pathss = paths;/进入下一级目录pathss = pathss + listsi.getName() + /;/递归遍历所有目录operate(pathss);public static void main(String args)/String paths = new String(D:/Work/2004.10.8/test system/test01/txt/);String paths = new String(D:/test/txt/);operate(paths);java.io.File file=new java.io.File(c:/a.txt);String file_str=;byte bytes = null;try java.io.BufferedInputStream bis = new java.io.BufferedInputStream(newjava.io.FileInputStream(file);int n = (int) file.length();bytes = new byten;bis.read(bytes);file_str = new String(bytes);catch (IOException ex) ex.printStackTrace();/读取文件完成,把文件写如数据库String sql=insert into table(file_content) values(+file_str+);执行SQL写入数据库Java读取桌面txt文件,然后输出到另一个txt文件中,但是输出的文件却是空的。求解原因。 检举 | 2012-2-28 12:17 提问者: xiongk_c | 浏览次数:176次import java.io.*;public class FileInAndOutByte static String s=;/*-*/ public static void main(String args) readF();writeF();System.out.println(s=+s);public static void readF() String strR =C:/Documents and Settings/Administrator/桌面/ip.txt;try File myfile=new File(strR);FileInputStream fis=new FileInputStream(myfile);byte data1=new byte1024;int rs=0;while(rs=fis.read(data1, 0, rs)0) s+=new String(data1,0,rs);for(int i=0;idata1.length;i+) System.out.println(data1i); catch(Exception e) e.printStackTrace();/*-*/public static void writeF() String strW=C:/Documents and Settings/Administrator/桌面/Ram.txt;try byte data2=s.getBytes(); File youfile=new File(strW); FileOutputStream fos=new FileOutputStream(youfile); fos.write(data2); fos.flush(); fos.close(); catch(Exception e) e.printStackTrace();满意回答 FileInputStream 类的read(byte b, int off, int len) 是从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。你的程序中fis.read(data1, 0, rs)中的rs等于0,就是读0个字符到data1,输出当然是空的。File linkFile = new File(logKakunePath);String fname=linkFile.list();for(int i = 0; i fname.leng
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 保险培训讲师考试题库及答案
- 有用的企业面试题库及参考答案详解【夺分金卷】
- 足疗按摩师职业资格考试试题及答案
- 2025年直播电商主播与品牌合作模式创新与市场趋势报告
- 2025年老年健康管理长期照护服务模式与养老产业政策创新实践报告
- 2025年工业互联网平台生物识别技术在工业数据分析与挖掘中的应用报告
- 2025至2030年中国羊毛衫行业市场发展现状及投资方向研究报告
- 考点解析-华东师大版8年级下册期末试题含答案详解(基础题)
- 押题宝典执业药师资格证之《西药学专业二》试题含答案详解【轻巧夺冠】
- 2025版企业股权让与担保合同模板
- 民营医院成本管理办法
- 杭州银行薪资管理办法
- 教堂定期活动方案
- 福建省2025年中考物理真题及答案
- GB/T 2820.5-2025往复式内燃机驱动的交流发电机组第5部分:发电机组
- 呼吸机管路设计与应用
- 2025-2030年中国黑胶唱片行业市场现状供需分析及投资评估规划分析研究报告
- 马工程《教育学原理》核心框架解析
- 采石场人员管理制度
- 2025年湖北省高考物理试卷真题(含答案解析)
- 小学生无故旷课问题
评论
0/150
提交评论