使用Java读取.vbs文件 [英] Read .vbs files using java
问题描述
我正在使用Java swing开发工具.在其中,我必须阅读.vbs文件.我已经使用了以下代码.
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());
}
其中fileName是有效的.vbs文件路径
这仅适用于我的PC中的.vbs文件.对于其他系统,.vbs文件未打开.我认为问题是由于字符集的变化.对我来说,它是"US -ASCII",对其他人来说,它可能是"UTF 8"或"UTF 16".那么我该如何解决这个问题呢?我的目的是使用我的工具为任何系统打开.vbs文件.我该怎么办?哪种字符集适用于所有系统?请帮助我.
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.
推荐答案
一种解决方案是尝试打开文件,如果失败,则尝试使用其他编码打开文件.这样就很容易存储有效的编码,因此每次下一次尝试都将使用相同的编码.经过尝试后,您还可以让用户选择效果最好的编码.
这里的问题是大多数文本文件(或编码)不存储编码标识符.但是,UTF-8具有用于指示字节顺序的标识符,但是要检测到它,您需要将文件读取为二进制文件.当读取文件为UTF-8时,这将自动用于确定大字节序或小字节序.因此,有一些方法可以检测正确的编码,但是您也可以尝试或让用户选择(或混合使用这些方法).
有关UTF-8的更多信息:
http://en.wikipedia.org/wiki/UTF-8 [
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!
这篇关于使用Java读取.vbs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!