对java中的文件进行操作
我在使用java中的所有FileReader和FileWriters时遇到了一些问题。我希望我的程序能够得到这个人的名字,然后在询问他们几个问题后评估他们的分数。这是很容易的,但现在我想要做的就是记录它在这样的日志文件:每次这些人打开程序会检索其当前得分对java中的文件进行操作
Jacob : 10
Mark : 15
Steve : 7
然后,或者如果它是一个新的它会追加他们的名字和分数。 我无法找到搜索文件的方法,然后在最后检索整数。
编辑:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class Main {
static Scores scoreClass = new Scores();
private static void setupMap() {
try {
FileInputStream fin = new FileInputStream("scores.map");
@SuppressWarnings("resource")
ObjectInputStream ois = new ObjectInputStream(fin);
scoreClass = (Scores) ois.readObject();
} catch (Exception e) {
System.err.println("Could not load score data");
e.printStackTrace();
}
}
private static void saveMap() {
try {
FileOutputStream fout = new FileOutputStream("scores.map");
@SuppressWarnings("resource")
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(scoreClass);
} catch (Exception e) {
System.err.println("Could not save score data");
e.printStackTrace();
}
}
public static void main(String[] args) {
setupMap();
int score = 0;
String guess;
String[][] questions_with_answers = {{"I __________ to school.(go)", "She __________ tennis. (play)", "She _______ a bitch. (be)"}, {"am going", "was playing", "is being"}};
Scanner answer = new Scanner(System.in);
Scanner studentInput = new Scanner(System.in);
System.out.println("What is your name?");
String name = answer.nextLine();
if(Scores.getScore(name) == -1){
Scores.setScore(name, score);
} else {
Scores.getScore(name);
}
System.out.println("Hello " + name + ". Your score is: " + score);
for(int i = 0; i < (questions_with_answers[0].length); i++){
System.out.println(questions_with_answers[0][i]);
guess = studentInput.nextLine();
if(guess.equals(questions_with_answers[1][i])){
System.out.println("Correct! You get 1 point.");
score = score + 1;
System.out.println("Your score is " + score);
} else {
System.out.println("You made a mistake! No points...");
}
}
System.out.printf("Congrats! You finished the test with "+ score +" points out of " + questions_with_answers[0].length);
Scores.setScore(name, score);
saveMap();
answer.close();
studentInput.close();
}
}
这不会引发任何异常,但仍然没有从文件中读取输入:/
您可以将数据存储为一个哈希表,然后序列化到一个保存文件
我写了一个示例,其中包含一个名为Scores的类,该类具有读取分数并将分数添加到包含的散列图的方法。我还在主类中添加了保存和打开包含分数类数据的文件的方法。
主类:
public class Main {
static Scores scoreClass = new Scores();
public static void main(String[] args) {
setupMap();
//Do score calculations
saveMap();
}
private static void setupMap() {
try {
FileInputStream fin = new FileInputStream("scores.map");
@SuppressWarnings("resource")
ObjectInputStream ois = new ObjectInputStream(fin);
scoreClass = (Scores) ois.readObject();
} catch (Exception e) {
System.err.println("Could not load score data");
e.printStackTrace();
}
}
private static void saveMap() {
try {
FileOutputStream fout = new FileOutputStream("scores.map");
@SuppressWarnings("resource")
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(scoreClass);
} catch (Exception e) {
System.err.println("Could not save score data");
e.printStackTrace();
}
}
}
成绩等级:
public class Scores implements Serializable{
private static final long serialVersionUID = 1L;
public static Map<String, Integer> scoreMap = new HashMap<String, Integer>();
public static int getScore(String name) {
if (scoreMap.containsKey(name)) {
return scoreMap.get(name);
} else {
return -1;
}
}
public static void setScore(String name, int score) {
scoreMap.put(name, score);
}
}
非常感谢帮助人:)仍然由于某种原因得分和名称不从文件读取:/任何想法为什么那可能是? –
使用[java.util.Properties](https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html)类将负责为您进行序列化:-) – Selim
那你试试这么远吗?向我们展示一些代码 – nachokk
您可以在每行中使用'String [] array = line.split(“:”);'这将返回一个数组,在您的情况下,如果它被正确保存,数组的大小应该是2 。然后,只需使用'int score = Integer.valueOf(array [1])' – nachokk