当前位置: 代码迷 >> Android >> 怎么截取File文件里的内容
  详细解决方案

怎么截取File文件里的内容

热度:21   发布时间:2016-05-01 12:37:09.0
如何截取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)
  相关解决方案