Java:帮助输出到文件?

时间:2014-09-14 03:22:34

标签: java arrays netbeans java-io

处理类的Java项目。教授希望我们输出一个我以前从未做过的文件。 说明:     创建program1.states.in.out并在提交项目时将其包含在项目文件夹中     此文件不会显示为步骤3中显示的输出。它只包含     未格式化的数据行(没有间隔;没有标题......)。那么输出文件将包含50个     仅数据行。在步骤6中,您将需要再次为此输入创建此副本。所以     确保格式正确。它看起来像:

Texas          Austin         TX19759614Southwest      5
New_Mexico     Santa_Fe       NM 1736931Southwest      5
Arizona        Phoenix        AZ 4668631Southwest      5

这看起来与他的输入文件结构相同。但是我已经修剪/解析了它以填充State数组。如何将其恢复为此格式,然后将其输出到文件?

用于修剪/解析输入文件的代码(在main中):

inputString = br1.readLine();
        while (inputString != null) {
            stateName = inputString.substring(0, 15).trim();
            stateCapital = inputString.substring(15, 30).trim();
            stateAbbrev = inputString.substring(30, 32).trim();
            statePop = Integer.parseInt(inputString.substring(32, 40).trim());
            stateRegion = inputString.substring(40, 55).trim();
            stateRegionNum = Integer.parseInt(inputString.substring(55));
            Sdriver.insert(stateName, stateCapital, stateAbbrev, statePop, stateRegion, stateRegionNum);
            inputString = br1.readLine();    // read next input line.
        }
        br1.close();     //Close input file being read

这是State.class。

public class State {
   private String stateName;    
   private String stateCapital;
   private String stateAbbrev;
   private int statePop;
   private String stateRegion;
   private int stateRegionNum;
   public static final String HEADER_STRING = "\n%-15s  %-15s  %-4s  %-10s  %-15s  %-8s\n\n";
   private static final String DISPLAY_STRING = "%-15s  %-15s  %-4s  %,10d  %-15s  %1d";

    public State(String name, String capital, String abbrev, int pop, String region, int regionNum) {
      stateName = name;
      stateCapital = capital;
      stateAbbrev = abbrev;
      statePop = pop;
      stateRegion = region;
      stateRegionNum = regionNum;
      }//End State

   public void displayState(int nElems, State myState[]) {
      System.out.print(String.format(State.HEADER_STRING, "State", "Capital", "Abbr", "Population", "Region", "Region #"));

        for(int x = 0; x < nElems - 1; x++) {
            System.out.println(myState[x].toString());
       } 
   }//End displayState

编辑:我尝试输出代码。

String file = "src/program1.states.in.out.txt";
            BufferedWriter writer = new BufferedWriter(new FileWriter("src/program1.states.in.out.txt")); {
            for(int x = 0; x < Sdriver.getnElems(); x++) {
                writer.write(Sdriver.getState(x).toString());
            }
        }

创建了一个文件,但该文件为空。

1 个答案:

答案 0 :(得分:0)

你看起来并不是一个不太糟糕的地方:

String file = "src/program1.states.in.out.txt";
    BufferedWriter writer = new BufferedWriter(new FileWriter("src/program1.states.in.out.txt")); {
    for(int x = 0; x < Sdriver.getnElems(); x++) {
        writer.write(Sdriver.getState(x).toString());
    }
}

但请考虑使用PrintWriter对象和displayState()方法。类似于:

PrintWriter printWriter = null;
try {
    String filePath = "src/program1.states.in.out.txt";
    writer = new BufferedWriter(new FileWriter(filePath));
    PrintWriter printWriter = new PrintWriter(writer);
    for(int x = 0; x < Sdriver.getnElems(); x++) {
        printWriter.println(Sdriver.getState(x).displayState());
    }
} catch (IOException ioE) {
    ioE.printStackTrace();
} finally {
    if (printWriter != null) {
        printWriter.close();
    }
}