Break text file into columns Java

Total
0
Shares
break text file into columns java

Introduction

To break text file into columns in Java you can use the split() method. There are several methods to get there but we suggest either the string.split() method, StringTokenizer or the Pattern.compile() methods.

Getting Started

  1. I believe you have a java application set up. For this demonstration, I will be using a set-up by maven.
  2. Then you should set up a ClassPathResource which reads text files using the default character encoding of the operating system.
  3. Use Scanner to read a text file line by line

Therefore, you can apply either method to break your text file into columns.  

Using String.split()

The string split() method breaks a given string around matches of the given regular expression. There are two variants of the split() method in Java:

1. public String split(String regex)

This method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. By default, the limit is 0.

The parameter for this is regex (a delimiting regular expression).

It returns an array of strings calculated by splitting the given string.

2. public String split(String regex, int limit)

Parameters for this are regex (the delimiting regular expression) and limit (controls the number of times the pattern is applied and therefore affects the length of the resulting array).

This returns the array of strings counted by splitting this string around matches of the given regular expression.

Using StringTokenizer

In Java, the string tokenizer allows breaking a string into tokens. You can also get the number of tokens inside the string object. You can specify the delimiter that is used to split the string. In the above example, we have set the delimiter as space (”).

It is also possible to use StringTokenizer with multiple delimiters.

Using Pattern.compile ()

This method splits the given input sequence around matches of the pattern. The parameter for this is input – the character sequence to be split. It returns the array of strings computed by splitting the input around matches of the pattern.

Code Example

//Using Different split methods examples 

package com.example.demoz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.StringTokenizer;

@SpringBootApplication
public class DemozApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemozApplication.class, args);

		// using string.split()
		System.out.println("Hello guys, Today we learn about break text file into columns java using method split()");
		String str = " Hello guys : Today we learn about : break text file into columns java : using method split()";
		String[] arrOfStr = str.split(":");
		for (String a : arrOfStr)
			System.out.println(a);

		System.out.println("How are you doing today?");

		//using public String split(String regex, int limit)
		String string = "How are you doing today?";
		String split[] = string.split(" ", 0);
		for (String s : split)
			System.out.println(s);



		//using string tokenizer

		StringTokenizer st =
				new StringTokenizer("A StringTokenizer example");

		// get how many tokens are inside st object
		System.out.println("Tokens count: " + st.countTokens());

		// iterate st object to get more tokens from it
		while (st.hasMoreElements()) {
			String token = st.nextElement().toString();
			System.out.println("Token = " + token);
		}
	}
}

4,100 100,100 100,100 50,60
//Reading File Contents and Using string.split() method to split contents
{
			File resource = new ClassPathResource(
					"myText.txt").getFile();
			Scanner file=new Scanner(resource);
			String[] split=file.nextLine().split(",");
			for (String a : split)
				//After splitting file columns using the ',' character
				System.out.println(a);
		}

Results

//Using Different Split methods code Results
Hello guys, Today we learn about break text file into columns java using method split()
 Hello guys 
 Today we learn about 
 break text file into columns java 
 using method split()
How are you doing today?
How
are
you
doing
today?
Tokens count: 3
Token = A
Token = StringTokenizer
Token = example

Process finished with exit code 0
//File contents split results using ',' character
4
100 100
100 100
100 50
60


Process finished with exit code 0

Live Demo

A repository of the code above is available in this live demo application. For it to run you have to clone the files to a Java IDE.

Open Live Demo