0

如何将文件输入文本拆分为 2 个不同的数组?我想为名称创建一个数组,为电话号码创建一个数组。我设法进行文件输入,但我尝试了一切,似乎无法拆分名称和数字,然后将其放入 2 个不同的数组中。我是菜鸟请帮忙

这是 phonebook.txt 文件的样子

Bin Arry,1110001111
Alex Cadel,8943257000
Poh Caimon,3247129843
Diego Amezquita,1001010000
Tai Mai Shu,7776665555
Yo Madow,1110002233
Caup Sul,5252521551
This Guy,7776663333
Me And I,0009991221
Justin Thyme,1113332222
Hey Oh,3939399339
Free Man,4533819911
彼得·派珀,6480013966
威廉·穆洛克,9059671045

下面是我的代码

   import java.io.*;
   import java.util.Scanner;
   import java.io.FileInputStream;
   import java.io.FileNotFoundException;
   import java.io.IOException;
   import java.io.InputStreamReader;
   import java.io.BufferedReader;
   public class demos {
   public static void main(String[]  args){

    FileInputStream Phonebook;  
    DataInputStream In;
    int i = 0;

     String fileInput;  
      try  
      { 

            Phonebook = new FileInputStream("phonebook.txt"); 

            FileReader fr = new FileReader("phonebook.txt");
            BufferedReader br = new BufferedReader(fr);
            String buffer;
            String fulltext="";
            while ((buffer = br.readLine()) != null) {

                fulltext += buffer;

               // System.out.println(buffer);

                String names = buffer;
                char [] Y ; 
                Y = names.toCharArray();
                System.out.println(Y);

      }}
         catch (FileNotFoundException e)  
            { 
            System.out.println("Error - this file does not exist"); 
         }  
        catch (IOException e)   
        { 
            System.out.println("error=" + e.toString() ); 

         }
4

2 回答 2

1

对于一个完整的功能(而不是命令式)解决方案,我向您推荐这个:

public static void main(String[] args) throws IOException {
    Object[] names = Files.lines(new File("phonebook.txt").toPath()).map(l -> l.split(",")[0]).toArray();
    Object[] numbers = Files.lines(new File("phonebook.txt").toPath()).map(l -> l.split(",")[1]).toArray();


    System.out.println("names in the file are : ");
    Arrays.stream(names).forEach(System.out::println);
    System.out.println("numbers in the file are : ");
    Arrays.stream(numbers).forEach(System.out::println);
}

输出 names in the file are : Bin Arry Alex Cadel Poh Caimon Diego Amezquita Tai Mai Shu Yo Madow Caup Sul This Guy Me And I Justin Thyme Hey Oh Free Man Peter Piper William Mulock numbers in the file are : 1110001111 8943257000 3247129843 1001010000 7776665555 1110002233 5252521551 7776663333 0009991221 1113332222 3939399339 4533819911 6480013966 9059671045

如您所见,函数式编程简短而智能……。当你习惯时很容易

于 2020-01-16T15:29:59.377 回答
1

如果您使用的是 Java 8,则可以简化它:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;

public class Test {
    static ArrayList<String> names = new ArrayList<String>();
    static ArrayList<String> numbers = new ArrayList<String>();

    /**
     * For each line, split it on the comma and send to splitNameAndNum()
     */
    public static void main(String[] args) throws IOException {
        Files.lines(new File("L:\\phonebook.txt").toPath())
        .forEach(l -> splitNameAndNum(l.split(",")));
    }

    /**
     * Accept an array of length 2 and put in the proper ArrayList
     */
    public static void splitNameAndNum(String[] arr) {
        names.add(arr[0]);
        numbers.add(arr[1]);
    }
}

在 Java 7 中:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.ArrayList;

public class Test {
    static ArrayList<String> names = new ArrayList<String>();
    static ArrayList<String> numbers = new ArrayList<String>();

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("L:\\phonebook.txt")));

        String line;

        while ((line = br.readLine()) != null) {
            splitNameAndNum(line.split(","));
        }
    }

    /**
     * Accept an array of length 2 and put in the proper ArrayList
     */
    public static void splitNameAndNum(String[] arr) {
        names.add(arr[0]);
        numbers.add(arr[1]);
    }
}
于 2020-01-15T19:43:03.883 回答