0
votes

I am having a bit of an issues trying to pass in a file read by my program and sorted accordantly. I am not used to working with files, and i ran out of ideas as to how this could be achieved.

/////////////////////////////////////// class reads file ///////////////////////////////////

import java.io.*; public class InFileReader {

private BufferedReader inputStream = null;

private String fileLine;

private StringBuilder sb;

public String getFile(File fileRead) throws FileNotFoundException, IOException {

inputStream = new BufferedReader(new FileReader(fileRead)); //reads files
sb = new StringBuilder();
while((fileLine = inputStream.readLine()) != null){//keep reading lines in file till there is none
  sb.append(fileLine).append("\n");
}
return sb.toString(); //returns StringBuffer read values in String form

} } ///////////////////////////////////////////////////// end of read file class ///////////////////////

public void getFile(File fileRead) throws FileNotFoundException,
IOException {

  try {

    String input = fileReader.getFile(fileRead.getAbsoluteFile());

    HashMap<Integer, Thing.Ship> hashmap = new HashMap<>();
    while (!input.isEmpty()) { // as long as there is data in the file keep looping 
      Scanner sc = new Scanner(input); // scan file

      if (!input.startsWith("//")) {   // take out "//"  from directory 
        String type = "";
        if (sc.hasNext()) {  // if there are character lines get next line
          type = sc.next();

        }
        if (type.equalsIgnoreCase("port")) { // looks for "port"
          world.assignPort(new Thing.SeaPort(sc)); // assigns value  to Seaport 
        } else if (type.equalsIgnoreCase("dock")) {
          world.assignDock(new Thing.Dock(sc));
        } else if (type.equalsIgnoreCase("ship")) {
          Thing.Ship s = new Thing.Ship(sc);
          hashmap.put(s.getIndex(), s);
          world.assignShip(s);
        } else if (type.equalsIgnoreCase("pship")) {
          Thing.Ship s = new Thing.PassengerShip(sc);
          hashmap.put(s.getIndex(), s);
          world.assignShip(s);
        } else if (type.equalsIgnoreCase("cship")) {
          Thing.Ship s = new Thing.CargoShip(sc);
          hashmap.put(s.getIndex(), s);
          world.assignShip(s);
        } else if (type.equalsIgnoreCase("person")) {
          world.assignPerson(new Thing.Person(sc));
        }
      }
    }
    //inputOut.setText(type);
    inputOut.setText(world.toString());
  } catch (Exception e) {
    System.out.println(e + "-----");
  }

}

Here fileRead knows where to find the file to be read "C:\Users\abe\IdeaProjects\CreateSeaPortDataFile\src\text.txt"

public void getFile(File fileRead) throws FileNotFoundException,
IOException {

this is where things just fall apart:

String input = fileReader.getFile(fileRead.getAbsoluteFile());

My intent here is to pass the location of the file so that the getFile class can read it and then be sorted into the hashmap. again i am not familiar with how to work with file, any suggestion or comment would be greatly appreciated. thank you in advanced.

1

1 Answers

0
votes

If you get a FileNotFoundException then the file was not found. You say the filename was "C:\Users\abe\IdeaProjects\CreateSeaPortDataFile\src\text.txt". If you type that name in the code you must escape the backslash:

"C:\\Users\\abe\\IdeaProjects\\CreateSeaPortDataFile\\src\\text.txt".