2

我有这个文本文件:

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 很陌生-_-

4

5 回答 5

1

您可以使用这将文件作为字符串列表读取,FileUtils.readLines() 这将为您提供字符串列表。然后遍历列表并使用查找日期string.contains("March 15, 2013")

注意:我可以与您分享完整的代码,但您应该尝试使用上述信息进行编码。

于 2013-03-16T11:54:28.970 回答
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'
于 2013-03-16T12:22:28.523 回答
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();         
}
于 2013-03-16T11:55:25.447 回答
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
于 2013-03-16T12:21:39.227 回答
0

正如建议的那样,您可以迭代到您想要的行,然后您可以尝试这样的事情:-

String s= FileUtils.readLines(filename).get(lineNumber);

有关FileUtils.readLines()查看内容的更多信息。

于 2013-03-16T11:49:40.827 回答