已阅读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年4.19江苏事业单位统考《综合知识和能力素质》笔试真题答案
- 中医内科学专业实践知识-5-2-真题(含答案与解析)-交互
- 银行2025反洗钱培训
- 建设高品质碳化硅单晶材料研发项目可行性研究报告写作模板-申批备案
- 外呼中心服务规范及管理制度汇编
- 大学生机器人工程专业职业生涯规划书
- 2025商业大厦租赁合同范本
- 脑梗死中医诊疗方案
- 国网陕西省电力有限公司招聘笔试真题2024
- 《三爱三节》主题班会课件
- 新版《关于防范遏制矿山领域重特大生产安全事故的硬措施》解读课件
- 《中国海军的发展史》课件
- 安全课《保护鼻子》
评论
0/150
提交评论