Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从文本文件中读取和存储数据,其中第一行是标题,其他行是相关数据_Java_Algorithm_File Io - Fatal编程技术网

Java 如何从文本文件中读取和存储数据,其中第一行是标题,其他行是相关数据

Java 如何从文本文件中读取和存储数据,其中第一行是标题,其他行是相关数据,java,algorithm,file-io,Java,Algorithm,File Io,我有一个300行左右的文本文件。格式如下: Name Amount Unit CountOfOrder A 1 ml 5000 B 1 mgm 4500 C 4 gm 4200 // more data public static void main(Str

我有一个300行左右的文本文件。格式如下:

    Name      Amount     Unit    CountOfOrder
      A         1         ml          5000
      B         1         mgm         4500
      C         4         gm          4200

    // more data
       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
我需要逐行读取文本文件,因为每行数据都应该放在一起进行进一步处理

       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
现在我只对每一行使用字符串数组,并通过索引访问数据

for each line in file:
    array[0] = {data from the 'Name' column}
    array[1] = {data from the 'Amount' column}
    array[2] = {data from the 'Unit' column}
    array[3] = {data from the 'CountOfOrder' column}
    ....

    someOtherMethods(array);

    ....
       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
但是,我意识到,如果文本文件更改其格式(例如,切换两列或插入另一列),它将破坏我的程序(通过索引访问可能是错误的,甚至导致异常)

       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
因此,我想使用标题作为参考来访问每一列。也许HashMap是一个很好的选择,但由于我必须将每行数据放在一起,如果我为每行构建一个HashMap,那就太昂贵了

       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 

有人对此有什么想法吗?请帮忙

您可以使用读取文件

       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
CSVReader reader=new CSVReader(new FileReader(“yourfile.txt”),“\t”);
列表行=reader.readAll();

第一行包含标题。

您可以读取文件的每一行,并假设文件的第一行具有列标题,您可以解析该行以获得所有列的名称

String[] column_headers = firstline.split("\t");
       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 

这将为您提供所有列的名称,您现在只需在选项卡上通过拆分读取,它们将全部对齐。

您可以执行以下操作:

    BufferedReader in = new BufferedReader(new InputStreamReader(
            new FileInputStream(FILE)));

    String line = null;
    String[] headers = null;
    String[] data = null;
    Map<String, List<String>> contents = new HashMap<String, List<String>>();

    if ((line = in.readLine()) != null) {
        headers = line.split("\t");
    }
    for(String h : headers){
        contents.put(h, new ArrayList<String>());
    }
    while ((line = in.readLine()) != null) {
        data = line.split("\t");
        if(data.length != headers.length){
            throw new Exception();
        }
        for(int i = 0; i < data.length; i++){
            contents.get(headers[i]).add(data[i]);

        }
    }
       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
BufferedReader in=新的BufferedReader(新的InputStreamReader(
新文件输入流(文件));
字符串行=null;
String[]headers=null;
字符串[]数据=null;
映射内容=新的HashMap();
如果((line=in.readLine())!=null){
页眉=行分割(“\t”);
}
for(字符串h:标题){
put(h,newarraylist());
}
而((line=in.readLine())!=null){
数据=行分割(“\t”);
if(data.length!=headers.length){
抛出新异常();
}
对于(int i=0;i

它会给你灵活性,只需要做一次地图。然后,您可以从地图中获取数据列表,因此它应该是一个方便的数据结构,供程序的其余部分使用。

这将为您提供列的单个列表

       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
publicstaticvoidmain(字符串args[])抛出FileNotFoundException、IOException{
列表标题列表=新的ArrayList();
List column1=新的ArrayList();
List column2=新的ArrayList();
List column3=新的ArrayList();
List column4=新的ArrayList();
int lineCount=0;
BufferedReader br=新的BufferedReader(新文件读取器(“file.txt”);
试一试{
StringBuilder sb=新的StringBuilder();
String line=br.readLine();
字符串标记[];
while(行!=null){
令牌=行分割(“\t”);
如果(行数!=0)
{
整数计数=0;
列1.添加(令牌[count]);++count;
第2列。添加(令牌[count]);++count;
第3列。添加(令牌[count]);++count;
第4列。添加(令牌[count]);++count;
继续;
}
如果(行数==0){

对于(int count=0;count使用标准
java.util.Scanner

       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
    String aa = "   asd    9    1   3  \n d -1 4 2";
    Scanner ss = new Scanner(aa);
    ss.useDelimiter("\n");
    while ( ss.hasNext()){
        String line = ss.next();
        Scanner fs = new Scanner(line);
     System.out.println(  "1>"+     fs.next()+" " +fs.nextInt() +" " +fs.nextLong()+" " +fs.nextBigDecimal());

    }
使用一堆hashmap是可以的……我不会害怕;) 如果您需要处理大量数据……那么尝试将您的问题转化为数据处理转换

       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        } 
例如:
将所有数据读入hashmap,但使用一些
JPA
实现将它们存储在数据库中……然后您可以对数据进行“a”循环;)\

您只需要一个散列映射就可以将列名映射到正确的列索引。您可以像以前一样,通过使用整数索引来填充数组,以便按您需要的名称检索列使用
array[hashmap.get(“Amount”)]

是否有某种分隔符用于分隔每列数据?如果格式更改,可以使用列表存储列的名称并使用索引获取字段。否则,将数据封装在类中。@JoshM,很抱歉我忘了提到这一点。是的,分隔符是制表符(\t)@D.Q.文件中是否包含列标题?@collapsar,您好,这个解决方案太棒了!我想在地图中存储数据,所以我从来没有想过在地图中使用数字作为值!请您将您的评论作为答案,以便我可以接受它?非常感谢!在您的hashMap中,您的键不应该是String[],因为header是一个String[]数组,而不是String。此解决方案是动态的,因此在添加另一列时不会失败。在我看来,如果将数组存储为键,则使用Map的整个目的似乎都会失败。我正在寻找CSV答案,但很失望地发现仍然是“逐行手动读取”-答案。谢谢你让我振作起来:)
       public static void main(String args[]) throws FileNotFoundException, IOException {
            List<String> headerList = new ArrayList<String>();
            List<String> column1 = new ArrayList<String>();
            List<String> column2 = new ArrayList<String>();
            List<String> column3 = new ArrayList<String>();
            List<String> column4 = new ArrayList<String>();

            int lineCount=0;

            BufferedReader br = new BufferedReader(new FileReader("file.txt"));
                try {
                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();
                    String tokens[];

                    while (line != null) {
                        tokens = line.split("\t");
                        if(lineCount != 0)
                        {
                           int count = 0;
                           column1.add(tokens[count]); ++count;
                           column2.add(tokens[count]); ++count;
                           column3.add(tokens[count]); ++count;
                           column4.add(tokens[count]); ++count;
                           continue;
                         }

                        if(lineCount==0){
                            for(int count=0; count<tokens.length; count++){
                                    headerList.add(tokens[count]);
                                    lineCount++;
                            }
                        }

                    }
                } catch (IOException e) {
                    } finally {
                             br.close();
                         }


        }