user2914870
user2914870

Reputation: 1

How to copy data from txt file to array

I am doing a project that requires me to copy all the information from txt file into array. The content of the txt is listed below. What i want here is that i want to grab all the names of goods and description into its arrays respectively.

GoodTitle        Description            
    Gold       The shiny stuff
    Wheat   What wheaties are made of
    Wood    To make more ships
    Spices  To disguise the taste of rotten food
    Tobacco Smoko time
    Coal    To make them steam ships go
    Coffee  Wakes you up
    Tea Calms you down

What I have done so far:

public void openFile()
    {
        ArrayList <String> ShippingTokens = new ArrayList<String>();
        try{
            FileInputStream fstream = new FileInputStream("D://Shipping.txt");
            // Use DataInputStream to read binary NOT text.
                    BufferedReader br = new BufferedReader (new InputStreamReader (fstream));
            String strline;
            while ((strline = br.readLine()) != null){ 
                strline = strline.trim();

            if ((strline.length()!=0)) {
                String[] Shippings = strline.split("//s+");
                ShippingTokens.add(Shippings[TOKEN_COLUMN]);
            }

        }

        for (String s : ShippingTokens) {
            System.out.println(s);
        }

        in.close();
        } catch (Exception e){
            System.err.println("Error");
        }

    }

Upvotes: 0

Views: 2526

Answers (3)

Petr Mensik
Petr Mensik

Reputation: 27496

First of all, Java SE 7 really simplified working with files, so I suggest you using it.

try(BufferedReader reader = Files.newBufferedReader(Paths.get("D://Shipping.txt"), StandardCharsets.UTF_8)) { 
    List<String> lines = Files.readAllLines(reader, StandardCharsets.UTF_8);
    for(String line : lines) {
         String[] shipping = line.split("//s+");
         //now do whatever you want with the values
    }
}

Upvotes: 1

oalsing
oalsing

Reputation: 100

One possible solution is to first read the first word of the string, and store that in the array. Then you would probably want to store the rest of the line, which could be done in various way. One possible solution is to read the whole line, store the first word as the GoodTitle, remove the first word and store the rest of the string as the description, I am not quite sure how BufferedReaders work in Java.

You might want to consider using a Scanner instead. http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Which comes with methods such as next() and nextLine(), which probably would ease your solution.

Are you sure that you want to store this in an array? It would probably make more sense to store the information in a HashMap with the GoodTitle as the key.

Upvotes: 0

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

Please ensure you defined TOKEN_COLUMN in your class.

 ShippingTokens.add(Shippings[TOKEN_COLUMN]);

if your TOKEN_COLUMN value is 0 then your output will be as below.

GoodTitle   
Gold
Wheat
Wood
Spices
Tobacco
Coal
Coffee
Tea

Upvotes: 0

Related Questions