繁体   English   中英

如何从 java 中的 .txt 文件中获取特定信息?

[英]How can I get specific information out of a .txt file in java?

我是 Java 新手,我找不到打开文本文件的方法并告诉我的计算机行 1 从 0 到“”它是第一个节点,从“”到“”它是第二个节点,从“ " 到 \\n 这是重量。

这是上述 .txt 文件的摘录

Eskildstrup 马里博 28

Eskildstrup NykøbingF 13

Eskildstrup 沃尔丁堡 25

Haslev Korsør 60

Haslev Køge 24

该程序的重​​点应该是创建一个图形,然后使用 prim ......这应该不会太难,但我首先很难进行排序......

我有一些关于我如何在 C++ 上做到的例子:

while(std::getline(game, s))
        {
            std::string n=s.substr(0,s.find("="));
            if(n==m_nom)
            {
                existe=true;
                std::string m=s.substr(s.find("=")+1,100000000);
                std::string o=m.substr(0, m.find(";"));
                std::string coin=m.substr(m.find(";")+1, 100000000);
                std::cout<< o<<std::endl<<coin<<std::endl;
    
                std::string delimiter = ",";

或者

    for (int i = 0; i < m_ordre; i++)
        {
            int tempId=0;
            std::string tempNom;
            double tempAltitude=0;
    
            ifs >> tempId >> tempNom >> tempAltitude;
            m_stations.push_back(new Sommet(tempId, tempNom, tempAltitude));
        }

但我似乎无法在 Java 中做同样的事情。 现在,我有我的节点、顶点和图类。

这就是我的图形类的样子 rn...

class Graph
{
    public Graph(File doc){
        try {
            BufferedReader br= new BufferedReader(new FileReader(doc));
            String s, line, unique, del=" ";
            while ((s= br.readLine())!= null)
                System.out.println(s);
            while () {

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

一种非常基本的方法,但可能就足够了:

  1. 读取文件的行
  2. 用空格分割每一行
  3. 处理结果(例如,为您的图形创建一条边)

以下示例将java.nio.Pathjava.nio.Files用于上述操作 1. 和 2.:

public static void main(String[] args) throws IOException {
    String fileLocation = "/path/to/your/graph-coords.txt";
    // create a Path from the String
    Path filePath = Paths.get(fileLocation);
    // if everything is fine with the file (checks omitted for brevity), read its lines
    List<String> lines = Files.readAllLines(filePath);
    // then handle each line:
    lines.forEach(line -> {
        // split each line by an arbitrary number of whitespaces
        String[] lineValues = line.split("\\s+");
        // and do what you want with the results, e.g. create an edge of the graph
        System.out.println(lineValues[0] + " --" + lineValues[2] + "km--> " + lineValues[1]);
    });
}

这段代码的输出是

Eskildstrup --28km--> Maribo
Eskildstrup --13km--> NykøbingF
Eskildstrup --25km--> Vordingborg
Haslev --60km--> Korsør
Haslev --24km--> Køge

作为处理值的示例,我只是将它们重新排列为不同的String假设重量是以公里为单位的距离。 将文件的所有行放在一个Collection使得检查换行符变得过时(这个库的一个非常方便的东西)。

根据您的计算机系统创建初始String ,我使用了 Linux/MacOs 样式的路径,在 Windows 上,我认为您必须提供驱动器号。

此答案仅显示如何读取行并将它们拆分为单个值。 它不检查文件的存在或读取访问权限,尽管可以使用java.nio (例如java.nio.Files.isRegularFile(Path filePath)java.nio.Files.isReadable(Path filePath)java.nio.Files.exists(Path filePath) )。 Exception处理是强制性的,这里我只是让main方法throw一个IOException来编译代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM