我有一个像这样的文件:
1
00:00:00,535 --> 00:00:02,462
Je suis professeur d'informatique et
d'ingéniérie informatique
2
00:00:02,462 --> 00:00:04,389
à Carnegie Mellon,
3
00:00:04,389 --> 00:00:06,318
et mes recherches se concentrent sur
l'utilisabilité de la vie privée
文件太长,大约有600个字幕,每个字幕都有其“ ID”(例如1、2、3 ...),其“时间码”(例如00:00:02,462-> 00:00: 04,389 ...),其“ subtitle_line1”(例如“ Je suis professeur d'informatique等” ...)和subtitle_line2(例如“ d'ingéniérieinformatique” ...),但不是全部;有些字幕具有只是“ subtitle_line1”(例如ID = 2的字幕)。
我需要阅读并将其存储在数据库中。为此,我创建了一个数据库,其模式如下:
字幕(ID,时间码,subtitle_line1,subtitle_line2)
然后我像这样创建一个Java bean Subtitle.java:
public class Subtitle {
private String subtitleId;
private String timeCode;
private String originalSubtitleLine1;
private String originalSubtitleLine2;
public String getSubtitleId() {
return subtitleId;
}
public void setSubtitleId(String subtitleId) {
this.subtitleId = subtitleId;
}
public String getTimeCode() {
return timeCode;
}
public void setTimeCode(String timeCode) {
this.timeCode = timeCode;
}
public String getOriginalSubtitleLine1() {
return originalSubtitleLine1;
}
public void setOriginalSubtitleLine1(String originalSubtitleLine1) {
this.originalSubtitleLine1 = originalSubtitleLine1;
}
public String getOriginalSubtitleLine2() {
return originalSubtitleLine2;
}
public void setOriginalSubtitleLine2(String originalSubtitleLine2) {
this.originalSubtitleLine2 = originalSubtitleLine2;
}
}
我也做了一个SubtitleHandler.java类来读取文件并提取信息,例如:
public class SubtitlesHandler {
private ArrayList<String> originalSubtitles = null;
private ArrayList<String> translatedSubtitles = null;
public SubtitlesHandler(String fileName) {
originalSubtitles = new ArrayList<String>();
translatedSubtitles = new ArrayList<String>();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
originalSubtitles.add(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public ArrayList<String> getSubtitles() {
return originalSubtitles;
}
}
如何从文件中提取数据以构建字幕对象,然后将其存储在数据库中?感谢您的建议。
答案 0 :(得分:0)
Scanner scanner=new Scanner(new File("yourfile.txt"));
Scanner grpScanner=null;
String id="";
String title="";
String subtitleLine1="";
String subtitleLine2="";
//split into subtitle group using empty line
scanner.useDelimiter(Pattern.compile("^\\s*$", Pattern.MULTILINE));
//iterate subtitle group
while(scanner.hasNext())
{
//store subtitle group as string
String subgrp=scanner.next();
//read each line in subtitle group
grpScanner=new Scanner(subgrp);
while(grpScanner.hasNext())
{
id=grpScanner.next(); //get Id
title=grpScanner.next(); // get timecode
subtitleLine1=grpScanner.next(); //get Subtitle_line1
subtitleLine2=grpScanner.hasNext() ? grpScanner.next(): ""; //check for subtitle_line 2
}
grpScanner.close(); //close grpScanner
}