我有这个文本文件:
2013001 Jaymarion Manalo L. Service Crew March 15, 2013 8:00 12:00 13:00 17:00 10.0
2013001 Jaymarion Manalo L. Service Crew March 16, 2013 8:00 12:05 13:05 17:30 10.0
现在,我有一个条件,如果String date1 =“2013年3月15日”,它将读取它所属的行,并且只会得到“8:00”,“12:00”,“13:00” “和”17:00“并将其显示在我声明的文本字段中。我的意思是,这甚至可能吗?怎么做?我希望你能得到我想说的话。我是Java -_-
的新人答案 0 :(得分:1)
您可以使用FileUtils.readLines()
将文件读取为字符串列表
这将为您提供一个字符串列表。然后迭代列表并使用string.contains("March 15, 2013")
查找日期。
注意:我可以与您分享完整的代码,但您应该尝试使用上述信息进行编码。
答案 1 :(得分:1)
来源可能。
您需要逐行读取文件。检查每一行是否包含date1
。如果是,则提取并解析所需的数据。
以下是使用标准API实现此目的的示例代码:
public class ReadFileLineByLineAnExtractSomeText {
private static final String src = "test.txt";
private static final String date1 = "March 15, 2013";
@BeforeClass
public static void genTextFile() throws IOException {
OutputStream os = new FileOutputStream(src);
os.write(("blah bla foo bar\n" +
"2013001 Jaymarion Manalo L. Service Crew March 15, 2013 8:00 12:00 13:00 17:00 10.0\r\n" +
"2013001 Jaymarion Manalo L. Service Crew March 16, 2013 8:00 12:05 13:05 17:30 10.0\r" +
"... the end").getBytes());
os.close();
}
@Test
public void testReadAndExtract() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(src));
String line = br.readLine();
int lineNumber = 0;
while(line != null) {
lineNumber++;
int i = line.indexOf(date1);
if(i != -1) {
int s = i + date1.length();
int e = line.length();
System.out.println(date1 + " found in line " + lineNumber + " at index " + i + ", extract text from " + s + " to " + e);
String extractedText = line.substring(s, e);
String[] extractedTextParts = extractedText.trim().split("\\s+");
for(String part : extractedTextParts) {
if(isTime(part)) {
System.out.println(" '" + part + "'");
}
}
}
line = br.readLine();
}
br.close();
}
private boolean isTime(String part) {
return part == null ? false : part.matches("\\d{1,2}:\\d{1,2}");
}
}
输出
March 15, 2013 found in line 2 at index 42, extract text from 56 to 94
'8:00'
'12:00'
'13:00'
'17:00'
答案 2 :(得分:0)
根据建议您可以迭代到您想要的行,然后您可以尝试这样的事情: -
String s= FileUtils.readLines(filename).get(lineNumber);
有关FileUtils.readLines()
检查this的详细信息,请
答案 3 :(得分:0)
从行读取行...
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
//in any loop use bufferedreader.readline() untill you get your desired line
//to part your string use split()
bufferedReader.close();
}
答案 4 :(得分:0)
你可以试试这个:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileReaderService {
public static void main(String[] args) {
FileReaderService fileReaderService = new FileReaderService();
fileReaderService.performExecute("March 15, 2013");
}
public void performExecute(String inputPattern) {
List<String[]> matches = new ArrayList<String[]>();
try {
FileInputStream fileInputStream = new FileInputStream("C:/Users/Guest/Desktop/your-file.txt"); /*change your file path here*/
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
String strLine;
while ((strLine = bufferedReader.readLine()) != null) {
if (strLine.contains(inputPattern)) {
String[] splits = strLine.split("[\\s]{3,}"); /*splits matching line with 3 consecutive white spaces*/
matches.add(splits);
}
}
dataInputStream.close();
for (String[] items : matches) {
System.out.println(items[1] + "\t" + items[2] + "\t" + items[3] + "\t" + items[4]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<强>输出:强>
8:00 12:00 13:00 17:00