从.text到对象表读取行

时间:2014-12-10 06:21:18

标签: java object methods text-files

我需要帮助;我想要做的是将文本文件的每一行放入一个对象表中。 让我们举例:

我有那个文本文件:

ATI
8
10
AMX
12
15

正如我们所看到的,这个文本文件包含不同类型的主板。

所以我有这个构造函数(在另一个类中)的主板对象:

motherBoard(String type, int size, int max_size){
this.type = type;
this.size = size;
this.max_size = max_size;
}

所以我要做的就是创建一个对象表,在这个表中(在这个例子中)表的第一个对象将包含文本文件的3个第一行。然后,表的第二个对象将包含接下来的3行......

我想要做的所有操作都在一个方法中...... 到目前为止,这是我的代码:

public static CarteMere[] chargerEnMemoire(String nomFichier) throws IOException{
    CarteMere[] newCarteMere = new CarteMere[0];
    try {
        FileReader reader = new FileReader( "liste.txt" );
        BufferedReader buffer = new BufferedReader( reader );
        int nmLines = 0;
        while ( buffer.ready() ) {
            nmLines++; // To evaluate the size of the table
        }
        buffer.close();
        newCarteMere = new CarteMere[nmLines/3];
        for(int i=0; i<(nmLines/3);i++){

        }
    } catch ( FileNotFoundException e ) {
        System.out.println( e.getMessage() );
    } catch ( IOException e ) {
        System.out.println( e.getMessage() );
    }

    return newCarteMere;
}

我需要推动的地方......

4 个答案:

答案 0 :(得分:0)

从你所知道的开始,你有一个文件,它包含三行一组的数据,一个是String,另外两个是数字。

你有一个代表那些数据的类,在可能的情况下总是更容易将数据存储在一个对象中,所以你可以做类似的事情......

try (BufferedReader br = new BufferedReader(new FileReader("liste.txt"))) {
    String name = br.readLine();
    int size = Integer.parseInt(br.readLine());
    int max = Integer.parseInt(br.readLine());

    Motherboard mb = new MotherBoard(name, size, max);
    // Add it to a List or what ever else you want to do it...
} catch (IOException exp) {
    exp.printStackTrace();
}

答案 1 :(得分:0)

如果你的pojo类中有所有3个变量的getter和setter,并且你在motherBoard类中重载了toString()方法,

Integer noOfLines=0, i=0, j=0;
String [] data=null;
String sCurrentLine=null;
BufferedReader br = new BufferedReader(new FileReader("liste.txt"));
while ((sCurrentLine = br.readLine()) != null) 
{
    noOfLines++;
    data[i]=sCurrentLine;
} 
i=-1;
j=noOfLines/3;
motherBoard mb=null;
for(int x=0;x<=j;x++)
{
    mb=new motherBoard(data[++i], Integer.parseInt(data[++i]), Integer.parseInt(data[++i]))
    System.out.println(mb);
}

答案 2 :(得分:0)

关于只读取文件中的数据并将其加载到类中,希望这个例子可以帮助您:

<强> MotherBoard.java

public class MotherBoard {
    String type;
    int size;
    int max_size;

    public MotherBoard (String type, int size, int max_size) {
        this.type = type;
        this.size = size;
        this.max_size = max_size;
    }

    public String toString() {
        return "Motherboard data : type=" + type + " size=" + size + ", max_size=" + max_size;
    }
}

<强> Solution.java:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Solution {
    public static void main(String[] args) {
        try {
            FileReader reader = new FileReader( "liste.txt" );
            BufferedReader buffer = new BufferedReader( reader );
            int nmLines = 0;
            int numMBs = 0;
            while (buffer.ready()) {
                numMBs++;
                /* Not a good approach if your data isn't perfect - just an example for StackOverflow */
                MotherBoard mb = new MotherBoard (buffer.readLine(),
                        Integer.parseInt(buffer.readLine()),
                        Integer.parseInt(buffer.readLine()));
                System.out.println("Motherboard " + numMBs + ":");
                System.out.println(mb.toString());
            }
            /* Probably want to do this in a finally block too */
            buffer.close();
            reader.close();
        } catch ( FileNotFoundException e ) {
            System.out.println( e.getMessage() );
        } catch ( IOException e ) {
            System.out.println( e.getMessage() );
        }
    }
}

编译并运行:

javac -cp . MotherBoard.java Solution.java 
java Solution

输出:

Motherboard 1:
Motherboard data : type=ATI size=8, max_size=10
Motherboard 2:
Motherboard data : type=AMX size=12, max_size=15

答案 3 :(得分:0)

所以我找到了一种方法......我想我找到了一个解决方案......

这是我的新代码:

public static CarteMere[] chargerEnMemoire(String nomFichier) throws IOException{
    CarteMere[] newCarteMere = new CarteMere[0];
    try {
        FileReader reader = new FileReader( nomFichier );
        BufferedReader buffer = new BufferedReader( reader );
        int nmLines = 0;
        while ( buffer.readLine() != null) {
            nmLines++; // To evaluate the size of the table
                        }
        buffer.close();
        reader.close();
        //RESET THE BUFFER AND READER TO GET ON TOP OF THE FILE
        FileReader reader2 = new FileReader( nomFichier );
        BufferedReader buffer2 = new BufferedReader( reader2 );
        newCarteMere = new CarteMere[nmLines/3];
        for(int i=0; i<(nmLines/3);i++){
            int forme = CarteMere.chaineFormeVersCode(buffer2.readLine());
            int mem_inst = Integer.parseInt(buffer2.readLine());
            int mem_max = Integer.parseInt(buffer2.readLine());
            newCarteMere[i] = new CarteMere(forme, mem_inst, mem_max);
        }
        buffer2.close();
        reader2.close();
    } catch ( FileNotFoundException e ) {
        System.out.println( e.getMessage() );
    } catch ( IOException e ) {
        System.out.println( e.getMessage() );
    }

    return newCarteMere;
}

但是重置缓冲区并重新像这样的想法对我来说似乎很难看......你们有没有想法做同样的sime但是更正确?

提前感谢