读取txt文件中数据的问题

yuyeahcool 2005-08-05 09:13:23
我把表里的记录读出来保存在文本文件中,比如说表里有username、password两个字段,有3条记录
分别是:
li,123
liu,234
wang,345
项目要求我保存在文本文件中的样式是:
li●123
liu●234
wang●345
既字段间用“●”隔开,每条记录要求换行,在写入文本文件时,换行符我用的是“\r\n”
现在我要读这个文本中的数据,恢复到数据库里,但不知道怎么读这个文本文件,怎么把文本文件中的数据按记录读,然后按字段付给变量,循环插入数据库?请大家告诉我,最好能给我断程序,万分感谢了!!!
...全文
104 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
congliu 2005-08-05
  • 打赏
  • 举报
回复
以下一个读文件的例程,希望对楼主有帮助.
/* Readfile.java

读取文件的内容,并将原样输出至屏幕上

使用方法:java Readfile 文件名

*/



import java.io.*;



public class Readfile

{

public static void main(String[] args)

{

byte[] buff = new byte[1024];

boolean cont = true;

FileInputStream infile = null;



// 生成对象infile 准备读取文件

try

{

infile = new FileInputStream(args[0]);

}

catch (FileNotFoundException e)

{

System.err.println("没有找到文件");

System.exit(1);

}



while (cont)

{

try

{

int n = infile.read(buff); // 从文件读取数据

System.out.write(buff, 0, n); // 写入System.out中

}

catch (Exception e)

{

cont = false;

}

}



try

{

infile.close();

}

catch (IOException e)

{

System.err.println("文件错误");

System.exit(1);

}

}

}
lcllcl987 2005-08-05
  • 打赏
  • 举报
回复
readline该文件,然后以●分割每行(用charat也可以),应该不难.
分多点的话我就帮你写了

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧