8

我正在制作一个可以在本地服务器上运行的 java 程序。

服务器使用 PHP 从客户端接收请求。

   <?php

    $file = fopen('temp.txt', 'a+');
    $a=explode(':',$_GET['content']);
    fwrite($file,$a[0].':'.$a[1]. '\n');

    fclose($file); 
    ?>

现在我在本地服务器上有文件“temp.txt”。

Java程序应该逐行打开读取的文件,每个喜欢应该被划分/分解“:”(在一行中只有一个':')

我已经尝试了很多方法,但不能完全像 PHP 分割线的方式。

是否可以在 JAVA 中使用相同/相似的爆炸功能。

4

2 回答 2

14

是的,在 Java 中,您可以使用String#split(String regex)方法来拆分String对象的值。

更新:例如:

String arr = "name:password";
String[] split = arr.split(":");
System.out.println("Name = " + split[0]);
System.out.println("Password = " + split[1]);
于 2013-06-07T11:21:21.980 回答
4

您可以在 Java 中使用String.split以“:”“分解”每一行。

编辑

单行示例:

String line = "one:two:three";
String[] words = line.split(":");
for (String word: words) {
    System.out.println(word);
}

输出:

one
two
three
于 2013-06-07T11:21:13.230 回答