Java的读取文件并存储在文本数组 [英] Java read file and store text in an array

查看:259
本文介绍了Java的读取文件并存储在文本数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何用扫描器读取与Java文件和文件IOException异常,但我不知道的唯一的事情是如何存储在文件作为一个数组中的文本。这里是我的code的一个片段:

 公共静态无效的主要(字串[] args)抛出IOException
    // TODO这里code应用程序逻辑    // //读取KeyWestTemp.txt    //创建TOKEN1
    串TOKEN1 =;    // for-each循环计算五月热指数月 - 10月
    //创建扫描仪inFile1
    扫描仪inFile1 =新的扫描仪(新的文件(KeyWestTemp.txt));    // while循环
    而(inFile1.hasNext()){        //我怎么可以创建文本数组中读取?        //找到下一行
        TOKEN1 = inFile1.nextLine();

下面是我的KeyWestTemp.txt文件包含:

  70.3,70.8,73.8,77.0,80.7,83.4,84.5,84.4,83.4,80.2,76.3,72.0


解决方案

作为字符串存储:

 公共类ReadTemps {    公共静态无效的主要(字串[] args)抛出IOException
    // TODO这里code应用程序逻辑    // //读取KeyWestTemp.txt    //创建TOKEN1
    串TOKEN1 =;    // for-each循环计算五月热指数月 - 10月    //创建扫描仪inFile1
    扫描仪inFile1 =新的扫描仪(新的文件(KeyWestTemp.txt))useDelimiter(\\\\ S *);    //原来的答复使用LinkedList的,但可能是preferable使用在大多数情况下的ArrayList
    //列表<串GT;临时工=新的LinkedList<串GT;();
    清单<串GT;临时工=新的ArrayList<串GT;();    // while循环
    而(inFile1.hasNext()){
      //找到下一行
      TOKEN1 = inFile1.next();
      temps.add(TOKEN1);
    }
    inFile1.close();    的String [] = tempsArray temps.toArray(新的String [0]);    对于(一个String:tempsArray){
      的System.out.println(多个);
    }
  }
}

有关花车:

 进口的java.io.File;
进口java.io.IOException异常;
进口java.util.LinkedList中;
进口的java.util.List;
进口java.util.Scanner中;公共类ReadTemps {  公共静态无效的主要(字串[] args)抛出IOException
    // TODO这里code应用程序逻辑    // //读取KeyWestTemp.txt    //创建TOKEN1    // for-each循环计算五月热指数月 - 10月    //创建扫描仪inFile1
    扫描仪inFile1 =新的扫描仪(新的文件(KeyWestTemp.txt))useDelimiter(\\\\ S *);
    //原来的答复使用LinkedList的,但可能是preferable使用在大多数情况下的ArrayList
    //列表<浮球GT;临时工=新的LinkedList<浮球GT;();
    清单<浮球GT;临时工=新的ArrayList<浮球GT;();    // while循环
    而(inFile1.hasNext()){
      //找到下一行
      浮TOKEN1 = inFile1.nextFloat();
      temps.add(TOKEN1);
    }
    inFile1.close();    浮法[] = tempsArray temps.toArray(新的浮动[0]);    为(浮动小号:tempsArray){
      的System.out.println(多个);
    }
  }
}

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 an array. Here is a snippet of my code:

 public static void main(String[] args) throws IOException{
    // TODO code 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"));

    // while loop
    while(inFile1.hasNext()){

        // how can I create array from text read?

        // find next line
        token1 = inFile1.nextLine();

Here is what my KeyWestTemp.txt file contains:

70.3,   70.8,   73.8,   77.0,   80.7,   83.4,   84.5,   84.4,   83.4,   80.2,   76.3,   72.0   

解决方案

Stored as strings:

public class ReadTemps {

    public static void main(String[] args) throws IOException {
    // TODO code 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);
    }
  }
}

For floats:

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 code 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);
    }
  }
}

这篇关于Java的读取文件并存储在文本数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆