Spliting file content into ArrayList - java
I have a file where I get to read it's content. Now I would like to split each seperate line into an arrayList individually and cant succeed.
This is what I have so far
try {
input = new FileInputStream(test);
byte testContent[] = new byte[(int) test.length()];
input.read(testContent);
String testFile = new String(testContent);
System.out.println(testFile);
}
An example of file content is as follows,
0,0,5,13,9,1,0,0,0,0,13,15,10,15,5,0,0,3,15,2,0,11,8,0,0,4,12,0,0,8,8,0,0,5,8,0
0,0,0,12,13,5,0,0,0,0,0,11,16,9,0,0,0,0,3,15,16,6,0,0,0,7,15,16,16,2,0,0,0,0,1,1
0,0,0,4,15,12,0,0,0,0,3,16,15,14,0,0,0,0,8,13,8,16,0,0,0,0,1,6,15,11,0,0,0,1,8,1
0,0,7,15,13,1,0,0,0,8,13,6,15,4,0,0,0,2,1,13,13,0,0,0,0,0,2,15,11,1,0,0,0,0,0,1
0,0,0,1,11,0,0,0,0,0,0,7,8,0,0,0,0,0,1,13,6,2,2,0,0,0,7,15,0,9,8,0,0,5,16,10,0,1
I would like the above to be in arrays like
[0,0,5,13,9,1,0,0,0,0,13,15,10,15,5,0,0,3,15,2,0,11,8,0,0,4,12,0,0,8,8,0,0,5,8,0]
[0,0,0,12,13,5,0,0,0,0,0,11,16,9,0,0,0,0,3,15,16,6,0,0,0,7,15,16,16,2,0,0,0,0,1,1]
[0,0,0,4,15,12,0,0,0,0,3,16,15,14,0,0,0,0,8,13,8,16,0,0,0,0,1,6,15,11,0,0,0,1,8,1]
[0,0,7,15,13,1,0,0,0,8,13,6,15,4,0,0,0,2,1,13,13,0,0,0,0,0,2,15,11,1,0,0,0,0,0,1]
[0,0,0,1,11,0,0,0,0,0,0,7,8,0,0,0,0,0,1,13,6,2,2,0,0,0,7,15,0,9,8,0,0,5,16,10,0,1]
Thanks in advance for any help
How about Files.readAllLines():
List<String> lines = Files.readAllLines(new File(test).toPath());
If you're using Java 7, you'll need this version:
List<String> lines = Files.readAllLines(new File(test).toPath(), StandardCharsets.UTF_8);
String [] lines = testFile.split("\\r?\\n");
Use String#split()
String[] strings= testFile.split("\\r?\\n");
List<String> list = Arrays.asList(strings);
You can have more details about this method from this question
How to split a string in Java
You can try something like this:
BufferedReader br = new BufferedReader(new FileReader(yourFile));
List<String[]> listOfArrays = new ArrayList<String[]>();
String nextLine;
while((nextLine = br.readLine()) != null){
String[] lineArray = nextLine.split(",");
listOfArrays.add(lineArray);
}
Related
Trying to separate a txt file into two ArrayLists?
This image is a text file I need to separate by date and the digits alongside it BufferedReader wordReader = new BufferedReader(new FileReader("\\Users\\rosha\\eclipse-workspace\\working\\src\\workingfix\\spx_data_five_years.txt")); ArrayList<String> spxIndex = new ArrayList<>(); ArrayList<String> date = new ArrayList<>(); //populating the Array with the file String line = wordReader.readLine(); while (line != null) { date.add(line); line = wordReader.readLine(); } wordReader.close(); Would really love to understand how to separate this file into two Arrays. Been at it for a while and some Guidance in the right direction would be incredible. Apologies if it's a simple solution for some reason I'm having trouble getting started. Here is the some of the Text File, if I can get guidance on this I'll be in good shape 1/4/2010 1132.99 1/5/2010 1136.52 1/6/2010 1137.14 1/7/2010 1141.69 1/8/2010 1144.98 1/11/2010 1146.98 1/12/2010 1136.22 1/13/2010 1145.68
Your two examples are a little bit different, in the Image, it seems like the entries are separated by a tab. In your text example, the entries are separated by a space. If they are separated by a space, a simple String[] splitter = line.split(" "); suffices. This gives you the result as an Array, which you can write in the ArrayLists.
Here is the solution, using split method public static void main(String[] args) throws IOException { ArrayList<String> spxIndex = new ArrayList<>(); ArrayList<String> date = new ArrayList<>(); String sCurrentLine; BufferedReader br = new BufferedReader(new FileReader("\\Users\\rosha\\eclipse-workspace\\working\\src\\workingfix\\spx_data_five_years.txt")); while ((sCurrentLine = br.readLine()) != null) { String[] lineValues = sCurrentLine.split(" "); date.add(lineValues[0]); spxIndex.add(lineValues[1]); } br.close(); System.out.println(date); System.out.println(spxIndex); }
how to save contents of a file as single string using java
I am trying to extract a specific content from text file by using delimiters. This is my code : File file = new File("C:\\Inputfiles\\message.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String st; while ((st=br.readLine()) != null) { String[] strings = StringUtils.split(st, "------------"); System.out.println(strings);} But as a result, each and everyline is getting splitted by delimiter and saved as array. Can anyone suggest how I can save the contents from file as a single string, so I can get limited number of lines only as Array.
You can use StringBuilder or StringBuffer to do that. File file = new File("C:\\Inputfiles\\message.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String st; while ((st=br.readLine()) != null) { String[] strings = StringUtils.split(st, "------------"); StringBuilder singleString = new StringBuilder(); for(String s : strings){ singleString.append(s); } System.out.println(singleString.toString()); }
Thanks All, I did using below changes String contents = new String(Files.readAllBytes(Paths.get("C:\\Inputfiles\\message1.txt"))); String[] splitted = StringUtils.split(contents, "-------"); for(int i=0;i<splitted.length;i++) System.out.println(splitted[i]);
ArrayList to Array
How do I convert this ArrayList's value into an array? So it can look like, String[] textfile = ... ; The values are Strings (words in the text file), and there are more than a 1000 words. In this case I cannot do the, words.add("") 1000 times. How can I then put this list into an array? public static void main(String[]args) throws IOException { Scanner scan = new Scanner(System.in); String stringSearch = scan.nextLine(); List<String> words = new ArrayList<String>(); //convert to array BufferedReader reader = new BufferedReader(new FileReader("File1.txt")); String line; while ((line = reader.readLine()) != null) { words.add(line); }
You can use String[] textfile = words.toArray(new String[words.size()]); Relevant Documentation List#toArray(T[])
words.toArray() should work fine. List<String> words = new ArrayList<String>(); String[] wordsArray = (String[]) words.toArray();
you can use the toArray method of Collection such as shown here Collection toArray example
List<String> words = new ArrayList<String>(); words.add("w1"); words.add("w2"); String[] textfile = new String[words.size()]; textfile = words.toArray(textfile);
Text processing in java android
I have a file that looks similar to this 12345 one 12345 two 12345 three ....... Question is how can i get all of the values from second row and store them in a String[] i know how to read file in java just dont know how to cut the second row
1. Store Each line from the file into an ArrayList<String>, its more flexible than String[] array. 2. Then access the line you need by get() method of ArrayList Eg: ArraList<String> arr = new ArrayList<String>(); //Now add each lines into this arr ArrayList arr.get(1); // Getting the Second Line from the file `
You can split the file line by new line. String [] names = fileString.split("\n");
Ok this is what i did but it skips first line FileInputStream fstream = new FileInputStream("/sys.........."); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; strLine = br.readLine(); while ((strLine = br.readLine()) != null) { delims = strLine.split(" "); first = delims[1]; where.add(first); } in.close(); From example above it contains only "two" and "three"
How to read a String (file) to array in java
Suppose there is a file named as SUN.txt File contains : a,b,dd,ss, I want to make dynamic array depending upon the number of attributes in file. If ther is a char after comma then array will be of 0-4 i.e of length 5. In the above mentioned case there is no Char which returns 0-3 Array of length 4. I want to read the NULL after comma too. How do i do that? Sundhas
You should think about Reading the file into a String Splitting the file by separator ',' Using a list for adding the characters and convert the list to an array, when the list is filled
As Markus said, you want to do something like this.. //Create a buffred reader so that you can read in the file BufferedReader reader = new BufferedReader(new FileReader(new File( "\\SUN.txt"))); //The StringBuffer will be used to create a string if your file has multiple lines StringBuffer sb = new StringBuffer(); String line; while((line = reader.readLine())!= null) { sb.append(line); } //We now split the line on the "," to get a string array of the values String [] store = sb.toString().split(","); I do not quite understand why you would want the NULL after the comma? I am assuming that you mean after the last comma you would like that to be null in your array? I do not quite see the point in that but that is not what the question is. If that is the case you wont read in a NULL, if after the comma there was a space, you could read that in. If you would like a NULL you would have to add it in yourself at the end so you could do something like //Create a buffred reader so that you can read in the file BufferedReader reader = new BufferedReader(new FileReader(new File( "\\SUN.txt"))); //Use an arraylist to store the values including nulls ArrayList<String> store = new ArrayList<String>(); String line; while((line = reader.readLine())!= null) { String [] splitLine = line.split(","); for(String x : splitLine) { store.add(line); } //This tests to see if the last character of the line is , and will add a null into the array list if(line.endsWith(",")) store.add(null); } String [] storeWithNull = store.toArray();
Well if you want want to simply open the file and store the content in a array of string then 1) open the file into a string 2) split the string using a regex "," http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String) but I'm curious why you can't use a String file directly ?
For your datatructure, use a list of arrays. Each list entry is a line of your textfile, each entry is an array that holds the comma separated values: List<String[]> data = new ArrayList<String[]>(); String line = readNextLine(); // custom method, to be implemented while (line != null) { data.add(line.split(",")); line = readNextLine(); } (assuming, your file contains 1..n lines of comma separated values) You may want to have it like this: "a,b,c,d," -> {"a", "b", "c", "d", null} Here's a suggestion how to solve that problem: List<String[]> data = new ArrayList<String[]>(); String line = readNextLine(); // custom method, to be implemented while (line != null) { String[] values = new String[5]; String[] pieces = line.split(","); for (int i = 0; i<pieces.length; i++) values[i] = pieces[i]; data.add(values); line = readNextLine(); }
its seems like a CSV file something like this will work assuming it has 5 lines and 5 values String [][] value = new String [5][5]; File file = new File("SUN.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String line = null; int row = 0; int col = 0; while((line = br.readLine()) != null ){ StringTokenizer s = new StringTokenizer(line,","); while (s.hasMoreTokens()){ value[row][col] = s.nextToken(); col++; } col = 0; row++; } i havent tested this code
Read the file, using BufferedReader, one line at the time. Use split(",", -1) to convert to an array of String[] including also empty strings beyond the last comma as part of your array. Load the String[] parts into a List.