1、通过FileReader(String fileName)
读取整个文件,并通过BufferedReader(Reader in)
作为每行的缓冲;
2、在通过readLine()
读取每行内容,通过空格作为分隔符,每行转为数组,并存在List<String[]>
列表中;
3、最后打印 csv文件中的内容。文章来源地址https://www.toymoban.com/news/detail-612599.html
- csv 文件内容如下:
clk 101 111
reset 102 112
test 103 113
Mode1 104 114
Mode0 105 115
- 打印的代码如下:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class transform {
List<String[]> ReadMethod(){
String File = "C:/Users/zeal.zhang/Documents/ExamB/src/Data/1.csv";
String line = "";
String SplitBy = " ";
String[] Line;
List<String[]> BikeDataList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(File))) {
while ((line = br.readLine()) != null) {
Line = line.split(SplitBy);
BikeDataList.add(Line);
System.out.println("signal "+Line[0]+" {\n\tsite 1 { pogo = "+Line[1]+"; }" +
"\n\tsite 2 { pogo = "+Line[2]+"; }\n}");
}
} catch (IOException e) {
e.printStackTrace();
}
return BikeDataList;
}
public static void main(String[] args) {
new transform().ReadMethod();
}
- 输出的内容如下:
signal clk {
site 1 { pogo = 101; }
site 2 { pogo = 111; }
}
signal reset {
site 1 { pogo = 102; }
site 2 { pogo = 112; }
}
signal test {
site 1 { pogo = 103; }
site 2 { pogo = 113; }
}
signal Mode1 {
site 1 { pogo = 104; }
site 2 { pogo = 114; }
}
signal Mode0 {
site 1 { pogo = 105; }
site 2 { pogo = 115; }
}
文章来源:https://www.toymoban.com/news/detail-612599.html
到了这里,关于通过Java读取csv文件内容的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!