gpt4 book ai didi

java - 从CSV文件添加数据和输出数据必须在java中按行对齐

转载 作者:行者123 更新时间:2023-12-02 07:35:43 24 4
gpt4 key购买 nike

下面的代码读取 .csv 文件并以(当前输出)方式将其显示在控制台上。

我希望它在控制台上显示为(想要的输出)方式...抱歉,我无法添加图像...(行)意味着在2个数据之间,我需要一条垂直线...请指导我。提前谢谢你

想要的输出:-

Firstname (line) Lastname (line) Main-Email (line) (current employer) (line)
ram (line) rfa (line) k@.com (line) (golhys) (line)

电流输出:-

Line # 1 Token : "First Name","Last Name","Main-Email","Current Employer","Internal    Contact ID","PSI Club Member Nbr.","Membership Type","Membership Status","Member Since","Membership Card Sent","Membership Start Date","Membership Expiration Date","Nationality 1","Membership ID"

Readfile.java

public static void main(String[] args) {
String fileName="C:/Users/Desktop/cvsFile.csv";
try {
BufferedReader br = new BufferedReader( new FileReader(fileName));
// String strLine = null;
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while( (fileName = br.readLine()) != null)
{
lineNumber++;

//break comma separated line using ","
st = new StringTokenizer(fileName, "");
while(st.hasMoreTokens())
{
//display csv values
tokenNumber++;
System.out.println("Line # " + lineNumber + " Token : "+ st.nextToken());//.replaceAll(",", ",|,") replaceAll("..(?!$)", "$0 ")

}
//reset token number
tokenNumber = 0;
}
}

catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

最佳答案

引用这段代码:

public static void main(String[] args) {
String fileName="/home/Csvfile.csv";
try {
BufferedReader br = new BufferedReader( new FileReader(fileName));
String strLine = null;
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while( (fileName = br.readLine()) != null)
{
lineNumber++;
//break comma separated line using ","
st = new StringTokenizer(fileName, ",");
System.out.print("\n" + lineNumber + " | ");
while(st.hasMoreTokens())
{
//display csv values
tokenNumber++;
System.out.print(st.nextToken()+" | ");
}
//reset token number
tokenNumber = 0;
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

关于java - 从CSV文件添加数据和输出数据必须在java中按行对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12258241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com