File.separator用法

File.separator
在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。
比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
File file1 = new File (“C:\tmp\test.txt”);
在Linux下则是这样的:
File file2 = new File ("/tmp/test.txt");
如果要考虑跨平台,则最好是这么写:
File myFile = new File(“C:” + File.separator + “tmp” + File.separator, “test.txt”);
File类有几个类似separator的静态字段,都是与系统相关的,在编程时应尽量使用。

需求
一行是一条交易明细每行分6列列间用|分隔。#为注释符号。类TransRecord存储一条明细 解析文件数据至List。
交易明细文件内容如下例


#客户号 姓名 所述机构号 性别 帐号 发生时间 发生额
000001|刘德华|0000|1|4155990188888888|20060720200005|300.00
000201|晓龙|0002|1|4155990199999999|20060720200005|500.00
000101|黄晓明|0012|1|4155990100000000|20060720200005|1000.50
000101|张东健|0012|1|4155990155555555|20060720200005|600.99
000301|梁朝伟|0013|0|4155990111111111|20060722201005|5000.00
000001|刘德华|0000|1|4155990188888888|20060725200005|200.00

首先先将如下内容写入一个文件中:
---------------------------------------------------------------------------
#客户号 姓名 所述机构号 性别 帐号 发生时间 发生额
000001|刘德华|0000|1|4155990188888888|20060720200005|300.00
000201|晓龙|0002|1|4155990199999999|20060720200005|500.00
000101|黄晓明|0012|1|4155990100000000|20060720200005|1000.50
000101|张东健|0012|1|4155990155555555|20060720200005|600.99
000301|梁朝伟|0013|0|4155990111111111|20060722201005|5000.00
000001|刘德华|0000|1|4155990188888888|20060725200005|200.00

public class Demo03 {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src" + File.separator + "transrecord.dat")))) {
            br.readLine();
            br.readLine();
            List<TransRecord> transRecords = new ArrayList<>();
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] transRecordFields = line.split("\\|");
                transRecords.add(new TransRecord(transRecordFields[0], transRecordFields[1], transRecordFields[2], Integer.parseInt(transRecordFields[3]), transRecordFields[4], transRecordFields[5], Double.parseDouble(transRecordFields[6])));
            }
            System.out.println(transRecords);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class TransRecord {
    private String accountNo;
    private String name;
    private String orgNo;
    private int gender;
    private String no;
    private String happenDate;
    private double happenMoney;

    public TransRecord() {
    }

    public TransRecord(String accountNo, String name, String orgNo, int gender, String no, String happenDate, double happenMoney) {
        this.accountNo = accountNo;
        this.name = name;
        this.orgNo = orgNo;
        this.gender = gender;
        this.no = no;
        this.happenDate = happenDate;
        this.happenMoney = happenMoney;
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOrgNo() {
        return orgNo;
    }

    public void setOrgNo(String orgNo) {
        this.orgNo = orgNo;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getHappenDate() {
        return happenDate;
    }

    public void setHappenDate(String happenDate) {
        this.happenDate = happenDate;
    }

    public double getHappenMoney() {
        return happenMoney;
    }

    public void setHappenMoney(double happenMoney) {
        this.happenMoney = happenMoney;
    }

    @Override
    public String toString() {
        return "TransRecord{" +
                "accountNo='" + accountNo + '\'' +
                ", name='" + name + '\'' +
                ", orgNo='" + orgNo + '\'' +
                ", gender=" + gender +
                ", no='" + no + '\'' +
                ", happenDate='" + happenDate + '\'' +
                ", happenMoney=" + happenMoney +
                '}';
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值