![](/img/trans.png)
[英]How to replace specific keys in text file with values in JSON file…not replacing
[英]Replacing the values of columns in text file
我有一个具有不同列的文件,每列的值都是字符串标签
col1 col2 col3 ----- ----- ----- A WM S2 B JK S3 C ZO S2
如果我想将每个值的值替换为数字,我将使用以下格式的表
col1 col2 col3 ----- ----- ----- 0 3 6 1 4 7 2 5 6
如何用Java编写?
您可以使用java.util.Scanner
,因为.next()
将平等对待数字和文本
final Scanner scanner = new Scanner(new File("path_to_file"));
final List<String[]> list = new ArrayList<String[]>();
try {
// skip 2 first lines
if (scanner.hasNextLine())
scanner.nextLine();
if (scanner.hasNextLine())
scanner.nextLine();
while (scanner.hasNextLine()) {
final String[] line = new String[3];
for (int i = 0; i < 3 && scanner.hasNext(); i++) {
line[i] = scanner.next();
}
list.add(line);
scanner.nextLine();
}
} finally {
scanner.close();
}
// the file content is in "list" variable now, so you can modify and save back to file
如您所不知道的行数
你做以下
-读取所有行(这将为您提供行数)rowsNo。
-您重写了表格。
for (int row=0; row<rowsNo; row++) {
String rowStr = "";
for (int col=0; col<colsNo; col++)
rowStr += row+col*colsNo;
System.out.println(rowStr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.