gpt4 book ai didi

java - 文本文件很重要

转载 作者:行者123 更新时间:2023-12-01 13:47:37 25 4
gpt4 key购买 nike

在此输入图像描述,我正在开发一个在文本文件中写入和读取的项目,这是从文本文件读取和写入的两个函数:

public void writetofile()
{
String bucky4[]={custname,custlname,agee,address,id};
for(int i = 0; i < bucky4.length; i++) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("records.txt", true)))
{
String s;
s = bucky4[i];
bw.write(s);
bw.newLine();
bw.flush();
}
catch(IOException ex) {
JOptionPane.showMessageDialog(null, "Error In File");
}
}
}

public void filereader (){
int i=0;
Object[] options = {"OK"};
try
{
FileInputStream in = new FileInputStream("records.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
StringBuffer sb = new StringBuffer();

while((strLine = br.readLine())!= null)
{
sb.append(strLine +"\n");
}
JOptionPane.showMessageDialog( null, sb.toString());
}catch(Exception e){
JOptionPane.showOptionDialog(null, "Error", "Customers", JOptionPane.PLAIN_MESSAGE,JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
}
}

问题是每当我从文件中读取时,详细信息都会被打印出来,没有任何特定的格式,如下所示:

  1. 迈克尔

  2. jackson

  3. 22

  4. c-.8.16 pri 血清

  5. d20

我期待有一个功能可以帮助我像这样格式化它:

  1. 名字:迈克尔

  2. 姓氏: jackson

          ......,

    提及我无法使用构造函数,并且感谢任何帮助

最佳答案

您可以使用字符串数组来保存值

String[] titles = {"first name: ", "last name: ", ...}

int i = 0;
while((strLine = br.readLine())!= null)
{
sb.append(titles[i] + strLine +"\n");
i++;
}

关于java - 文本文件很重要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241349/

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