在编写java之后从文件打印
我试图找到这个问题的答案,但我无法通过搜索几个不同的东西。请原谅,如果我的格式不正确,这是我在这里的第一篇文章 所以我正在用java编写一个程序,它基本上会记录用户在不同路线和困难上的攀岩历史 我在写入文本文件时遇到了一个问题(我对在编写java之后从文件打印,java,file-io,Java,File Io,我试图找到这个问题的答案,但我无法通过搜索几个不同的东西。请原谅,如果我的格式不正确,这是我在这里的第一篇文章 所以我正在用java编写一个程序,它基本上会记录用户在不同路线和困难上的攀岩历史 我在写入文本文件时遇到了一个问题(我对FileIO还是新手),在写入文件后,直到退出程序并重新启动程序后,才会打印写入的新信息。这是程序,所讨论的方法是writer(): 公共类爬升{ 私有字符串名称; 私有字符类型; 私人双重评级; 私有字符分级; 私有字符串loc; 私家侦探; 公共爬升(){} 公共
FileIO
还是新手),在写入文件后,直到退出程序并重新启动程序后,才会打印写入的新信息。这是程序,所讨论的方法是writer()
:
公共类爬升{
私有字符串名称;
私有字符类型;
私人双重评级;
私有字符分级;
私有字符串loc;
私家侦探;
公共爬升(){}
公共爬升(字符串名称、字符类型、双重评级、字符子评级、字符串loc、整数尝试){
this.name=名称;
this.type=type;
这个。评级=评级;
这个。次级评级=次级评级;
this.loc=loc;
this.trys=尝试;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符getType(){
返回类型;
}
公共void集合类型(字符类型){
this.type=type;
}
公共双重评级(){
回报率;
}
公共评级(双重评级){
这个。评级=评级;
}
公共字符串getLoc(){
返回loc;
}
公共无效设置位置(字符串位置){
this.loc=loc;
}
公共int getTries(){
回归尝试;
}
公共无效集合尝试次数(整数尝试次数){
this.trys=尝试;
}
公共字符getSubRating(){
回报细分;
}
公共无效设置分级(字符分级){
这个。次级评级=次级评级;
}
公共静态字符串头(){
返回字符串。格式(“%-10s%-10s%-10s%-10s%-10s%,”名称“,”类型“,”等级“,”位置“,”尝试“);
}
@凌驾
公共字符串toString(){
字符串温度=双精度。toString(额定值)+子额定值;
返回字符串。格式(“%-10s%-10s%-10.5s%-10s%-10s%n”,名称、类型、温度、位置、尝试);
}
}
公共类爬坡器{
/*
*打印爬升对象数组的格式化输出
*/
公共静态无效打印记录(爬升[]c){
试一试{
System.out.println(gramb.header());
for(int i=0;i3){
分等级=温度字符((温度长度()-1));
温度=温度子字符串(0,温度长度()-1);
额定值=Double.parseDouble(温度);
}否则{
额定值=Double.parseDouble(温度);
次级评级='';
}
String loc=file.next();
int trys=file.nextInt();
爬升爬升1=新爬升(名称、类型、额定值、次级额定值、位置、尝试次数);
添加(爬升1);
}
grampobjarray=新爬升[grampsarray.size()];
对于(int i=0;ipublic 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");
}
}
}
}
finally {
objFromFile(fn);
}
case 'w':
writer(fn, input);
c = objFromFile(fn); // <--- add this
break;
climbObjArray = climbsArray.toArray(new Climb[climbsArray.size()]);