java调用vbs文件,使用java读取.vbs文件

该博客讨论了在使用Java Swing开发工具时遇到的一个问题:在读取.vbs文件时,由于不同系统的字符集差异导致在某些系统上无法正常打开。博主提出了一种解决方案,即尝试使用多种编码方式打开文件,如果失败则提示用户选择合适的编码。建议的方法包括尝试常见的UTF-8和UTF-16编码,并在成功后记录下来以便后续使用。此外,还提到了UTF-8的字节顺序标记(BOM)用于检测文件编码的可能性。
摘要由CSDN通过智能技术生成

I am developing a tool using java swing. In it I have to read a .vbs file. I have used the following code for it.Scanner fileReader1 = new Scanner(new File(fileName),"US-ASCII");

while (fileReader1.hasNext())

{

System.out.println(fileReader1.nextLine());

}

where fileName is valid .vbs file path

This is working only for .vbs files in my PC. For other systems, .vbs files are not opening. I think the problem is due the change in charset. For me it is "US -ASCII" and for others it may be "UTF 8" or "UTF 16". So how can I resolve this problem? My purpose is to open .vbs file using my tool for any systems. What I have to do for this? which charset will work for all systems? Please help me.

解决方案

One solution would be to try to open the file, and if it fails you try to open the file using another encoding. It is then very handy to store the encoding that worked so each next attempt starts using that same encoding. After some tries you could also let the user choose the encoding that might work best.

The problem here is that most text files (or encodings) don''t store an encoding identifier. UTF-8 however has an identifier to indicate the byte order but to detect it you need to read the file as binary file. When reading a file as UTF-8 this is automatically used to determine little or big endian byte order. So there are some possibilities to detect the right encoding but you can also just try or let the user choose (or a mix of these methods).

More info on UTF-8:

http://en.wikipedia.org/wiki/UTF-8[^]

Good luck!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值