我正在为一个学校项目工作,我需要自己研究语法和与该项目有关的其他事物。
我已经有了批量代码,这个想法是对住在公寓楼,楼层和房间的人的索引。这还将节省水电的租金/支付。
我有一种方法可以将它们存储为多维数组。我不知道如何将代码复制到.txt文件中。
这是一个不使用数组的示例,例如
package buildings
public class main{
public static String name = "James";
public static int buildingName = 1;
public static int floor = 2;
public static int room = 1;
public static float rent = 250.99;
}
我如何将这些变量放入文本文件中?以后如何在再次运行程序时引用它们?
答案 0 :(得分:0)
使用Java 8实现它的方法如下。
public class WriteClass {
public static void main(String... args) {
Person jack = new Person("Jack", 1, 2, 1, 250.99);
List<String> list = new ArrayList<>();
list.add(jack.toString());
try {
Path path = Paths.get(Paths.get("").toAbsolutePath().toString().concat("/persons.txt"));
System.out.println(path.toAbsolutePath().toString());
Files.write(path, list);
} catch (IOException ignored) {
ignored.printStackTrace();
}
System.out.println(jack);
}
static class Person {
String name;
int buildingNum;
int floorNum;
int roomNum;
double rentCost;
public Person(String name, int buildingNum, int floorNum, int roomNum, double rentCost) {
this.name = name;
this.buildingNum = buildingNum;
this.floorNum = floorNum;
this.roomNum = roomNum;
this.rentCost = rentCost;
}
@Override
public String toString() {
return String.format("{name: %s, building No.: %d, floor No.: %d, room No.: %d, rent cost: %.2f",
name, buildingNum, floorNum, roomNum, rentCost);
}
}
}
文件的输出将为:
{name: Jack, building No.: 1, floor No.: 2, room No.: 1, rent cost: 250.99}
答案 1 :(得分:0)
这很简单,首先,您需要考虑一种用于存储变量的绝对格式。
在这种情况下,对于数组,我使用此!var1!var2!var3
,然后使用特殊字符来象征数组中的下一行,例如^
这样的2d数组
[0,0,0]
[1,2,3]
[4,5,6]
在文件0!0!0!^1!2!3!^4!5!6!
中看起来像这样,因此当您从包含此文件的文件中读取内容时
Scanner scanner = new Scanner( new File("src\\vars\\array1"));
String file = scanner.useDelimiter("\\A").next();
scanner.close();`
//you do
String[] arrays = file.split("^");`
//and then
String[][] test = new int[arrays.length][arrays.length];`
Assuming it's a square array, if
//not, just either remember how long it is or count throught the file first
for(int x = 0; x < arrays.length; x++)
test[x] = arrays[x].split("!");
//and then finally
int[][] finalArray = new int[test.length][test[0].length];
for(int x = 0; x < arrays.length; x++)
for(int y = 0; y < arrays.length; y++)
finalArray[x][y] = Integer.parseInt(test[x][y]);
并写入文件
File newfile = new File("src\\vars\\array1");
BufferedWriter out = new BufferedWriter(new FileWriter(newfile, true));
out.write("Print to the file using your format :)");`
对不起,整个代码放在括号里,堆栈编辑器生我的气。
答案 2 :(得分:0)
首先,在这种情况下,您应该考虑使用POJO(普通的旧Java对象)。将人员的每个属性存储为类的属性。
@Getter
@Setter
public class Person {
private String name;
private int buildingNumber;
private int floor;
private int room;
private double rent;
}
我会给你3个选择。
选项1
1)为每个人创建一个自定义字符串,该字符串存储由某些公共字符分隔的Person的所有属性。即
Person p = new Person("ABC",1, 4,2,1250);
String splitter = "*_*"
String deserializedString = p.getName() + splitter + p.getBuildingNumber() + splitter + p.getFloor() + splitter + p.getRoom() + splitter + p.getRent();
这将创建一个字符串,例如:
ABC*_*1*_*4*_*2*_*1250
2)在为所有人提供了字符串之后,将该列表存储在所需的文件中。
3)要从文件中读回,请一一读取文件中的所有行。
4)您只有一行,用*_*
分割字符串,这将为您提供String [],您可以使用它重新生成Person类。
选项2
在这种情况下,您可以将Java对象写入文件中,而不是将Person信息以纯文本格式存储在文件中。
为此,您将必须创建一个包装器类。
@Getter
@Setter
public class BuildingPeopleData {
List<Person> allPeople = new ArrayList<Person>();
}
存储以上列表中所有人员的信息。
public static void main(String[] args) {
BuildingPeopleData data = new BuildingPeopleData();
data.setAllPeople(listOfPeople) // create listOfPeople list first. I have intentionally not explained that here.
// Code to Write data object in file
// Code to read data back from file
// Example tutorial: https://www.mkyong.com/java/how-to-write-an-object-to-file-in-java/
}
选项3
使用Jackson之类的第三方库。 Jackson库允许我们将Java Pojo转换为String,它将以JSON格式表示您的数据,如下所示:
{"Name":"ABC","BuildingNumber":1,"Floor":4,"room":2,"rent":1250}
将这些字符串存储在文件中。
当您想读回它们时,以字符串形式从文件中读取它们。
然后使用Jackson库将json字符串转换为Person类的实例。