在java中读取CSV文件并显示内容

时间:2016-02-04 17:58:28

标签: java

我想创建一个程序来读取将包含的逗号分隔值文件 [员工姓名,公司名称,工资]。 并显示输出 员工姓名: - ,薪水:---,公司: -

为此,我创建了2个classes.i刚刚编写了一个简单的程序,它将读取和显示逗号分隔值文件。我想知道如何单独显示内容?

   <style>
  .game_filter_show {color: red; } 
  .game_filter_hide{background-color: blue;}
  </style>

    <div class="element games_survival">text in here1</div>
    <div class="element games_fps">text in here2</div>

    <div onclick="allFunction()">all</div>
    <div onclick="fpsFunction()">fps</div>
    <div onclick="survivalFunction()">survivval</div>

<script>
function allFunction() {
var elements = document.getElementsByClassName("element");

      elements = Array.prototype.slice.call(elements,0);
    elements.map(function(element){
          element.classList.toggle("game_filter_show", true);
          element.classList.toggle("game_filter_hide", false);
    })
}

function fpsFunction() {
var elements = document.getElementsByClassName("element");
      elements = Array.prototype.slice.call(elements,0);
    elements.map(function(element){
      if (element.classList.contains("games_fps")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false);
    } else {
        element.classList.toggle("game_filter_show", false);
        element.classList.toggle("game_filter_hide", true);
    }    
    })
}

function survivalFunction() {
var elements = document.getElementsByClassName("element");
      elements = Array.prototype.slice.call(elements,0);
    elements.map(function(element){
      if (element.classList.contains("games_survival")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false); 
    } else {
        element.classList.toggle("game_filter_show", false);
        element.classList.toggle("game_filter_hide", true);
    } 
    })
}
</script>
package com.readcsvfile;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadCsvFile { 

  private String currentLine;
  BufferedReader br; 
  public void readFile(String fileName) throws IOException { 
  br = new BufferedReader(new FileReader(fileName));
  while ((currentLine = br.readLine()) != null) { 
  System.out.println(currentLine); 
  }
 }
}## 

2 个答案:

答案 0 :(得分:0)

public class ReadCsvFile { 

    private String currentLine;
    BufferedReader br;

    public void readFile(String fileName) throws IOException { 
         br = new BufferedReader(new FileReader(fileName));
         while ((currentLine = br.readLine()) != null) { 
           String [] lineAfterSplit = currentLine.split(",");
           System.out.print("Employee Name:--"+ lineAfterSplit[0] + " Company Name:--" + lineAfterSplit[1] + " Salary:--" + "lineAfterSplit[2]);
         }
     }
}

答案 1 :(得分:0)

您可以使用split()方法执行此类操作,将输入拆分为所需的输出:

String[] str = currentLine.split(",");
System.out.println("Employee Name: " + str[0] + ",Salary: " + str[1] + ",Company:" + str[2]); 

以下是代码段:

public class ReadCsvFile {
    private String currentLine;
    BufferedReader br; 
    public void readFile(String fileName) throws IOException { 
        br = new BufferedReader(new FileReader(fileName));
        while ((currentLine = br.readLine()) != null) { 
            String[] str = currentLine.split(",");
            System.out.println("Employee Name: " + str[0] + ",Salary: " + str[1] + ",Company:" + str[2]); 
        }
    }
}