通过创建txt文件对象转换成Java中的数组 [英] Creating objects via txt file into an array in Java

查看:382
本文介绍了通过创建txt文件对象转换成Java中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成一个小程序。

我有一个文本文件(.txt)存储上,我已经有了对象不同的数据。

文件的结构是下一个(exemples data.txt中):


  • Sedane

  • 2005

  • 195000

  • 柴油

  • 蓝色

  • SUV

  • 2013

  • 34000

  • 燃料

  • 黑色

每个对象由真,我已经叫共建级车。
所以1行是汽车的类型,第二建年,第3行是milage,第四是燃料的种类,和第五行是汽车的颜色

所以basicly我需要打开该文件,将数据加载到内存当我执行我的程序到其与对象的数组。

我确定,打开该文件,但说到读取数据,并把它在一个阵列即时阻止。

数组大小是2本例,但如果我在文件中有更多的条目它会适应它的大小在程序启动时加载的时候。

下面就是我必须得unti(我的code ...)

 公共静态无效loadCars(){
    FOPEN的FileReader;
    打开的BufferedReader;
    串线;    尝试{
        fEntree =新的FileReader(data.txt中);
        打开=新的BufferedReader(FOPEN);
        而(opened.ready()){
            行= opened.readLine();
            //不知道这里做什么????
        }
        opened.close();
    }赶上(IOException异常五){
        的System.out.println(文件不存在!);
    }}


解决方案

  LineNumberReader LNR =新LineNumberReader(新的FileReader(新文件(文件1)));
lnr.skip(Long.MAX_VALUE);长长度= lnr.getLineNumber();lnr.close();在=新的BufferedReader(新的FileReader(data.txt中));车[] =汽车新车[长度/ 5];
串currentLine;
INT I = 0;的for(int i = 0; I<长/ 5; I + = 5){
    字符串名称= in.readLine();
    字符串年= in.readLine();
    串英里= in.readLine();
    串气= in.readLine();
    串色= in.readLine();
    汽车[I] =新车(名称,年份,里程,气体,颜色);
}

您将不得不处理异常也是如此,在环绕的东西尽量捕捉结构。

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

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

这篇关于通过创建txt文件对象转换成Java中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆