我試過找到這個答案,但我無法通過搜索幾個不同的東西。請原諒,如果我沒有正確格式化的東西,這是我的第一篇文章。寫入文件後從文件打印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
,然後將其存儲在印刷和其他的東西對象的數組。
我不認爲這應該是問題,因爲它使用相同的文件對象。除非我需要在寫入之後重新創建文件對象?
更換for循環和數組
climbObjArray
初始化你必須拿出'objFromFile()'的源代碼 –好的,我也在'objFromFile()'方法中加入了。 –
我試着用一個虛擬的'Climb'類來執行和調試你的代碼,它帶有無所作爲的構造函數,並且它似乎工作正常:輸入以及時的方式輸出到標準輸出,並且輸出到文件是在那裏及時爲'objFromFile文件)'成功讀取它。你能否更具體地說明在上述代碼中不起作用的東西? –