用java/android解析文件
我有一个文本文件,如下所示用java/android解析文件,java,Java,我有一个文本文件,如下所示 Title - Welcome to the Dibb Date - 13/03/11 Information - Hello and welcome to our website. Title - Welcome to student room Date - 06/05/11 Information - Hello and welcome to the student room. We are a online forum that allows previous
Title - Welcome to the Dibb
Date - 13/03/11
Information - Hello and welcome to our website.
Title - Welcome to student room
Date - 06/05/11
Information - Hello and welcome to the student room. We are a online forum that allows previous and current students to ask questions.
我需要解析这个文本文件并保存标题行、日期行等内容,其余内容将保存为信息。我知道如何读取文件并将完整文件保存为字符串,但我一直无法获取选择信息
代码
这是我用来读取文本文件的代码
helloTxt.setText(readTxt());
}
private String readTxt() {
InputStream inputStream = getResources().openRawResource(R.raw.pages);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = byteArrayOutputStream.toString();
return str;
}
逐行读取文件
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
br.close();
如果您可以保证每一行最多有一个-
,那么您可以使用以下模式
String[] tokens = line.split("\s-\s");
这条线
标题-欢迎来到Dibb
它会给你
tokens[0] = "Title";
tokens[1] = "Welcome to the Dibb";
我试着写一些课程来帮助你解决你的问题
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Test4 {
private List<Information> parser(String data) {
List<Information> informations = new ArrayList<Information>();
String blocks[] = data.split("\n\r");
for(String block : blocks) {
String[] lines = block.split("\n");
Information information = new Information();
information.setTitle((lines[0].split("-"))[1].trim());
information.setDate((lines[1].split("-"))[1].trim());
information.setInfo((lines[2].split("-"))[1].trim());
informations.add(information);
}
return informations;
}
private void runner() throws IOException {
InputStream inputStream = getClass().getResourceAsStream("input.txt");
String input = "";
int cc;
while((cc = inputStream.read()) != -1) {
input += (char) cc;
}
List<Information> informations = parser(input);
for(Information information : informations) {
System.out.println(information);
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Test4 test4 = new Test4();
test4.runner();
}
class Information {
private String title;
private String date;
private String info;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Information [" + (title != null ? "title=" + title + ", " : "")
+ (date != null ? "date=" + date + ", " : "") + (info != null ? "info=" + info : "") + "]";
}
}
}
import java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.List;
公共类Test4{
私有列表解析器(字符串数据){
列表信息=新的ArrayList();
字符串块[]=数据。拆分(“\n\r”);
用于(字符串块:块){
String[]line=block.split(“\n”);
信息=新信息();
information.setTitle((行[0]。拆分(“-”)[1]。trim());
information.setDate((第[1]行).拆分(“-”)[1].trim());
information.setInfo((第[2]行).split(“-”)[1].trim());
信息。添加(信息);
}
返回信息;
}
私有void runner()引发IOException{
InputStream InputStream=getClass().getResourceAsStream(“input.txt”);
字符串输入=”;
int cc;
而((cc=inputStream.read())!=-1){
输入+=(字符)cc;
}
列表信息=解析器(输入);
用于(信息:信息){
系统输出打印项次(信息);
}
}
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException{
Test4 Test4=新的Test4();
test4.runner();
}
班级信息{
私有字符串标题;
私有字符串日期;
私有字符串信息;
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getDate(){
返回日期;
}
公共无效设置日期(字符串日期){
this.date=日期;
}
公共字符串getInfo(){
退货信息;
}
公共void setInfo(字符串信息){
this.info=info;
}
@凌驾
公共字符串toString(){
返回“信息[”+(title!=null?“title=“+title+”,“:”)
+(date!=null?“date=“+date+”,“:”)+(info!=null?“info=“+info:”)+“];
}
}
}
看起来您可以在第一次-之后拆分。这方面有什么问题吗?谢谢你的建议,但我需要完整的一行,这样我就不能在-“但我需要完整的一行,这样我就不能在-”如果你需要完整的一行,你将如何提取字段?@SLateffa请看我下面的答案-断断续续的解决方案。如果标题或日期包含-
,则这将完全中断。@MathiasSchwarz,到目前为止它没有。关于行处理:是否最好在第一个'-'之前读取,将字符串和rtrim/ltrim拆分为两部分?谢谢,我没有使用这个精确的代码,但它帮助了我很多,我现在已经做了一半我需要做的事情:Dhanks,我没有使用这个精确的代码,但它帮助了很多,我现在已经做了一半我需要做的事情:DNo matter。如果你感到满意,就接受答案,投赞成票。谢谢你的代码还有其他问题吗?