Java从文本文件中读取并删除字符串中的破折号

时间:2015-01-31 09:31:10

标签: java file-io

我有一个文本文件:

John Smith 2009-11-04
Jenny Doe 2009-12-29
Alice Jones 2009-01-03
Bob Candice 2009-01-04
Carol Heart 2009-01-07
Carlos Diaz 2009-01-10
Charlie Brown 2009-01-14

我正在尝试删除短划线并将它们存储为单独的类型:first,last,year,month,day,然后将其添加到sortedset / hashmap。但出于某种原因。它运作不正常。

这是我的代码:

public class Test {

    File file;
    private Scanner sc;
    //HashMap<Name, Date> hashmap = new HashMap<>();

    /**
     * @param filename
     */
    public Test(String filename) {
        file = new File(filename);
    }

    public void openFile(String filename) {
        // open the file for scanning
        System.out.println("Test file " + filename + "\n");
        try {
            sc = new Scanner(new File("birthdays.dat"));
        } 
        catch(Exception e) {
            System.out.println("Birthdays: Unable to open data file");
        }
    }

    public void readFile() {
        System.out.println("Name              Birthday");
        System.out.println("----              --------");
        System.out.println("----              --------");

        while (sc.hasNext()) {
            String line = sc.nextLine();
            String[] split = line.split("[ ]?-[ ]?");

            String first = split[0];
            String last = split[1];
            //int year = Integer.parseInt(split[2]);
            //int month = Integer.parseInt(split[3]);
            //int day = Integer.parseInt(split[4]);

                    Resource name = new Name(first, last);
                    System.out.println(first + " " + last + " " + split[2]  );
            //hashmap.add(name);

        }

    }

    public void closeFile() {
        sc.close();
    }

    public static void main(String[] args) throws FileNotFoundException,
            ArrayIndexOutOfBoundsException {
        try {
            Scanner sc = new Scanner( new File(args[0]) );

                for( int i = 0; i < args.length; i++ ) {
                    //System.out.println( args[i] );

                    if( args.length == 0 ) {

                    }
                    else if( args.length >= 1 ) {

                    }
                //  System.out.printf( "Name %-20s Birthday", name.toString(), date.toString() );
                }

        } catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("Usage: Birthdays dataFile");
            // Terminate the program here somehow, or see below.
            System.exit(-1);
        } catch (FileNotFoundException e) {
            System.err.println("Birthdays: Unable to open data file");
            // Terminate the program here somehow, or see below.
            System.exit(-1);
        }   

        Test r = new Test(args[0]);
        r.openFile(args[0]);
        r.readFile();
        r.closeFile();
    }
}

5 个答案:

答案 0 :(得分:1)

你在破折号上分裂,但你的程序是围绕使用空格的分割构建的。

尝试只拆分空格

String[] split = line.split("\\s");

所以"John Smith 2009-11-04".split("[ ]?-[ ]?");会产生["John Smith 2009", "11", "04"]当你想要的是它在空格["John", "Smith", "2009-11-04"]上分割时

答案 1 :(得分:1)

我会这样做,首先创建一个域对象:

public class Person {
    private String firstName;
    private String lastName;
    private LocalDate date;

    //getters & setters

    //equals & hashCode

    //toString
}

现在创建一个方法,将您所拥有的格式的String解析为Person

//instance variable
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

public Person parsePerson(final String input) {        
    final String[] data = input.split("\\s+");
    final Person person = new Person();
    person.setFirstName(data[0]);
    person.setLastName(data[1]);
    person.setDate(LocalDate.parse(data[2], dateTimeFormatter));
    return person;
}

请注意,DateTimeFormatter是一个实例变量,这是为了速度。如果需要解析不在当前语言环境中的日期,还应在格式化程序上设置ZoneInfo

现在,您可以非常轻松地将文件读入List<Person>

public List<Person> readFromFile(final Path path) throws IOException {
    try (final Stream<String> lines = Files.lines(path)) {
        return lines
                .map(this::parsePerson)
                .collect(toList());
    }
}

现在你有一个List<Person>,你可以根据需要对它们进行排序或处理。

您可以在创建List

时执行此操作
public List<Person> readFromFile(final Path path) throws IOException {
    try (final Stream<String> lines = Files.lines(path)) {
        return lines
                .map(this::parsePerson)
                .sorted(comparing(Person::getLastName).thenComparing(Person::getFirstName))
                .collect(toList());
    }
}

或者拥有Person implements Comparable<Person>,只需使用自然顺序。

TL; DR:对您的对象使用Objects,生活变得更加简单。

答案 2 :(得分:0)

我会使用正则表达式:

private static Pattern LINE_PATTERN
        = Pattern.compile("(.+) (.+) ([0-9]{4})-([0-9]{2})-([0-9]{2})");

...

    while (sc.hasNext()) {
        String line = sc.nextLine();
        Matcher matcher = LINE_PATTERN.matcher(line);
        if (!matcher.matches()) {
            // malformed line
        } else {
            String first = matcher.group(1);
            String last = matcher.group(2);
            int year = Integer.parseInt(matcher.group(3));
            int month = Integer.parseInt(matcher.group(4));
            int day = Integer.parseInt(matcher.group(5));
            // do something with it
        }
    }

答案 3 :(得分:0)

你正在分裂空格和连字符。这种模式不存在。

    String[] split = line.split("[ ]?");

    String first = split[0];
    String last = split[1];

    line = split[2];
    //now split the date 
    String[] splitz = line.split("-");

或类似的东西可能会起作用:

     String delims = "[ -]+";
     String[] tokens = line.split(delims);

答案 4 :(得分:0)

如果我理解你的问题,那么这就是答案。看看吧。

  List<String> listGet = new ArrayList<String>();
    String getVal = "John Smith 2009-11-04";
    String[] splited = getVal.split("[\\-:\\s]");
    for(int j=0;j<splited.length;j++)
    {
        listGet.add(splited[j]);

    }
    System.out.println("first name :"+listGet.get(0));
    System.out.println("Last name :"+listGet.get(1));
    System.out.println("year is :"+listGet.get(2));
    System.out.println("month is :"+listGet.get(3));
    System.out.println("day is :"+listGet.get(4));

OP:

first name :John
Last name :Smith
year is :2009
month is :11
day is :04