【急!!】不知道怎么写!!!

雷诺丶 2012-03-29 12:57:11
这是一段TXT 文档中数据

标题:诺基亚新闻
内容:11111111111111
时间:2010:10:10
地点:北京

标题:三星新闻
内容:22222222222222
时间:2011:10:10
地点:上海

标题:MOTO新闻
内容:11111111111111
时间:2010:10:10
地点:江南

前面 标题 内容等等是字段、、后面是具体内容,要插入数据库的数据、、
我现在就是不知道该怎么写??怎么给分离出来!!
...全文
193 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
雷诺丶 2012-03-29
  • 打赏
  • 举报
回复
恩、、好了、、已经把分 给你了!!!!!!
himi_ 2012-03-29
  • 打赏
  • 举报
回复
确实 我考虑少了 txt中输入完数据就回个车吧 我懒得改了
雷诺丶 2012-03-29
  • 打赏
  • 举报
回复
谢谢你了 【himi_】 问题解决的、、也谢谢其他朋友、、我要是分多的话、、肯定给你们每人 几千分、、可惜咱穷啊!!!!! 非常感谢、各位朋友啊!!!!!!!
雷诺丶 2012-03-29
  • 打赏
  • 举报
回复
我刚刚 debug看了是这个问题、、

[Quote=引用 16 楼 的回复:]

引用 15 楼 的回复:

直插入进去 两条数据,第三段新闻插不进去怎么回事啊


引用 10 楼 的回复:

Java code

public static void main(String[] args) {
String path = "D:\\news.txt";
String strLine;
String title = "", content = "……
[/Quote]
sdojqy1122 2012-03-29
  • 打赏
  • 举报
回复
你在结尾多行几个空行试试
sdojqy1122 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

直插入进去 两条数据,第三段新闻插不进去怎么回事啊


引用 10 楼 的回复:

Java code

public static void main(String[] args) {
String path = "D:\\news.txt";
String strLine;
String title = "", content = "", time = "", addre……
[/Quote]
那是因为第三条数据结尾少了个空行,程序就是这么写的,有问题的。
if (strLine.length() == 0) {
excuteSQL
.executeSql("insert into dwd(aaa,bbb,ccc,dddd) values('"
雷诺丶 2012-03-29
  • 打赏
  • 举报
回复
直插入进去 两条数据,第三段新闻插不进去怎么回事啊

[Quote=引用 10 楼 的回复:]

Java code

public static void main(String[] args) {
String path = "D:\\news.txt";
String strLine;
String title = "", content = "", time = "", address = "";
Buffer……
[/Quote]
菖蒲老先生 2012-03-29
  • 打赏
  • 举报
回复

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));
}
}
X497347200 2012-03-29
  • 打赏
  • 举报
回复


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,不然上面有问题,又不是时 秒 干嘛要用: 蛋疼
  • 打赏
  • 举报
回复
ObjectInputStream/ObjectOutputStream
qybao 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
按照 3楼 我是这样写的


Java code


public static void main(String[] args) {
String path = "D:\\news.txt";
String strLine;
try {
File file = new File(path);
……
[/Quote]
String[] vStrs = line.split("\\:");
要修改为 String[] vStrs = line.split("\\:", 2); 即只分割为2个元素的数组
否则,如果不包含:的字符串,长度不足的话会抛出异常,其次如果包含多个:的话(比如时间),会被分割为多个

himi_ 2012-03-29
  • 打赏
  • 举报
回复

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();
}
}
jiangyunlin1988 2012-03-29
  • 打赏
  • 举报
回复
你把整条记录的每他字段值都读出来之后。封装成一个对像再入库。
分界点就是 判断以“标题”开始的时候。
himi_ 2012-03-29
  • 打赏
  • 举报
回复
你老笨了,等着。
雷诺丶 2012-03-29
  • 打赏
  • 举报
回复
做完判断 就会执行一次插入语句、、这样插入的话 根本能就不是一条数据啊
雷诺丶 2012-03-29
  • 打赏
  • 举报
回复
按照 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();
}
}
雷诺丶 2012-03-29
  • 打赏
  • 举报
回复
这样写的话插入数据库只能插入一个字段吧!!
我要插入的是一条数据!!

[Quote=引用 3 楼 的回复:]

Java code

public static void main(String[] args) {
String path = "D:\\news.txt";
String strLine;
try {
File file = new File(path);
BufferedReader……
[/Quote]
菖蒲老先生 2012-03-29
  • 打赏
  • 举报
回复
然后无非就是split,substring之类等等字符串操作会吧。。。
himi_ 2012-03-29
  • 打赏
  • 举报
回复

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();
}
}
雷诺丶 2012-03-29
  • 打赏
  • 举报
回复
能不能教我怎么写!!!!

[Quote=引用 1 楼 的回复:]

读取每一行的字符串为变量strLine,
判断 strLine.startsWith("标题") ?
[/Quote]
加载更多回复(1)

62,614

社区成员

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

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