java 数组生成txt,通过创建txt文件对象转换成Java中的数组

I am trying to complete a little program.

I've got a text file (.txt) to store different data on objects that i've got.

The structure of the file is the next (exemples data.txt) :

Sedane

2005

195000

Diesel

Blue

SUV

2013

34000

Fuel

Black

Each object is made true a class that i've build called Cars.

So the 1 line is the type of car, the 2nd the year of built, the 3rd line is the milage, the 4th is the type of fuel, and the 5th line is the color of the car.

So basicly i need to open the file, and load the data into the memory when i execute my program into an array with object in it.

I'm ok to open the file but i'm blocked when it comes to reading the data and putting it in an array.

The array size is 2 for this exemple, but if i have more entries in the file it's going to adapt it's size when loading at the startup of the program.

Here's what i've got unti now (for my code ...)

public static void loadCars () {

FileReader fopen;

BufferedReader opened;

String line;

try {

fEntree = new FileReader( "data.txt" );

opened = new BufferedReader( fopen );

while ( opened.ready() ) {

line = opened.readLine();

// Don't know what to do here ????

}

opened.close();

} catch ( IOException e ) {

System.out.println( "File doesn't exist !" );

}

}

解决方案

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

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);

}

You'll have to handle exceptions too, surround stuff in try catch structures.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值