如何截取File文件里的内容啊
我最近在学做唐诗宋词这个小软件,而那些诗词都是从通过File文件将其导入进去,我想把File文件内的内容将其截取成每一首一首的诗,在进行截取它的诗歌名、作者名和诗句,这要如何截取啊
[最优解释]
private static int pos = 0;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File mFile = new File("E:\\1.txt");
int fileSize = (int) mFile.length();
InputStream in = null ;
in = new FileInputStream(mFile) ;
byte[] bt = new byte[fileSize];
String title, author, text, note, trans;
in.read(bt);
title = getTitle(bt, fileSize);
author = getAuthor(bt, fileSize);
text = getText(bt, fileSize);
note = getNote(bt, fileSize);
trans = getTrans(bt, fileSize);
System.out.println("title: " + title);
System.out.println("author: " + author);
System.out.println("text: " + text);
System.out.println("note: " + note);
System.out.println("trans: " + trans);
}
private static String getTitle(byte[] bt, int fileSize)
{
String title = null, str = null;;
int titleStart = 0;
for(int i = pos; i < fileSize - 1; i += 2)
{
str = new String(bt, i, 2);
if(str.equals("《"))
{
titleStart = i + 2;
}
else if(str.equals("》"))
{
pos = i;
int titleLength = pos - titleStart;
byte[] titleBt = new byte[titleLength];
System.arraycopy(bt, titleStart, titleBt, 0, titleLength);
title = new String(titleBt);
break;
}
}
return title;
}
private static String getAuthor(byte[] bt, int fileSize)
{
String author = null, str = null;
int authorStart = 0;
authorStart = pos + 10;
for(int i = pos; i < fileSize - 1; i += 2)
{
str = new String(bt, i, 2);
if((authorStart != 0) && (i > authorStart)
&&(str.indexOf("\n") != -1))
{
pos = i;
int authorLength = pos - authorStart;
byte[] authorBt = new byte[authorLength];
System.arraycopy(bt, authorStart, authorBt, 0, authorLength);
author = new String(authorBt);
break;
}
}
return author;
}
private static String getText(byte[] bt, int fileSize)
{
String text = null, str = null;
int textStart = 0, textEnd = 0;
textStart = pos + 2;
for(int i = pos; i < fileSize - 1; i += 2)
{
str = new String(bt, i, 2);
if(str.equals("【"))
{
textEnd = i - 2;
int textLength = textEnd - textStart;
byte[] textBt = new byte[textLength];
System.arraycopy(bt, textStart, textBt, 0, textLength);
text = new String(textBt);
pos = i;
break;
}
}
return text;
}
private static String getNote(byte[] bt, int fileSize)
{
String note = null, str = null;
int noteStart = 0, noteEnd = 0;
noteStart = pos + 12;
for(int i = noteStart; i < fileSize - 1; i += 2)
{
str = new String(bt, i, 2);
if(str.equals("【"))
{
noteEnd = i - 2;
int noteLength = noteEnd - noteStart;
byte[] noteBt = new byte[noteLength];
System.arraycopy(bt, noteStart, noteBt, 0, noteLength);
note = new String(noteBt);
pos = i;
break;
}
}
return note;
}
private static String getTrans(byte[] bt, int fileSize)
{
String trans = null, str = null;
int transStart = 0, transEnd = 0;
transStart = pos + 12;
for(int i = transStart; i < fileSize - 1; i += 2)
{
str = new String(bt, i, 2);
if(str.equals("=="))
{
transEnd = i - 2;
int noteLength = transEnd - transStart;
byte[] noteBt = new byte[noteLength];
System.arraycopy(bt, transStart, noteBt, 0, noteLength);
trans = new String(noteBt);
pos = i;
break;
}
}
return trans;
}
[其他解释]
用正则当然是最好的方法,但是自己解析也不难,你检查一下,看看是否格式都是这样:
《标题》
作者:XXX
内容
【注解】:
1、XXX
...
【韵译】:
XXX
以及传说中的分割线
=============================
确定后分析就是了,首先把文件都读进内存,然后挨个分析文字,比如遇到“《”,做个标记--标题开始,遇到“》”,把从标题开始地方到现在的中间内容保存为标题,以此类推,把整首诗分析出来,遇到分割线,循环处理下一首,我之前说的“\xd\xa”,是回车换行符
[其他解释]
你的文件是什么格式的?txt?
如果有规律的话可以用工具导入到exl里,然后再生成sql数据库文件。
[其他解释]
用正则表达式吧,如果不懂正则表达式的话,去baidu搜索,30分钟学会正则表达式
[其他解释]
把文件读进内存,然后判断"\xd\xa",就是按行分析,找出一定规律后,针对性的写截取代码
[其他解释]
我弄了两天了还是没弄出来
[其他解释]
那要看你文件内容什么格式,有啥规律
[其他解释]
《登幽州台歌》
作者:陈子昂
前不见古人,后不见来者。
念天地之悠悠,独怆然而泪下。
【注解】:
1、幽州:古十二州之一,现今北京市。
2、悠悠:渺远的样子。
3、怆然:悲伤凄凉。
4、泪:眼泪。
【韵译】:
先代的圣君,我见也没见到,
后代的明主,要等到什么时候?
想到宇宙无限渺远,我深感人生短暂,
独自凭吊,我涕泪纵横凄恻悲愁!
=============================
《古意》
作者:李颀
男儿事长征,少小幽燕客。
赌胜马蹄下,由来轻七尺。
杀人莫敢前,须如猬毛磔。
黄云陇底白云飞,未得报恩不能归。
辽东小妇年十五,惯弹琵琶解歌舞。
今为羌笛出塞声,使我三军泪如雨。
【注解】:
1、古意:犹“拟古”。
2、轻七尺:犹轻生甘死。
3、解:擅长。
【韵译】:
好男儿远去从军戍边,
他们从小就游历幽燕。
个个爱在疆场上逞能,
为取胜不把生命依恋。
厮杀时顽敌不敢上前,
胡须象猬毛直竖满面。
陇山黄云笼罩白云纷飞,
不曾立过战功怎想回归?
有个辽东少妇妙龄十五,
一向善弹琵琶又善歌舞。
她用羌笛吹奏出塞歌曲,
吹得三军将士泪挥如雨。
=============================
等号下面接着又是另一首诗
[其他解释]
是txt的
[其他解释]
直接解析“《》”得到标题,第一个“:”后是作者,第一个“【】”中是注解,第二个“【】”中是韵译。
方法有点笨,但可以实现,主要就是根据特殊字符解析字符串。
[其他解释]
我想问下代码如何写呢
[其他解释]
该回复于2012-12-06 08:49:03被管理员删除
[其他解释]
先读取文件的内容,再用正式表达式去格式化就可以了
[其他解释]
代码有些繁琐,你可以自己再精简一下。
[其他解释]
谢谢了,差不多弄懂了