博图 路径字符串不正确,有没有一个Java实用程序将转换字符串路径使用正确的文件分隔符字符?...

I have developed a number of classes which manipulate files in Java. I am working on a Linux box, and have been blissfully typing new File("path/to/some/file");. When it came time to commit I realised some of the other developers on the project are using Windows. I would now like to call a method which can take in a String of the form "/path/to/some/file" and, depending on the OS, return a correctly separated path.

For example:

"path/to/some/file" becomes "path\\to\\some\\file" on Windows.

On Linux it just returns the given String.

I realise it wouldn't take long to knock up a regular expression that could do this, but I'm not looking to reinvent the wheel, and would prefer a properly tested solution. It would be nice if it was built in to the JDK, but if it's part of some small F/OSS library that's fine too.

So is there a Java utility which will convert a String path to use the correct File separator char?

解决方案

Apache Commons comes to the rescue (again). The Commons IO method FileNameUtils.separatorsToSystem() will do what you want.

Needless to say, Apache Commons IO will do a lot more besides and is worth looking at.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值