2016-05-15 44 views
1

我试过找到这个答案,但我无法通过搜索几个不同的东西。请原谅,如果我没有正确格式化的东西,这是我的第一篇文章。写入文件后从文件打印java

因此,我正在用java编写一个程序,该程序基本上会记录用户在不同路线和难度上的攀岩历史。

我遇到了写入文本文件的问题(我还是FileIO的新手),在写入文件之后,直到退出程序并重新启动后才写入写入的新信息它。下面是程序,所讨论的方法是writer()

public class Climb { 
    private String name; 
    private char type; 
    private double rating; 
    private char subRating; 
    private String loc; 
    private int tries; 


    public Climb(){} 

    public Climb(String name, char type, double rating, char subRating, String loc, int tries) { 
     this.name = name; 
     this.type = type; 
     this.rating = rating; 
     this.subRating = subRating; 
     this.loc = loc; 
     this.tries = tries; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public char getType() { 
     return type; 
    } 

    public void setType(char type) { 
     this.type = type; 
    } 

    public double getRating() { 
     return rating; 
    } 

    public void setRating(double rating) { 
     this.rating = rating; 
    } 

    public String getLoc() { 
     return loc; 
    } 

    public void setLoc(String loc) { 
     this.loc = loc; 
    } 

    public int getTries() { 
     return tries; 
    } 

    public void setTries(int tries) { 
     this.tries = tries; 
    } 

    public char getSubRating() { 
     return subRating; 
    } 

    public void setSubRating(char subRating) { 
     this.subRating = subRating; 
    } 

    public static String header(){ 
     return String.format("%-10s %-10s %-10s %-10s %-10s","Name","Type","Rating","Location","Attempts"); 
    } 

    @Override 
    public String toString() { 
     String tempRating = Double.toString(rating) + subRating; 
     return String.format("%-10s %-10s %-10.5s %-10s %-10s %n", name, type, tempRating, loc, tries); 
    } 
} 

public class ClimbTracker { 
    /* 
    * prints a formatted output of an array of Climb objects 
    */ 
    public static void printRecord (Climb[] c) { 
     try { 
      System.out.println(Climb.header()); 
      for (int i = 0; i < c.length; i++){ 
       System.out.print(c[i].toString()); 
      } 
     } 
     catch (NullPointerException ex) { 
      System.out.println("Error: " + ex.getMessage()); 
     } 
    } 

    /* 
    * Creates a new array of Climb objects from a file and return the array. 
    * Number of objects in file doesn't need to be known 
    */ 
    public static Climb[] objFromFile (File fn){ 
     try { 
      Scanner file = new Scanner(fn); 
      ArrayList<Climb> climbsArray = new ArrayList<>(); 
      Climb[] climbObjArray; 
      while (file.hasNext()) { 
       // name, type, rating, subRating, location, tries 
       String name = file.next(); 
       char type = file.next().charAt(0); 
       String temp = file.next(); 
       char subRating; 
       double rating; 
       /* 
       * This if block is to deal with the problem that climbing 
       * ratings are often something like "5.12a", so it splits it 
       * into a double and a char as rating and subRating respectively 
       */ 
       if (temp.length() > 3){ 
        subRating = temp.charAt((temp.length() -1)); 
        temp = temp.substring(0, temp.length() -1); 
        rating = Double.parseDouble(temp); 
       } else { 
        rating = Double.parseDouble(temp); 
        subRating = ' '; 
       } 
       String loc = file.next(); 
       int tries = file.nextInt(); 
       Climb climb1 = new Climb(name,type,rating,subRating,loc,tries); 
       climbsArray.add(climb1); 
      } 
      climbObjArray = new Climb[climbsArray.size()]; 
      for (int i = 0; i < climbsArray.size(); i++) { 
       climbObjArray[i] = climbsArray.get(i); 
      } 
      return climbObjArray; 
     } 
     catch (FileNotFoundException ex){ 
      System.out.println("Error " + ex.getMessage()); 
     } 
     return null; 
    } 

    /* 
    * Will write new climbs to the file 
    */ 
    public static void writer (File fn, Scanner input){ 
     try { 
      FileOutputStream fn_stream = new FileOutputStream(fn,true); 
      PrintWriter out = new PrintWriter(fn_stream); 
      System.out.print("Name of route: "); 
      out.print(input.next() + " "); 
      System.out.print("(B)ouldering or (t)oprope: "); 
      out.print(input.next().charAt(0) + " "); 
      System.out.print("Rating: "); 
      out.print(input.next() + " "); 
      System.out.print("Location of route: "); 
      out.print(input.next() + " "); 
      System.out.print("Number of attempts: "); 
      out.print(input.next() + "\n"); 
      out.flush(); 
      fn_stream.flush(); 
      out.close(); 
      fn_stream.close(); 
     } catch (FileNotFoundException ex) { 
      System.out.println("Error: " + ex.getMessage()); 
     } catch (IOException ex) { 
      System.out.println("Error: " + ex.getMessage()); 
     } finally { 
      objFromFile(fn); 
     } 
    } 

    public static void main(String[] args) { 
     File fn = new File("climbs.txt"); 
     Scanner input = new Scanner(System.in); 
     Climb[] c = objFromFile(fn); 
     while (true) { 
      System.out.println("What would you like to do?"); 
      System.out.println("(P)rint recent climbs"); 
      System.out.println("(W)rite others"); 
      System.out.println("(E)xit"); 
      char option = input.next().charAt(0); 
      switch (option){ 
       case 'p': 
        printRecord(c); 
        break; 
       case 'w': 
        writer(fn, input); 
        break; 
       case 'e': 
        System.exit(0); 
        break; 
       default: 
        System.out.println("That isn't an option"); 
      } 
     } 
    } 
} 

在最后块的方法,读取文件并创建对象的ArrayList,然后将其存储在印刷和其他的东西对象的数组。

我不认为这应该是问题,因为它使用相同的文件对象。除非我需要在写入之后重新创建文件对象?

+0

更换for循环和数组climbObjArray初始化你必须拿出'objFromFile()'的源代码 –

+0

好的,我也在'objFromFile()'方法中加入了。 –

+1

我试着用一个虚拟的'Climb'类来执行和调试你的代码,它带有无所作为的构造函数,并且它似乎工作正常:输入以及时的方式输出到标准输出,并且输出到文件是在那里及时为'objFromFile文件)'成功读取它。你能否更具体地说明在上述代码中不起作用的东西? –

回答

0

要打印(从菜单中选择时)是一个在main这是Climb[] c,而一送writer()被调用后加载/刷新的阵列是另外一个它位于objFromFile()所以要装载到一个阵列,并打印另一个Climb[] climbObjArray

所以建议你在该匹配器write所以在main阵列被更新,从而将正确当用户选择功能print

1-取出已打印的情况下除去finally块,并调用objFromFile()mainwriter()

finally { 
    objFromFile(fn); 
} 

2-更新case 'w'该块如下:

case 'w': 
    writer(fn, input); 
    c = objFromFile(fn); // <--- add this 
    break; 

现在当case 'p'匹配时,调用printRecord(c);将通过更新c

PS:更好apprach到ArrayList转换成阵列使用toArray()代替for循环

climbObjArray = climbsArray.toArray(new Climb[climbsArray.size()]); 

与上述行