这是一段TXT 文档中数据标题:诺基亚新闻
内容:11111111111111
时间:2010:10:10
地点:北京标题:三星新闻
内容:22222222222222
时间:2011:10:10
地点:上海标题:MOTO新闻
内容:11111111111111
时间:2010:10:10
地点:江南前面 标题 内容等等是字段、、后面是具体内容,要插入数据库的数据、、
我现在就是不知道该怎么写??怎么给分离出来!!
内容:11111111111111
时间:2010:10:10
地点:北京标题:三星新闻
内容:22222222222222
时间:2011:10:10
地点:上海标题:MOTO新闻
内容:11111111111111
时间:2010:10:10
地点:江南前面 标题 内容等等是字段、、后面是具体内容,要插入数据库的数据、、
我现在就是不知道该怎么写??怎么给分离出来!!
判断 strLine.startsWith("标题") ?
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();
}
}
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();
}
}
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();
}
}
要修改为 String[] vStrs = line.split("\\:", 2); 即只分割为2个元素的数组
否则,如果不包含:的字符串,长度不足的话会抛出异常,其次如果包含多个:的话(比如时间),会被分割为多个
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,不然上面有问题,又不是时 秒 干嘛要用: 蛋疼
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));
}
}
if (strLine.length() == 0) {
excuteSQL
.executeSql("insert into dwd(aaa,bbb,ccc,dddd) values('"