而不是必须通过700-800行文本,每行是一些变化:
-5,-8,0:2.0
Run Code Online (Sandbox Code Playgroud)
我有一个方法,我必须传递每一行,这是anotherclass.setBlock(xCoord, yCoord, zCoord, id).所以对于上面的例子,它将是:
anotherclass.setBlock(x-5, y-8, 0, 2);
Run Code Online (Sandbox Code Playgroud)
有没有办法解析每一行文本并执行此操作?我一直在寻找年龄,这要么是因为我找不到正确的方法来说这个或者我根本找不到答案
我已经尝试过手动操作,但在100行后,它开始感觉效率非常低.我不能真正使用for循环,因为坐标不是连续的(或者更确切地说,它们是,但仅限于1或2行).
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.util.regex.Pattern.compile;
...
public static void main(String[] args) {
BufferedReader r = null;
try {
r = new BufferedReader(new InputStreamReader(
new FileInputStream("myfile.txt"), "UTF-8"));
final Pattern p = compile("(.+?),(.+?),(.+?):(.+)");
String line;
while ((line = r.readLine()) != null) {
final Matcher m = p.matcher(line);
if (!m.matches())
throw new RuntimeException("Line in invalid format: " + line);
anotherclass.setBlock(parseInt(m.group(1)), parseInt(m.group(2)),
parseInt(m.group(3)), parseDouble(m.group(4)));
}
}
catch (IOException e) { throw new RuntimeException(e); }
finally { try { if (r != null) r.close(); } catch (IOEXception e) {} }
}
Run Code Online (Sandbox Code Playgroud)