有一规则文本需读出

aaaa,1212121
bbb,458048509
怎么读出来啊 读出来还要放到数据库对应的字段中去?
我看到的程序是这样的:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*,java.lang.*"%>
<html>
<head>
<title>读取所有的文件数据</title>
</head>
<body>
<%
String path=request.getRealPath(".");
FileReader fr=new FileReader(path + "\\ReadData.txt");
//关键在于读取过程中,要判断所读取的字符是否已经到了文件的末尾,并且这个字符是不是文件中的断行符,即判断该字符值是否为13。
int c=fr.read();//从文件中读取一个字符
//判断是否已读到文件结尾
while(c!=-1){
 out.print((char)c);//输出读到的数据
 c=fr.read();//从文件中继续读取数据
 if(c==13){//判断是否为断行字符
  out.print("<br>");//输出分行标签
  fr.skip(1);//略过一个字符
  //c=fr.read();//读取一个字符
 }
}
fr.close();
%>
</body>
</html>
但读出来的是char(c)
怎么放到String去
请教各位大侠了 

解决方案 »

  1.   

    以逗号为分界,,读出来后强转String,不知道,这样可以不哦,,,
      

  2.   

    给你一个很经典的例子,BufferedReader、InputStreamReader和FileInputStream的嵌套按行读出D:\Test.txt的内容。至于存入数据库,可以用jdbc连接数据库然后insert into就好。import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class ReadFile{
    public static void main(String args[]){
    String s;
    try{
    BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream("d://Test.txt")));
    while((s=in.readLine())!=null)
    System.out.println("Read:"+s);
    }catch(FileNotFoundException e){
    System.out.println("File ont found!");
    System.exit(-2);
    }catch(IOException e){
    System.out.println("Error:"+e);
    System.exit(-3);
    }
    }
    }