您好,我是一名低水平的计算机科学学生,对文件 I/O 确实很困难/不熟悉。
我正在尝试使用缓冲阅读器读取文本文件。我了解如何使用 while 循环继续扫描直到到达文件末尾,但是如何指示我的读者只读取一行并执行某些操作直到到达该行的末尾,然后读取下一行并执行某些操作直到该行结束,等等?
基本上我的输入文本文件将每三行重复一次。该文本文件表示加权有向图中的节点。
输入文本文件应该如下所示:
每个节点由两行文本表示。例如最上面一行,第一个“S”是节点的名称,第二个“S”表示它是起始节点,第三个“n”表示它是常规节点,而不是目标节点,目标节点将用“g”表示。
第二行是连接到“S”的两个节点,第一个是“B”,加权距离为 1,第二个是“E”,加权距离为 2。
第三行应该是空白,并且模式是重复的。
S S n
B 1 E 2
B N n
C 2 F 3
C N n
D 2 GA 4
D N n
GA 1
E N n
B 1 F 3 H 6
F N n
I 3 GA 3 C 1
GA N g
H N n
I 2 GB 2 F 1
I N n
GA 2 GB 2
GB N g
我的代码如下:
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == openButton)
{
returnVal = fileChooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
selected_file = fileChooser.getSelectedFile();
String file_name = fileChooser.getSelectedFile().getName();
file_name = file_name.substring(0, file_name.indexOf('.'));
try
{
BufferedWriter buff_writer = null;
File newFile = new File("."+file_name+"_sorted.txt");
boolean verify_creation = newFile.createNewFile();
//if (verify_creation)
// System.out.println("file created successfully");
//else
// System.out.println("file already present in specified location");
file_reader1 = new BufferedReader(new FileReader(selected_file));
file_reader2 = new BufferedReader(new FileReader(selected_file));
FileWriter file_writer = new FileWriter(newFile.getAbsoluteFile());
buff_writer = new BufferedWriter(file_writer);
//find the number of nodes in the file
while( (currentLine = file_reader1.readLine()) != null)
{
k++;
//System.out.println("value of k: " + k);
}
nodeArray = new Node[k];
while( (currentLine = file_reader2.readLine()) != null)
{
//System.out.print(currentLine);
String[] var = currentLine.split(" ");
nodeArray[x] = new Node(var[0]);
if (var[1].equals('S') || var[1].equals('s'))
nodeArray[x].setType(NodeType.START);
else if (var[2].equals('g') || var[2].equals('G'))
nodeArray[x].setType(NodeType.GOAL);
else
nodeArray[x].setType(NodeType.NORMAL);
x++;
}
buff_writer.close();
file_writer.close();
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
我的节点类如下:
import java.util.*;
enum NodeType
{
START, GOAL, NORMAL;
}
public class Node
{
private String name;
private NodeType typeOfNode;
private final Map<Node, Integer> neighbors = new HashMap<>();
public Node(String name)
{
this.name = name;
}
public void setType(NodeType type)
{
typeOfNode = type;
}
public void addAdjacentNode(Node node, int distance)
{
neighbors.put(node, distance);
}
public String toString()
{
String output = "";
output += "node name: " + name + ",\n";
return output;
}
}
我的另一个主要问题是如何处理重复的三行序列中的第二行。第二行给出所有相邻节点及其距第一行描述的节点的加权距离。问题是,我不知道任何给定节点将存在多少个相邻节点。从技术上讲,可能没有,也可能有很多。
这里一位善良的程序员建议我使用 HashMap 来记录相邻节点,但我不确定如何构造一行代码来解释不确定数量的此类邻接
注意:这个问题引用了我之前提出的问题:how to create an adjacency matrix, using an input text file, to represent a directed weighted graph [java]?
如果有人能指出我正确的方向,我将永远感激不已
最佳答案
就输入问题而言,您的 while 循环会以相同的方式处理它读取的每一行。您必须添加一个变量来跟踪您正在处理的 3 行序列中的哪一行。
关于java - 如何迭代文本文件以执行不同的任务(包括创建未知数量的对象),具体取决于我正在阅读的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40443338/