繁体   English   中英

将文件文本传递到哈希图中

[英]Pass in file text into hashmap

我在尝试传递程序读取的文件并按顺序进行排序时遇到了一些问题。 我不习惯使用文件,而且我对如何实现这一点也没有足够的想法。

///////////////////////////////////////类读取文件//////// ////////////////////////////

导入java.io. *; 公共类InFileReader {

private BufferedReader inputStream = null;

私有String fileLine;

私人StringBuilder sb;

public String getFile(File fileRead)抛出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

}} ////////////////////////////////////////////////// //////读取文件类的结尾//////////////////////

public void getFile(File fileRead)抛出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 + "-----");
  }

}

在这里fileRead知道在哪里可以找到要读取的文件“ C:\\ Users \\ abe \\ IdeaProjects \\ CreateSeaPortDataFile \\ src \\ text.txt”

public void getFile(File fileRead)抛出FileNotFoundException,
IOException {

这就是事情分崩离析的地方:

字符串输入= fileReader.getFile(fileRead.getAbsoluteFile());

我的目的是传递文件的位置,以便getFile类可以读取它,然后将其排序到哈希图中。 再次,我不熟悉如何使用文件,任何建议或评论将不胜感激。 谢谢你。

如果收到FileNotFoundException,则找不到该文件。 您说文件名是“ C:\\ Users \\ abe \\ IdeaProjects \\ CreateSeaPortDataFile \\ src \\ text.txt”。 如果在代码中键入该名称,则必须转义反斜杠:

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

暂无
暂无

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

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