我有这个保存类,完全可以正常工作:
package com.mestru.esport;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Random;
public class NewPlayer {
public static void main (String args[])
{
Random rand = new Random();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
try
{
FileOutputStream saveFile = new FileOutputStream("Player.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(naturalTalent);
save.writeObject(luck);
save.writeObject(figure);
save.writeObject(agility);
save.writeObject(intelligence);
save.writeObject(multiTasking);
save.writeObject(stamina);
save.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}
然后我有一个类,它被设计为加载并使加载的变量可用于其他类。如果装载部件完美无缺,那么我就不能在'main'方法之外使用它们。
package com.mestru.esport;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Random;
public class Player
{
int naturalTalent;
int luck;
int figure;
int agility;
int intelligence;
int multiTasking;
int stamina;
public static void main (String args[]) {
int NEWnaturalTalent = 0;
int NEWluck = 0;
int NEWfigure = 0;
int NEWagility = 0;
int NEWintelligence = 0;
int NEWmultiTasking = 0;
int NEWstamina = 0;
try {
FileInputStream saveFile = new FileInputStream("Player.sav");
ObjectInputStream save = new ObjectInputStream(saveFile);
NEWnaturalTalent = (Integer) save.readObject();
NEWluck = (Integer) save.readObject();
NEWfigure = (Integer) save.readObject();
NEWagility = (Integer) save.readObject();
NEWintelligence = (Integer) save.readObject();
NEWmultiTasking = (Integer) save.readObject();
NEWstamina = (Integer) save.readObject();
save.close();
} catch (Exception exc) {
exc.printStackTrace();
}
Player player = new Player();
player.naturalTalent = NEWnaturalTalent;
player.luck = NEWluck;
player.figure = NEWfigure;
player.agility = NEWagility;
player.intelligence = NEWintelligence;
player.multiTasking = NEWmultiTasking;
player.stamina = NEWstamina;
}
public void test ()
{
System.out.println("Natural Talent: " + naturalTalent);
System.out.println("Luck: " + luck);
System.out.println("Figure: " + figure);
System.out.println("Agility: " + agility);
System.out.println("Intelligence: " + intelligence);
System.out.println("Multitasking: " + multiTasking);
System.out.println("Stamina: " + stamina);
}
}
当我在另一个类中使用方法'test'时,所有值都等于0.
所以这是我的问题。我可以以某种方式使我的所有实例变量等于'main'方法中加载的那些变量吗?我也不能在'main'方法之外使用“try”:(
提前感谢您的帮助< 3感谢所有的线索,我刚刚开始学习。
答案 0 :(得分:1)
当你加载时,你创建一个新的播放器,设置它然后丢弃它。
也许您打算设置当前播放器的字段。
此外,您不需要临时变量。你可以设置字段。
public static void main(String... ignored) throws IOException {
Player player = new Player();
player.load("Player.sav");
player.test();
}
public void load(String filename) throws IOException {
try (FileInputStream saveFile = new FileInputStream(filename);
ObjectInputStream save = new ObjectInputStream(saveFile)) {
naturalTalent = (Integer) save.readObject();
luck = (Integer) save.readObject();
figure = (Integer) save.readObject();
agility = (Integer) save.readObject();
intelligence = (Integer) save.readObject();
multiTasking = (Integer) save.readObject();
stamina = (Integer) save.readObject();
}
}
您可以将其写为文本,而不是以二进制形式编写此数据。这样可以更容易阅读/编辑,在这个用例中会更紧凑。即序列化Integer
对象可能会非常大。
答案 1 :(得分:0)
JavaCode For Main Class
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Random rand = new Random();
ArrayList<Integer> arl = new ArrayList<Integer>();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
String fileName = "/Users/rk4262/Player.txt";
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("naturalTalent value is :" + naturalTalent);
bufferedWriter.newLine();
bufferedWriter.write("luck value is : " + luck);
bufferedWriter.newLine();
bufferedWriter.write("figure value is : " + figure);
bufferedWriter.newLine();
bufferedWriter.write("agility value is : " + agility);
bufferedWriter.newLine();
bufferedWriter.write("intelligence value is : " + intelligence);
bufferedWriter.newLine();
// Always close files.
bufferedWriter.close();
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
Player p = new Player();
String s = p.readLine();
System.out.println(s);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher m = pattern.matcher(s);
while (m.find()) {
int n = Integer.parseInt(m.group());
arl.add(n);
}
p.NEWnaturalTalent = arl.indexOf(0);
p.NEWluck=arl.indexOf(1);
p.NEWfigure=arl.indexOf(2);
p.NEWagility=arl.indexOf(3);
}
}
玩家类代码
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Random rand = new Random();
ArrayList<Integer> arl = new ArrayList<Integer>();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
String fileName = "/Users/rk4262/Player.txt";
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("naturalTalent value is :" + naturalTalent);
bufferedWriter.newLine();
bufferedWriter.write("luck value is : " + luck);
bufferedWriter.newLine();
bufferedWriter.write("figure value is : " + figure);
bufferedWriter.newLine();
bufferedWriter.write("agility value is : " + agility);
bufferedWriter.newLine();
bufferedWriter.write("intelligence value is : " + intelligence);
bufferedWriter.newLine();
// Always close files.
bufferedWriter.close();
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
Player p = new Player();
String s = p.readLine();
System.out.println(s);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher m = pattern.matcher(s);
while (m.find()) {
int n = Integer.parseInt(m.group());
arl.add(n);
}
p.NEWnaturalTalent = arl.indexOf(0);
p.NEWluck=arl.indexOf(1);
p.NEWfigure=arl.indexOf(2);
p.NEWagility=arl.indexOf(3);
}
}