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!