Search code examples
javafile-iogreedy

JAVA : file I/O


I have got two text files with data in the following format

data.txt file as following format

A 10
B 20
C 15

data1.txt file is in format (start node,end node, distance):

A B 5 
A C 10
B C 20

I am trying to implement a search strategy, for that I need to load the data from data.txt and ONLY the start node and end node from data1.txt (i.e. I dont need the distance). I need to store this information in a stack as I think it would be a best data structure for implementing greedy search.

Actually I am not sure how to get started with file I/O to read these files and store them in array to implement greedy search. So I would highly appreciate any starting idea on how to proceed.

I am new to this, so please bear with me. Any help is much appreciated. Thanks.

EDIT: Here is what I have got till now

String heuristic_file = "data.txt";
try
    {          

        FileReader inputHeuristic = new FileReader(heuristic_file);
        BufferedReader bufferReader = new BufferedReader(inputHeuristic);
        String line;

        while ((line = bufferReader.readLine()) != null)   
        {
            System.out.println(line);
        }

        bufferReader.close(); 

    } catch(Exception e) {
        System.out.println("Error reading file " + e.getMessage());
    }

Solution

  • use split

                while ((line = bufferReader.readLine()) != null)   
                {
                    String[] tokens = line.split(" ");
                    System.out.println(line + " -> [" + tokens[0] + "]" + "[" + tokens[1] + "][" + tokens[2] + "]");
                }
    

    if you must have this in an array you can use the following:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class NodeTest {
    
      public static void main(String[] args) throws ParseException {
        try {          
          File first = new File("data.txt");
          File second = new File("data1.txt");    
    
          Node[] nodes1 =  getNodes(first);
          Node[] nodes2 =  getNodes(second);
    
          print(nodes1);
          print(nodes2);
        } 
        catch(Exception e) {
          System.out.println("Error reading file " + e.getMessage());
        }   
      }
    
      public static final void print(Node[] nodes) {
        System.out.println("======================");
        for(Node node : nodes) {
          System.out.println(node);
        }
        System.out.println("======================");
      }
    
      public static final Node[] getNodes(File file) throws IOException {
        FileReader inputHeuristic = new FileReader(file);
        BufferedReader bufferReader = new BufferedReader(inputHeuristic);
        String line;
        List<Node> list = new ArrayList<Node>();
        while ((line = bufferReader.readLine()) != null) {
          String[] tokens = line.split(" ");
          list.add(new Node(tokens[0], tokens[1]));               
        }
        bufferReader.close(); 
        return list.toArray(new Node[list.size()]);
      } 
    }
    
    class Node {
      String start;
      String end;
    
      public Node(String start, String end){
        this.start = start;
        this.end = end;
      }
    
      public String toString() {
        return "[" + start + "][" + end + "]";
      }
    }