简体   繁体   English

如何将文件输入拆分为2个不同的数组java

[英]How to split file input into 2 different arrays java

How do I split a file input text into 2 different array?如何将文件输入文本拆分为 2 个不同的数组? I want to make n array for the names, and an array for the phone numbers.我想为姓名创建 n 个数组,并为电话号码创建一个数组。 I managed to do the file input, but ive tried everything and cant seem to split the names and the numbers, then put it into 2 different arrays.我设法进行文件输入,但我尝试了所有方法,但似乎无法拆分名称和数字,然后将其放入 2 个不同的数组中。 Im noob pls help我是菜鸟请帮忙

here is how the phonebook.txt file looks like这是 phonebook.txt 文件的样子

Bin Arry,1110001111斌阿里,1110001111
Alex Cadel,8943257000亚历克斯·卡德尔,8943257000
Poh Caimon,3247129843 Poh Caimon,3247129843
Diego Amezquita,1001010000迭戈·阿梅斯基塔,1001010000
Tai Mai Shu,7776665555泰麦舒,7776665555
Yo Madow,1110002233哟玛多,1110002233
Caup Sul,5252521551 Caup Sul,5252521551
This Guy,7776663333这家伙,7776663333
Me And I,0009991221我和我,0009991221
Justin Thyme,1113332222贾斯汀百里香,1113332222
Hey Oh,3939399339嘿哦,3939399339
Free Man,4533819911自由人,4533819911
Peter Piper,6480013966彼得·派博,6480013966
William Mulock,9059671045威廉姆洛克,9059671045

below is my code下面是我的代码

   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() ); 

         }

You could simplify it if you are using Java 8:如果您使用的是 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]);
    }
}

And in Java 7:在 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]);
    }
}

For a full functionnal (rather than imperative) solution I propose you this one :对于完整的功能(而不是命令式)解决方案,我建议您使用以下解决方案:

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

output 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 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输出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

As you can see functionnal programming is short and smart ….正如你所看到的,函数式编程既简短又聪明…… and easy when you're accustomed当你习惯时很容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM