急急急,求帮助

sailling0521 2011-09-10 08:05:06
id:1;catid:8;typeid:0;title:IMS引导融合进入灿烂的明天;style:;thumb:http://www.fzjunling.com:90/cms/uploadfile/2011/0618/20110618021945807.png;keywords:引导 融合 进入 IMS;description:IMS引导融合进入灿烂的明天 ,随着网络融合的不断加深,互联网上的丰富业务将逐渐移植到电信网络上。IMS吸收与借鉴了互联网的开放模式;posids:1;url:http://www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=1;listorder:0;status:99;sysadd:1;islink:0;username:admin;inputtime:1308278108;updatetime:1308624394$id:7;catid:8;typeid:0;title:我的投稿我的投稿我的投稿我的投稿2;style:;thumb:;keywords:我的 投稿 我的;description:我的投稿我的投稿我的投稿;posids:0;url:http://www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=7;listorder:0;status:99;sysadd:0;islink:0;username:abc;inputtime:1308562243;updatetime:1308562243$id:9;catid:8;typeid:0;title:市话、长话、漫游一个价 国内接听全免费—全球通套餐全面;style:;thumb:;keywords:市话 长话 漫游;description:时间成就价值!16年精品网络,精彩应用、精细服务,全球通成就你我品质生活。2011年5月17日起,中国移动全球通套餐全面升级,新推上网、商...;posids:0;url:http://www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=9;listorder:0;status:99;sysadd:0;islink:0;username:adminx;inputtime:1308894309;updatetime:1308894309$id:10;catid:8;typeid:0;title:“我有积分、全家乐享”父亲节秒杀活动中奖清单公布;style:;thumb:;keywords:我有 积分 全家;description:上述客户参与我有积分、全家乐享父亲节秒杀活动并秒杀成功,特此公布。具体中奖客户是属于超低积分兑机还是半价购机的范畴,详询10086。;posids:1;url:http://www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=10;listorder:0;status:99;sysadd:0;islink:0;username:adminx;inputtime:1308895190;updatetime:1311146689$id:11;catid:8;typeid:0;title:玩手机游戏 享缤纷好礼;style:;thumb:;keywords:玩 手机游戏;description:手机游戏精彩备战,等你随时开玩,缤纷活动指尖狂欢,豪华大礼快来一起畅享;posids:0;url:http://www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=11;listorder:0;status:99;sysadd:0;islink:0;username:adminx;inputtime:1308895223;updatetime:1308895223


这是一段内容,我要怎么截取,
其实内容是3段新闻, 新闻属性有id,catid,typeid等等,每段新闻都是以“$”结尾,
求高手指教
...全文
179 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
打油的程序员 2011-09-11
  • 打赏
  • 举报
回复


/*output

----------------------item0-----------------------------------
1
8
0
IMS引导融合进入灿烂的明天

http://www.fzjunling.com:90/cms/uploadfile/2011/0618/20110618021945807.png
引导 融合 进入 IMS
IMS引导融合进入灿烂的明天 ,随着网络融合的不断加深,互联网上的丰富业务将逐渐移植到电信网络上。IMS吸收与借鉴了互联网的开放模式
1
//www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=1
0
99
1
0
admin
1308278108
1308624394

----------------------item1-----------------------------------
7
8
0
我的投稿我的投稿我的投稿我的投稿2


我的 投稿 我的
我的投稿我的投稿我的投稿
0
//www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=7
0
99
0
0
abc
1308562243
1308562243

----------------------item2-----------------------------------
9
8
0
市话、长话、漫游一个价 国内接听全免费—全球通套餐全面


市话 长话 漫游
时间成就价值!16年精品网络,精彩应用、精细服务,全球通成就你我品质生活。2011年5月17日起,中国移动全球通套餐全面升级,新推上网、商...
0
//www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=9
0
99
0
0
adminx
1308894309
1308894309

----------------------item3-----------------------------------
10
8
0
“我有积分、全家乐享”父亲节秒杀活动中奖清单公布


我有 积分 全家
上述客户参与我有积分、全家乐享父亲节秒杀活动并秒杀成功,特此公布。具体中奖客户是属于超低积分兑机还是半价购机的范畴,详询10086。
1
//www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=10
0
99
0
0
adminx
1308895190
1311146689

----------------------item4-----------------------------------
11
8
0
玩手机游戏 享缤纷好礼


玩 手机游戏
手机游戏精彩备战,等你随时开玩,缤纷活动指尖狂欢,豪华大礼快来一起畅享
0
//www.fzjunling.com:90/cms/index.php?m=content&c=index&a=show&catid=8&id=11
0
99
0
0
adminx
1308895223
1308895223

*/


打油的程序员 2011-09-11
  • 打赏
  • 举报
回复

package test;

import java.io.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;

public class Parse {
public static ArrayList<Item> items = new ArrayList<Item>();

public static void main(String[] args) {
String string = fileToString();// 假设你的某个文件有一段包括很多新闻的文字,将它用一个string存储起来
String regex = "id:(.*?)[;]catid:(.*?)[;]typeid:(.*?)[;]title:(.*?)[;]style:(.*?)[;]thumb:(.*?)[;]keywords:(.*?)[;]description:(.*?)[;]posids:(.*?)[;]url:http:(.*?)[;]listorder:(.*?)[;]status:(.*?)[;]sysadd:(.*?)[;]islink:(.*?)[;]username:(.*?)[;]inputtime:(.*?)[;]updatetime:(.*?)[$]";
Matcher matcher = Pattern.compile(regex, Pattern.DOTALL)
.matcher(string);//如果存在17个字段,而且已$结尾的串都将被匹配到

while (matcher.find()) {
// System.out.println("--------------------------------------------------------");
// for(int j=0; j<17; j++)
// System.out.println(matcher.group(j+1));
int id = Integer.parseInt(matcher.group(1));// //建立对象前 获得类对象的各个字段数据
int catid = Integer.parseInt(matcher.group(2));//假设你的代码正确的 不在进行异常处理
int typeid = Integer.parseInt(matcher.group(3));
String title = matcher.group(4);
String style = matcher.group(5);
String thumb = matcher.group(6);
String keywords = matcher.group(7);
String description = matcher.group(8);
int posids = Integer.parseInt(matcher.group(9));
String url = matcher.group(10);
int listorder = Integer.parseInt(matcher.group(11));
int status = Integer.parseInt(matcher.group(12));
int sysadd = Integer.parseInt(matcher.group(13));
int islink = Integer.parseInt(matcher.group(14));
String username = matcher.group(15);
String inputtime = matcher.group(16);
String updatetime = matcher.group(17);

Item tempItem = Item.createItem(id, catid,
typeid,
title,// 建立对象
style, thumb, keywords, description, posids, url,
listorder, status, sysadd, islink, username, inputtime,
updatetime);
items.add(tempItem);// 将建立的item添加的表中
}

for (int i = 0; i < items.size(); i++) {// 把表中的数据打印出来,对比文件 是否正确提取数据
System.out
.println("----------------------item"+i+"-----------------------------------");
Item item = items.get(i);
System.out.println(item);
}
}

// //////////////////////////////////////////////////////////////////////////////////////////////////////
public static String fileToString() {//在对话框中选中文件,把文件中的内容读出到字符串
StringBuilder sb = new StringBuilder();
JFileChooser jFileChooser = new JFileChooser();
javax.swing.filechooser.FileFilter fileFilter = new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) {
if (file.isDirectory())
return true;
String fileName = file.getName();
if (fileName.toUpperCase().endsWith("TXT"))
return true;
return false;
}

public String getDescription() {
return "读取文本文件";
}
};
// 建立一个过滤文件类型的过滤器(对话框中正确显示文件),是否启用过滤器决定于下面两句话
// jFileChooser.addChoosableFileFilter(fileFilter);
// jFileChooser.setFileFilter(fileFilter);
int returnValue = jFileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File file = jFileChooser.getSelectedFile();
if (!file.exists()) {
prompt("文件不存在");
return null;
}
// 检查文件名是否符合要求,这一步暂时省略......................................
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));

String string;
while ((string = br.readLine()) != null) {
sb.append(string);
sb.append("\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return sb.toString();
}

// ///////////////////////////////////////////////////////////////////////////////////////////////////////
public static int prompt(String promptMessage) {
return JOptionPane.showConfirmDialog(null, promptMessage, "友情提示",
JOptionPane.WARNING_MESSAGE);
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////

}

class Item {// 新闻类

private int id;// 目前我只是大概的认为他们是int或String //
// 如果将来发现操作不方便或效率出了问题,有些可以换成其他类型,比如enmu, byte long...
private int catid;
private int typeid;
private String title;
private String style;
private String thumb;
private String keywords;
private String description;
private int posids;
private String url;
private int listorder;
private int status;
private int sysadd;
private int islink;
private String username;
private String inputtime;
private String updatetime;

public Item(int id, int catid, int typeid, String title, String style,
String thumb, String keywords, String description, int posids,
String url, int listorder, int status, int sysadd, int islink,
String username, String inputtime, String updatetime) {
super();
this.id = id;
this.catid = catid;
this.typeid = typeid;
this.title = title;
this.style = style;
this.thumb = thumb;
this.keywords = keywords;
this.description = description;
this.posids = posids;
this.url = url;
this.listorder = listorder;
this.status = status;
this.sysadd = sysadd;
this.islink = islink;
this.username = username;
this.inputtime = inputtime;
this.updatetime = updatetime;
}

private Item() {
}

public static Item createItem() {
return new Item();
}

public static Item createItem(int id, int catid, int typeid, String title,
String style, String thumb, String keywords, String description,
int posids, String url, int listorder, int status, int sysadd,
int islink, String username, String inputtime, String updatetime) {
return new Item(id, catid, typeid, title, style, thumb, keywords,
description, posids, url, listorder, status, sysadd, islink,
username, inputtime, updatetime);
}

public String toString() {// 可以利用这个函数来控制输出格式
return id + "\n" + catid + "\n" + typeid
+ "\n" + title + "\n" + style
+ "\n" + thumb + "\n" + keywords
+ "\n" + description + "\n" + posids
+ "\n" + url + "\n" + listorder
+ "\n" + status + "\n" + sysadd
+ "\n" + islink + "\n" + username
+ "\n" + inputtime + "\n" + updatetime
+ "\n" ;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getCatid() {
return catid;
}

public void setCatid(int catid) {
this.catid = catid;
}

public int getTypeid() {
return typeid;
}

public void setTypeid(int typeid) {
this.typeid = typeid;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getStyle() {
return style;
}

public void setStyle(String style) {
this.style = style;
}

public String getThumb() {
return thumb;
}

public void setThumb(String thumb) {
this.thumb = thumb;
}

public String getKeywords() {
return keywords;
}

public void setKeywords(String keywords) {
this.keywords = keywords;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getPosids() {
return posids;
}

public void setPosids(int posids) {
this.posids = posids;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public int getListorder() {
return listorder;
}

public void setListorder(int listorder) {
this.listorder = listorder;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public int getSysadd() {
return sysadd;
}

public void setSysadd(int sysadd) {
this.sysadd = sysadd;
}

public int getIslink() {
return islink;
}

public void setIslink(int islink) {
this.islink = islink;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getInputtime() {
return inputtime;
}

public void setInputtime(String inputtime) {
this.inputtime = inputtime;
}

public String getUpdatetime() {
return updatetime;
}

public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}

}


topzergling 2011-09-10
  • 打赏
  • 举报
回复
对了 这个是最好在文本尾加一个"$"
topzergling 2011-09-10
  • 打赏
  • 举报
回复

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Parse {
private static StringBuilder sb;
private static String s;

private static void readFromFile(String file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
try {
while (true) {
add();
String temp = br.readLine();
if (temp.contains("$")) {
sb.append(temp.substring(0, temp.indexOf("$")));
toObject(sb.toString().split(";"));
s = temp.substring(temp.indexOf("$") + 1);
continue;
}
sb.append(temp);
}
} catch (EOFException e) {
System.out.println("文件已读完!");
}

}

private static void add() {
if(!s.equals("")){
sb.append(s);
s="";
}
}

private static void toObject(String[] str) {
//此处添加解析字符串数组代码,将其转换为一个新闻对象
sb=new StringBuilder();
}

public static void main(String[] args) {
try {
readFromFile("SomeFile");
} catch (IOException e) {
System.out.println("未找到文件!");
}
}

}


这是刚写的一个
从文本文件中读取信息的部分
转换为新闻对象没写
求测试……
topzergling 2011-09-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 k3108001263 的回复:]

1.最后一段不是以“$”结尾,
2.发现你那一堆乱七八糟的东西,有5个typeid,而你说 内容是3段新闻,你给我解释以下如何才叫一段新闻
3。
[/Quote]
LS看的真是仔细
这应该是5段新闻了吧
有4个"$"
打油的程序员 2011-09-10
  • 打赏
  • 举报
回复
1.最后一段不是以“$”结尾,
2.发现你那一堆乱七八糟的东西,有5个typeid,而你说 内容是3段新闻,你给我解释以下如何才叫一段新闻
3。
sailling0521 2011-09-10
  • 打赏
  • 举报
回复
求解啊
sailling0521 2011-09-10
  • 打赏
  • 举报
回复
在线等

62,614

社区成员

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

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