-6

Hi Have a file called read.txt and below is the data inside file.

OS:B,A,Linux,Windows 7,Windows     
ARCH:32 Bit,64 Bit    
Browser:Chrome,Firefox,IE   

I want to read the file and want to store the data into String array for each column by spiting with
":" symbol.

example is below

String a[] = { "A","B","Linux", "Windows 7", "Windows" };    

String b[] = { "32 Bit", "64 Bit"};    

String c[] = { "Chrome", "Firefox" ,"IE"};  
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 5
    Great, please code it:) – ajay.patel Sep 12 '14 at 12:34
  • Do you know google? It is a great search engine to find answers. The first result when I googled it gave me a perfect example http://stackoverflow.com/questions/19844649/java-read-file-and-store-text-in-an-array. – Matheno Sep 12 '14 at 12:37
  • Do you have any specific problem with your code (you have your code, right)? – Pshemo Sep 12 '14 at 12:38
  • Please refer [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) And [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Aniket Kulkarni Sep 12 '14 at 12:40

3 Answers3

5

A way would be to extract eachline through ReadLine. Once we have a string containing the line, split the line assuming that we have a single ":" as delimiter. Extract the 2nd element of the array and do another split using "," as delimiter

SamDJava
  • 267
  • 3
  • 13
0

Using apache commons io...

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;

public class StackOverflowExample {
    public static void main(String[] args) throws IOException{
        List<String> lines = FileUtils.readLines(null, "UTF-8");
        List<String[]> outLines = new ArrayList<String[]>();
        for(int i = 0; i < lines.size(); i++){
            String line = lines.get(i);
            outLines.add(line.split("[:,]"));

        }
   }
}

As has already been pointed out - you really should include an example of the code you are using that isn't doing what you expect it to do. If you really have no idea how to do it at all and have no code - I'm not sure this will help anyway.

Jason Palmer
  • 99
  • 2
  • 8
-1

Here's how you read a file:

BufferedReader reader = new BufferedReader("read.txt");
while((line = reader.readLine()) != null)
{
    //process line
}

So to receive the result you want:

ArrayList<String[]> arrays = new ArrayList<String[]>;
BufferedReader reader = new BufferedReader("read.txt");
while((line = reader.readLine()) != null)
{
    //process line
    line = line.split(":")[1];//get the second part
    arrays.add(line.split(","));//split at "," and save into the ArrayList
}
Geosearchef
  • 600
  • 1
  • 5
  • 22