在Java中通过txt文件将对象创建到数组中
我正在努力完成一个小程序 我有一个文本文件(.txt)来存储我得到的对象上的不同数据 文件的结构如下(例如data.txt):在Java中通过txt文件将对象创建到数组中,java,arrays,object,Java,Arrays,Object,我正在努力完成一个小程序 我有一个文本文件(.txt)来存储我得到的对象上的不同数据 文件的结构如下(例如data.txt): 轿车 2005年 195000 柴油机 蓝色的 越野车 2013年 34000 燃料 黑色的 每个对象都是我构建的一个名为Cars的类。 因此,1号线是汽车类型,2号线是制造年份,3号线是milage,4号线是燃料类型,5号线是汽车颜色 因此,基本上我需要打开文件,并在将程序执行到包含对象的数组中时将数据加载到内存中 我可以打开文件,但在读取数据并将其放入数组时,我
- 轿车
- 2005年
- 195000
- 柴油机
- 蓝色的
- 越野车
- 2013年
- 34000
- 燃料
- 黑色的
LineNumberReader lnr=newlinenumberreader(新文件读取器(新文件(“文件1”)));
lnr.skip(长最大值);
long length=lnr.getLineNumber();
lnr.close();
in=新的BufferedReader(新的文件读取器(“data.txt”);
车[]辆=新车[长度/5];
串电流线;
int i=0;
对于(int i=0;i您可以在下面查看我的解决方案(我还纠正/简化了读取文件的变量的一些问题,无论如何,这不是主要主题):
但我不得不说,这可能有点“太静态”。
为了简单起见,我假设您的所有字段都是字符串类型,但像“year”或“milage”这样的字段可能是int类型。在这种情况下,您可以使用Object[](而不是String[])数组,然后使用正确的类型强制转换值
我希望这可能会对您有所帮助。类似这样的操作就可以了。我正在将文件内容逐行添加到Arraylist中,而不是数组中。这样一来,您就不必事先知道数组需要有多大。另外,您可以随时将其更改为数组
public ArrayList<String> readFileToMemory(String filepath)
{
in = new BufferedReader(new FileReader( "data.txt" ));
String currentLine = null;
ArrayList<String> fileContents = new ArrayList<String>();
try
{
while((currentLine = in.readLine()) != null)
{
fileContents.add(currentLine);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
return fileContents;
}
public ArrayList readfiletomore(字符串文件路径)
{
in=新的BufferedReader(新的文件读取器(“data.txt”);
字符串currentLine=null;
ArrayList fileContents=新的ArrayList();
尝试
{
而((currentLine=in.readLine())!=null)
{
fileContents.add(currentLine);
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
最后
{
尝试
{
in.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
返回文件内容;
}
Hi there Anubian Noob,Ty用于回复。数组上的每个点都是一个对象……比如Cars[]arrayCars=新车[??]@Cyberflow欢迎来到StackOverflow!不要发表这样的非建设性评论。而是发表建设性的批评。这回答了你的问题吗?你有任何后续问题吗?我如何检查设置我在数组上创建了多少个点的行数?哦,对不起,我没有阅读你的部分问题,正在编辑。ArrayLis是吗ts ok或者你必须使用数组吗?试试这个:嗨,Nicolas,你的答案非常接近我想做的…但是我需要用txt文件中的car数创建一个数组(通过使用模5知道的数量,就像你在if中做的那样)是的,今年有INT类型,mileagei如果您采用我的解决方案,您可以通过以下操作创建数组:Car[]carArray=carList.toArray(new Car[carList.size());在我的代码末尾
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("File1")));
lnr.skip(Long.MAX_VALUE);
long length = lnr.getLineNumber();
lnr.close();
in = new BufferedReader(new FileReader( "data.txt" ));
Car[] cars= new Car[length/5];
String currentLine;
int i=0;
for(int i=0;i<length/5;i+=5) {
String name = in.readLine();
String year = in.readLine();
String miles = in.readLine();
String gas = in.readLine();
String color = in.readLine();
cars[i] = new Car(name,year,miles,gas,color);
}
public static void loadCars() {
FileReader fopen;
BufferedReader opened;
String line;
ArrayList<Car> carList = new ArrayList<Car>();
try {
fopen = new FileReader("data.txt");
opened = new BufferedReader(fopen);
int nFields = 5; // we have 5 fields in the Car class
String[] fields = new String[nFields]; // to temporary store fields values read line by line
int lineCounter = 0;
while ((line = opened.readLine()) != null) {
fields[lineCounter] = line;
lineCounter++;
if ((lineCounter) % nFields == 0) { //it means we have all 5 fields values for a car
carList.add(new Car(fields)); //therefore we create a new car and we add it to the list of cars
}
}
opened.close();
} catch (IOException e) {
System.out.println("File doesn't exist !");
}
}
class Car {
private String type;
private String year;
private String milage;
private String fuel;
private String color;
public Car(String[] fields) {
type=fields[0];
year=fields[0];
milage=fields[0];
fuel=fields[0];
type=fields[0];
}
}
public ArrayList<String> readFileToMemory(String filepath)
{
in = new BufferedReader(new FileReader( "data.txt" ));
String currentLine = null;
ArrayList<String> fileContents = new ArrayList<String>();
try
{
while((currentLine = in.readLine()) != null)
{
fileContents.add(currentLine);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
return fileContents;
}