Java read file and store text in an array

前端 未结 7 1856
心在旅途
心在旅途 2020-11-30 06:25

I know how to read a file with Java using Scanner and File IOException, but the only thing I don\'t know is how to store the text in the files as a

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 06:49

    Just read the whole file into a StringBuilder, then split the String by dot following a space. You will get a String array.

    Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt"));
    
    StringBuilder sb = new Stringbuilder();
    while(inFile1.hasNext()) {
        sb.append(inFile1.nextLine());
    }
    
    String[] yourArray = sb.toString().split(", ");
    

提交回复
热议问题