如图,将txt文本文本里的张三,李四,王二和麻子转换为如
String[] i = { "张三", "李四", "王二", "麻子"};
这样的数组

解决方案 »

  1.   

    我记得有个逐行读取readline(),然后去空格,存入list,再转换数组。
      

  2.   

    public class Test3 { public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new FileReader(
    "C:\\Users\\Administrator\\Desktop\\1.txt"));
    String line = null;
    List<String> list = new ArrayList<String>();
    while ((line = in.readLine()) != null) {
    String temp = line.trim();
    if (temp != null && !"".equals(temp))
    list.add(temp); } String[] arr = (String[]) list.toArray(new String[list.size()]);
    for (String s : arr)
    System.out.println("===" + s); }
    }
      

  3.   


    public static void main(String[] args) throws IOException {
    String[] names = (String[])Files.lines(Paths.get("path to file"))
                  .collect(Collectors.toList()).toArray();
    }
      

  4.   

    用BufferedReader的readLine方法
      

  5.   

    Stream本身就有toArray方法:
    String[] array = Files.lines(Paths.get("D:/1.txt")).toArray(String[]::new);