这是一段TXT 文档中数据标题:诺基亚新闻
内容:11111111111111
时间:2010:10:10
地点:北京标题:三星新闻
内容:22222222222222
时间:2011:10:10
地点:上海标题:MOTO新闻
内容:11111111111111
时间:2010:10:10
地点:江南前面 标题 内容等等是字段、、后面是具体内容,要插入数据库的数据、、
我现在就是不知道该怎么写??怎么给分离出来!!

解决方案 »

  1.   

    读取每一行的字符串为变量strLine,
    判断 strLine.startsWith("标题") ?
      

  2.   


    public static void main(String[] args) {
    String path = "D:\\news.txt";
    String strLine;
    try {
    File file = new File(path);
    BufferedReader br = new BufferedReader(new FileReader(file));
    while ((strLine = br.readLine()) != null) {
    if (strLine.startsWith("标题")) {
    System.out.println("你懂的...");
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
      

  3.   

    然后无非就是split,substring之类等等字符串操作会吧
      

  4.   

    按照 3楼  我是这样写的
      public static void main(String[] args) {
            String path = "D:\\news.txt";
            String strLine;
            try {
                File file = new File(path);
                BufferedReader br = new BufferedReader(new FileReader(file));
                while ((line = reader.readLine()) != null) {
             String[] vStrs = line.split("\\:");
                //String bt = vStrs[0];
             if(line.startsWith("标题")){
             aaa = vStrs[1];
             }else if(line.startsWith("内容")){
             bbb = vStrs[1];
             }else if(line.startsWith("时间")){
             ccc = vStrs[1];
             }else{
             dddd = vStrs[1];
             }
                
                excuteSQL.executeSql("insert into dwd(aaa,bbb,ccc,dddd) values('" + aaa + "','" + bbb + "','" + ccc + "','" + dddd + "');");
            }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
      

  5.   


    public static void main(String[] args) {
    String path = "D:\\news.txt";
    String strLine;
    String title = "", content = "", time = "", address = "";
    BufferedReader br;
    try {
    File file = new File(path);
     br= new BufferedReader(new FileReader(file)); while ((strLine = br.readLine()) != null) {
    if (strLine.startsWith("标题")) {
    title = strLine.substring(3);
    } else if (strLine.startsWith("内容")) {
    content = strLine.substring(3);
    } else if (strLine.startsWith("时间")) {
    time = strLine.substring(3);
    } else if (strLine.startsWith("地点")) {
    address = strLine.substring(3);
    }
    if (strLine.length() == 0) {
    excuteSQL
    .executeSql("insert into dwd(aaa,bbb,ccc,dddd) values('"
    + title
    + "','"
    + content
    + "','"
    + time
    + "','"
    + address + "');");
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    //刚才忘记了
    br.close();
    }
    }
      

  6.   

    String[] vStrs = line.split("\\:");
    要修改为 String[] vStrs = line.split("\\:", 2); 即只分割为2个元素的数组
    否则,如果不包含:的字符串,长度不足的话会抛出异常,其次如果包含多个:的话(比如时间),会被分割为多个
      

  7.   



    BufferedReader br = null;
    try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(
    "Fuck_you.txt"), "utf-8"));
    String line = null;
    String[] str=null;
    while ((line = br.readLine()) != null) {

    if (line.startsWith("标题")) {

     str = line.split(":");
     String str1=str[1].trim();//标题
    line = br.readLine();
    str = line.split(":");
     String str2=str[1].trim();//内容
     line = br.readLine();
     str = line.split(":");
     String str3=str[1].trim();//时间
     line = br.readLine();
     str = line.split(":");
     String str4=str[1].trim();//地点
    //News n=new News(); hibernate或者直接插入 你自己看着办吧
    //n.setTitle(str1);.................
    //dao.insert(n); 

    } } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException("XXXX异常", e);
    } finally {
    try {
    br.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace(); } }时间:2011:10:10 时间格式改下,2011-10-10,不然上面有问题,又不是时 秒 干嘛要用: 蛋疼
      

  8.   


    public static void main(String[] args) throws Exception { List<String[]> list = new ArrayList<String[]>(); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("E:/data.txt"), "utf-8"));
    String line = null;
    int i = 0;
    String[] data = new String[4];

    while ((line = br.readLine()) != null) {
    if (line.equals("")) {
    continue;
    }
    data[i % 4] = line.substring(line.indexOf(":") + 1);
    if (i > 0 && i % 4 == 3) {
    list.add(data);
    data = new String[4];
    }
    i++;
    } for (String[] d : list) {
    System.out.println(Arrays.toString(d));
    }
    }
      

  9.   

    那是因为第三条数据结尾少了个空行,程序就是这么写的,有问题的。
     if (strLine.length() == 0) {
                        excuteSQL
                                .executeSql("insert into dwd(aaa,bbb,ccc,dddd) values('"
      

  10.   

    我刚刚 debug看了是这个问题、、
      

  11.   

    谢谢你了  【himi_】 问题解决的、、也谢谢其他朋友、、我要是分多的话、、肯定给你们每人 几千分、、可惜咱穷啊!!!!!     非常感谢、各位朋友啊!!!!!!!
      

  12.   

    确实  我考虑少了 txt中输入完数据就回个车吧 我懒得改了