假设有文件:a.txt
内容为:
name,age,sex
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class $ {
public static void main(String[] args) {
Scanner in = null;
try {
in = new Scanner(new File("D:/a.txt"));
String result = "";
while (in.hasNextLine()) {
result += in.nextLine();
}
String[] arr = result.split(",");
System.out.println(Arrays.deepToString(arr));
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
}
}
}
结果:
[name, age, sex]