Java读取文件并将文本存储在数组中

Java读取文件并将文本存储在数组中,第1张

Java读取文件并将文本存储在数组中

存储为字符串

public class ReadTemps {    public static void main(String[] args) throws IOException {    // TODO pre application logic here    // // read KeyWestTemp.txt    // create token1    String token1 = "";    // for-each loop for calculating heat index of May - October    // create Scanner inFile1    Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\s*");    // Original answer used linkedList, but probably preferable to use ArrayList in most cases    // List<String> temps = new linkedList<String>();    List<String> temps = new ArrayList<String>();    // while loop    while (inFile1.hasNext()) {      // find next line      token1 = inFile1.next();      temps.add(token1);    }    inFile1.close();    String[] tempsArray = temps.toArray(new String[0]);    for (String s : tempsArray) {      System.out.println(s);    }  }}

对于花车

import java.io.File;import java.io.IOException;import java.util.linkedList;import java.util.List;import java.util.Scanner;public class ReadTemps {  public static void main(String[] args) throws IOException {    // TODO pre application logic here    // // read KeyWestTemp.txt    // create token1    // for-each loop for calculating heat index of May - October    // create Scanner inFile1    Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\s*");    // Original answer used linkedList, but probably preferable to use ArrayList in most cases    // List<Float> temps = new linkedList<Float>();    List<Float> temps = new ArrayList<Float>();    // while loop    while (inFile1.hasNext()) {      // find next line      float token1 = inFile1.nextFloat();      temps.add(token1);    }    inFile1.close();    Float[] tempsArray = temps.toArray(new Float[0]);    for (Float s : tempsArray) {      System.out.println(s);    }  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5478546.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存