// 导入 countryNo string[] buf = File.ReadAllLines(textBox_CountryNo.Text); string[] buf2 = File.ReadAllLines(textBox_CityNo.Text); foreach (string line in buf) { string[] ss = line.Split('\t'); if (ss.Length < 2) continue; try { byte index = byte.Parse(ss[0], System.Globalization.NumberStyles.AllowHexSpecifier); if (!myCountryNoDict.ContainsKey(index)) { myCountryNoDict.Add(index, ss[1]); } } catch (Exception ee) { } } for (int ii = 0; ii < buf2.Length; ++ii) { myCityNoDict.Add(ii, buf2[ii]); } 转java
时间: 2024-02-10 15:27:04 浏览: 243
在Java中,你可以将上述代码转换为以下形式:
```java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class CountryCity {
public static void main(String[] args) {
String countryNoFile = "path_to_countryNo_file.txt";
String cityNoFile = "path_to_cityNo_file.txt";
Map<Byte, String> myCountryNoDict = new HashMap<>();
Map<Integer, String> myCityNoDict = new HashMap<>();
try {
String[] buf = Files.readAllLines(Paths.get(countryNoFile)).toArray(new String[0]);
String[] buf2 = Files.readAllLines(Paths.get(cityNoFile)).toArray(new String[0]);
for (String line : buf) {
String[] ss = line.split("\t");
if (ss.length < 2) continue;
try {
byte index = Byte.parseByte(ss[0], 16);
if (!myCountryNoDict.containsKey(index)) {
myCountryNoDict.put(index, ss[1]);
}
} catch (NumberFormatException ignored) {
}
}
for (int ii = 0; ii < buf2.length; ++ii) {
myCityNoDict.put(ii, buf2[ii]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请确保将 `path_to_countryNo_file.txt` 和 `path_to_cityNo_file.txt` 替换为实际的文件路径。这段代码会读取两个文件,将数据存储在 `myCountryNoDict` 和 `myCityNoDict` 中。
阅读全文