[英]How do I input data from a CSV file into my code?
I have a CSV file that is like:我有一个类似于以下内容的 CSV 文件:
What I want is that using scanner, I will be able to choose one of the two orbits I have in the CSV file.我想要的是使用扫描仪,我将能够选择 CSV 文件中的两个轨道之一。 Let's say for example, using scanner if I put 1 in the console, it chooses the row of the CSV file: LEO,7168000,0,90 and if I put 2 the line: MEO,20200000,0,54.例如,如果我在控制台中输入 1,则使用扫描仪,它会选择 CSV 文件的行:LEO,7168000,0,90,如果我将 2 输入:MEO,20200000,0,54。
After that from the row chosen it saves each parameter in a variable skipping the name (LEO,MEO).之后,从选择的行开始,它将每个参数保存在一个变量中,跳过名称(LEO,MEO)。 For example if I choose LEO orbit, it saves the variables like:例如,如果我选择 LEO 轨道,它会保存如下变量:
So in the end I can use those parameters in my program.所以最后我可以在我的程序中使用这些参数。 Thank you for your answers.谢谢您的回答。
Csv file is a plain text file and separates with ,
character for each cell and \\n
for each row. Csv 文件是一个纯文本文件,每个单元格用,
字符分隔,
每行用\\n
分隔。
The easy way, you only need using FileInputStream to read and split \\n
and ,
character for using.简单的方法,您只需要使用 FileInputStream 读取和拆分\\n
和,
字符即可使用。
File file = new File("file.csv");
FileInputStream fis = null;
String dataStr = "";
try {
fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
dataStr += (char) content;
}
} catch (IOException e) {
e.printStackTrace();
}
// convert to array rows string
String[] dataRows = dataStr.split("\n");
// loop rows to get cells string
for (int i = 0; i < dataRows.length; i++) {
String[] dataCells = rowData[i].split(",");
//do what ever you want with dataCells
}
Thanks for read.感谢阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.