How to Effortlessly Process Text Files One by One

It’s not so inconvenient to write a text file to an Excel file to process it. But you need a special program to process multiple text files. Programming languages have their own ways to do this. Certain languages take very roundabout ways.

Here’s a simple task. There are text files. Each consists of numbers separated by spaces and they have different row counts and column counts. To write a repeatedly executable algorithm to calculate the average of all numbers in a specified file each time, JAVA produces the following code:

import   java.io.BufferedReader;

import java.io.IOException;

import java.io.FileReader;

import javax.swing.JFileChooser;

import   javax.swing.JOptionPane;

import java.util.ArrayList;

 

public class Week04 {

public static void  main(String[] args) throws IOException {

String theFile;

double theAverage;

theAverage =  getTheAverage(theFile);

displayTheResult(theAverage,"The   average is;");

}

 

public static String  getTheFileName() {

String theFile;

JFileChooser jfc = new  JFileChooser();

jfc.showOpenDialog(null);

 return theFile =  jfc.getSelectedFile().getAbsolutePath();

}

 

public static double getTheAverage(String  s) throws IOException {

double value = 0,  numValues = 0;

FileReader fr = new  FileReader(s);

BufferedReader br = new  BufferedReader(fr);

String aLine;

while ((aLine =  br.readLine()) !=   null) {

String nums[] = aLine.split("");

For (int i=0;i<nums.length;i++){

value +=  Double.parseDouble(nums[i]);  

numValues++;

}

}

if (numValues > 1) {

return  value/numValues;

} else {

return value;

}

 

}

public static void  displayTheResult(double x, String s){

JOptionPane.showMessageDialog(null,s + x);

 }

}

 

It’s horrible. Almost same effect if you get it done in C and BASIC. Python is simpler yet adopts a same way of thinking. Besides, they all require complicated configurations.

The coding would turn effortless and smooth if you choose to do the task with esProc. Only several lines are enough:


A

B

1

=file(pFile).import@i()

/ Read in a file through   parameter pFile that passed in d:\fo.txt,   for example

2

=A1.(~.array(" "))

/ Split each line into   numbers by spaces

3

=A2.conj()

/ Concatenate all numbers

4

=A3.avg(float(~))

/ Convert numbers into   floating-point type and calculate average

 

esProc is a scripting language that allows repeated execution on IDE and being called from the command line.


More examples can be found in CSV File Comparison.
Read HERE to find an easy way to merge and summarize Excel files.
Read Structured Text Computations with esProc learn esProc methods of structured text processing in different scenarios.

esProc is the file processor tool that is ready to use, simple to configure and powerful to process. See Data File Processor.
Read Getting Started with esProc to download and install esProc, get a license for free and find related documentation.