Java read file and store text in an array

前端 未结 7 1823
心在旅途
心在旅途 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(", ");
    
    0 讨论(0)
  • 2020-11-30 06:49

    I have found this way of reading strings from files to work best for me

    String st, full;
    full="";
    BufferedReader br = new BufferedReader(new FileReader(URL));
    while ((st=br.readLine())!=null) {
        full+=st;
    }
    

    "full" will be the completed combination of all of the lines. If you want to add a line break between the lines of text you would do full+=st+"\n";

    0 讨论(0)
  • 2020-11-30 06:51

    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);
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-30 06:52
    int count = -1;
    String[] content = new String[200];
    while(inFile1.hasNext()){
    
        content[++count] = inFile1.nextLine();
    }
    

    EDIT

    Looks like you want to create a float array, for that create a float array

    int count = -1;
    Float[] content = new Float[200];
    while(inFile1.hasNext()){
    
        content[++count] = Float.parseFloat(inFile1.nextLine());
    }
    

    then your float array would look like

    content[0] = 70.3
    content[1] = 70.8
    content[2] = 73.8
    content[3] = 77.0 and so on
    
    0 讨论(0)
  • 2020-11-30 06:55

    If you don't know the number of lines in your file, you don't have a size with which to init an array. In this case, it makes more sense to use a List :

    List<String> tokens = new ArrayList<String>();
    while (inFile1.hasNext()) {
        tokens.add(inFile1.nextLine());
    }
    

    After that, if you need to, you can copy to an array :

    String[] tokenArray = tokens.toArray(new String[0]);
    
    0 讨论(0)
  • 2020-11-30 06:56

    I use this method:

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    
    public class TEST {
        static Scanner scn;
    
    public static void main(String[] args) {
        String text = "";
    
        try{
            scn = new Scanner(new File("test.txt"));
        }catch(FileNotFoundException ex){System.out.println(ex.getMessage());}
        while(scn.hasNext()){
            text += scn.next();
            }
            String[] arry = text.split(",");
    
        //if need converting to float do this:
        Float[] arrdy = new Float[arry.length];
        for(int i = 0; i < arry.length; i++){
                arrdy[i] = Float.parseFloat(arry[i]);
            }
        System.out.println(Arrays.toString(arrdy));
                }
    }
    
    0 讨论(0)
提交回复
热议问题