gpt4 book ai didi

java - 从文本文件中读取数据并过滤内容

转载 作者:行者123 更新时间:2023-11-30 08:51:23 24 4
gpt4 key购买 nike

我有一个文本文件,其中包含一些证券市场数据我想读取文件的内容到现在为止我能够读取全部内容但我需要的是读取特定行而不是全部内容我想存储要存储在变量中的特定行数据如何实现?

要从中读取数据,我有这段代码。

public void loadPartsDataCleanly() throws FileNotFoundException
{
Part compart=new Part();
String line=" ";
File partsfile=new File("C:/Users/ATOMPHOTON/workspace/anup/parts.txt");//put your file location path
Scanner partscan=new Scanner(partsfile);
try {
while (partscan.hasNextLine()){
String mystr=partscan.nextLine();
System.out.println(mystr);


}
partscan.close();
}

catch (Exception e)
{
e.printStackTrace();
// TODO: handle exception
}


}

最佳答案

您可以更改您的部分:

while (partscan.hasNextLine()){
String mystr=partscan.nextLine();
System.out.println(mystr);
}
partscan.close();

int lineNo = 0;
List<String> theOnesICareAbout = new LinkedList<String>();
while (partscan.hasNextLine()){
String line=partscan.nextLine();
if (isOneOfTheImportantLines(lineNo)) {
theOnesICareAbout.add(line);
}
}
partscan.close();

在某个地方你需要一个函数来告诉你给定的行号是否是你关心的:

boolean isOneOfTheImportantLines(int lineNo) {
//YOUR LOGIC HERE
}

您可以添加额外的优化,例如不读取所有文件,而是在您拥有所有您关心的信息后停止读取等。首先让它发挥作用。如果它不起作用,它不起作用的速度有多快都没关系:)

关于java - 从文本文件中读取数据并过滤内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30575361/

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