How to create directories in Java

In java you can create a directory (or directories) using the standard IO package or using the java NIO package.

Create directories using the java IO package

// create a single directory
new File("c:\\myDir").mkdir();

// create a directory and all its sub-directories
new File("c:\\myRootDir\\Subdir1\\SubDir2").mkdirs();

 

Create directories using the java NIO package

This will work only on Java 7 and above.

Path path = Paths.get("C:\\Mydir");
Files.createDirectories(path);

You have to import the classes java.nio.file.Paths and java.nio.file.Files .

 

 

 

How to access to class private members in Java

It is possible to use to access class private instance variables and methods using Reflexion.

Access to private instance variables

MyClass myClass = new MyClass();
Field field = MyClass.class.getDeclaredField("myField");
field.setAccessible(true);
Object value = field.get(myClass);
System.out.println(value);

 

Access to private methods

MyClass myClass = new MyClass();
Method method = myClass.getClass().getDeclaredMethod("myMethodName");
method.setAccessible(true);
Object o = method.invoke(object);

 

How to get current TimeStamp in Java

There are several ways to get the current timestamp in Java. This example works in Java6, 7 and 8.

Please note that Java 8 contains now a simpler and more efficient date and time API.

// Using the java.sql.Timestamp class
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);

// Using the Date object
Date date = new Date();
System.out.println(new Timestamp(date.getTime()));

 

Spring Batch Tutorial

What is Spring Batch

Spring Batch is an open-source lightweight and comprehensive batch framework designed to develop batch applications that can process high volume of data.

Spring Batch provides the necessary classes and APIs supporting the following items:

  • Transaction management
  • Chunk based processing
  • Declarative I/O to read and write resources
  • Start/Stop/Restart
  • Retry/Skip
  • Web based administration interface (With Spring Batch Admin)

Spring Batch can execute a sequence of Jobs. A job consists of many steps that can be chained together. There are two types of steps:

  1. READ-PROCESS-WRITE steps : They consist of reading data for a source (Database, File, etc), process that data and finally write it to a resources (Database, File, etc).
  2. Tasklets or Single Task steps: They consis of sigle tasks, for example: deleting temporary files after the execution of the other steps.

Examples of tasks

Here are some examples of use of Spring Batch Tasks and the convenient type to use with each task:

  • READ-WRITE-PROCESS : Read data from MySQL database, process it and write it to CSV file.
  • READ-WRITE-PROCESS: Read Data from files in Folder A, process it, and write data to folder B .
  • TASKLET : Send newsletter to subscribers.
  • TASKLET : Clean up a folder.

How to define jobs and tasks

In Spring Batch we can use Annotations or Config files to define jobs and tasks sequence.

Here is an example of configuration using XML :

<job id="myJob" xmlns="http://www.springframework.org/schema/batch">
 <step id="step1" next="step2">
 <tasklet>
  <chunk reader="txtItemReader" processor="txtItemProcesser" writer="txtItemWriter"  commit-interval="1" />
 </tasklet>
 </step>
 <step id="step2" next="step3">
   <tasklet>
    <chunk reader="txtItemReader" writer="dbItemWriter" processor="dbItemProcesser" commit-interval="10" />
    </tasklet>
 </step>
 <step id="step3">
    <tasklet ref="sendReportEmail" />
 </step>
</job>

 

Use case example

In this example, we will use Spring Batch to read data from a MySql database, process it , and finally write it to a CSV file.

For this tutorial, we will be using the following versions :

  • spring-core : 4.3.10.RELEASE, Spring framework core
  • spring-batch-core: 3.0.8.RELEASE, Spring Batch core
  • mysql-connector-java: 5.1.22 : Contains My SQL driver and other utility classes
  • spring-jdbc: 4.3.10.RELEASE, to support the communication between Spring Batch and MySQL
  • Eclipse EDI: Neon 4.6.2
  • Maven :  Apache Maven 3.0.5

the pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.tutoref</groupId>
  <artifactId>spring-batch-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-batch-example</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
   <!-- Spring core -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>4.3.10.RELEASE</version>
   </dependency>
   <!-- Spring batch core -->
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>3.0.8.RELEASE</version>
    </dependency>
    <!-- MySQL connector -->
    <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.22</version>
   </dependency>
   <!-- Spring jdbc -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>4.3.10.RELEASE</version>
   </dependency>
  </dependencies>
</project>

 

Database structure and data

For the example we will create a products table and populate it with some data using the following script.

CREATE TABLE `product` (
  `id` int(11) NOT NULL,
  `name` varchar(64) NOT NULL,
  `unit_price` double NOT NULL,
  `quantity` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `product` (`id`, `name`, `unit_price`, `quantity`) VALUES
(1, 'Phone', 300, 50),
(2, 'Tablet', 250, 20),
(3, 'Mouse', 20, 30),
(4, 'Keyboard', 20, 30),
(5, 'Speaker', 40, 30),
(6, 'Screen', 300, 40);

 

The Project XML Configuration

As mentioned before, we can do the same configuration using Annotations. for this example, we will be using xml files to configure Spring Batch.

XML configuration can be written into the same file, however it is a good practice to separate them (Spring context, Data sources and jobs description ).

The datasource.xml file

This file contains the information of the datasource used to comunicate with the database.

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/jdbc
  http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
  
 <bean id="productDataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/tutoref" />
  <property name="username" value="root" />
  <property name="password" value="replaceWithYours" />
 </bean>

 <bean id="transactionManager"
  class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

</beans>
The spring-context.xml file

This file contains the Spring context configuration.

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

 <!-- Job Repository, Job Launcher and transactionManager configuration -->
 <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />
  
    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>

</beans>
The job-products.xml file

This file contains the jobs and tasks definition. It does also include the two previous xml files.

 

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:batch="http://www.springframework.org/schema/batch"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/batch
  http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 ">
 
 <import resource="spring-context.xml" />
 <import resource="datasource.xml" />

 <bean id="product" class="com.tutoref.batch.entity.Product" scope="prototype" />
 <bean id="itemProcessor" class="com.tutoref.batch.ProductItemProcessor" />
 <bean id="jobListener" class="com.tutoref.batch.ProductJobListener" />
 
 
    <!-- Reading from the database and returning a mapper row -->
    <bean id="productItemReader"
        class="org.springframework.batch.item.database.JdbcCursorItemReader">
        <property name="dataSource" ref="productDataSource" />
        <property name="sql" value="SELECT * FROM product" />
        <property name="rowMapper">
            <bean class="com.tutoref.batch.ProductMapper" />
        </property>

    </bean>
    
    <!-- Writing a line into an output flat file -->
    <bean id="productFlatFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
        <property name="resource" value="file:csv/products.csv" />
   <!-- Converting a product object into delimited list of strings -->
        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="delimiter" value="|" />
                <property name="fieldExtractor">
                    <!-- Returning the value of beans property using reflection -->
                    <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <property name="names" value="name,unitPrice,quantity" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
    
    <!-- And finally ... the job definition -->
    <batch:job id="productJob">
        <batch:step id="step1">
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="productItemReader" writer="productFlatFileItemWriter"
                    processor="itemProcessor" commit-interval="10" />
            </batch:tasklet>
        </batch:step>
        <batch:listeners>
            <batch:listener ref="jobListener" />
        </batch:listeners>
    </batch:job>
</beans>

The file declares also all the beans used by the jobs.

  • product: A reference to the Product Transfer/Value Bean.
  • itemProcessor: A Reference to class responsible for processing items
  • jobListener : A Refernece to the class responsible for listening to job processing events.
  • productItemReader: A reference to the class responsible for reading data from the database.
  • productFlatFileItemWriter: A reference to the class responsible for writing data to csv files.

The xml above make references to the following classes we created for this example.

Project source code

The Product Bean

Represents a product

package com.tutoref.batch.entity;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * The Product Bean
 * 
 * @author tutoref
 *
 */

public class Product {

 private int id;
 private String name;
 private double unitPrice;
 private int quantity;
 
 public int getId() {
  return id;
 }
 
 public void setId(int id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public double getUnitPrice() {
  return unitPrice;
 }
 
 public void setUnitPrice(double unitPrice) {
  this.unitPrice = unitPrice;
 }
 
 public int getQuantity() {
  return quantity;
 }
 
 public void setQuantity(int quantity) {
  this.quantity = quantity;
 }
 
 @Override
 public String toString() {
  return "[" + this.id + " | "+this.name+"]";
 }
}
The Product mapper

This class must implement the RowMapper Class and override the rowMap() method. It is responsible for converting a ResultSet row to a Product object.

package com.tutoref.batch;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

import com.tutoref.batch.entity.Product;

public class ProductMapper implements RowMapper<Product> {

 @Override
 public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
  Product product = new Product();
  product.setId(rs.getInt("id"));
  product.setName(rs.getString("name"));
  product.setQuantity(rs.getInt("quantity"));
  product.setUnitPrice(rs.getDouble("unit_price"));
  return product;
 }

 
}

 

The Item Processor

This object processes an item retrieved from the database. It must implement the ItemProcessor interface and override the process() method.

package com.tutoref.batch;

import org.springframework.batch.item.ItemProcessor;

import com.tutoref.batch.entity.Product;

public class ProductItemProcessor implements ItemProcessor<Product,Product>{

 public Product process(Product product) throws Exception {
  System.out.println("Processing item "+product);
  return product;
 }

}
The Job Listener

This object is responsible for listening to items processing events. It must implement the JobExecutionListener and override the beforeJob() and afterJob() methods.

package com.tutoref.batch;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;

public class ProductJobListener implements JobExecutionListener {

 
 public void beforeJob(JobExecution job) {
  System.out.println("Before processing job : " + job.getId() );
 }
 
 public void afterJob(JobExecution job) {
  System.out.println("After processing job : " + job.getId() );
 }

}
The Main program

This class executes the job. It references the Spring Batch context file (job-products.xml).

package com.tutoref.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring batch example
 * 
 * Main Clas
 *
 */
public class App 
{
    public static void main(String[] args){
     
     ApplicationContext context = new ClassPathXmlApplicationContext("job-products.xml");
        
        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("productJob");
      
        try {
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Job Exit Status : "+ execution.getStatus());
      
     } catch (JobExecutionException e) {
            System.out.println("The Job has failed :" + e.getMessage());
            e.printStackTrace();
        }
        
    }
}

Project structure

Finally  and to recapitulate, the project structure will look like the following.

Execution results

The execution of the code will print the following  trace on the console.

Before processing job : 0
INFO: Executing step: [step1]
Processing item [1 | Phone]
Processing item [2 | Tablet]
Processing item [3 | Mouse]
Processing item [4 | Keyboard]
Processing item [5 | Speaker]
Processing item [6 | Screen]
After processing job : 0
Job Exit Status : COMPLETED

 

And generate an xml file with the following content:

Phone|300.0|50
Tablet|250.0|20
Mouse|20.0|30
Keyboard|20.0|30
Speaker|40.0|30
Screen|300.0|40

 

Streams in java 8

A Stream is a sequence of elements supporting sequential and parallel aggregate operations. Streams have been introduced to Java starting from version 8.

Example of the use of Streams

Consider the following example:

// create a collection of Products
List<Product> products= new ArrayList<Product>();

// calculate the sum of prices of the products whose 
// made locally
// and the price is greater or equal to 10
int sumOfPrices = products.stream()
                      .filter(p -> p.isMadeLocally() == true)
                      .filter(p -> p.getPrice() >=10 )
                      .mapToInt(p -> p.getPrice())
                      .sum();

In the above example we create a stream of products using products.stream() .  Then we filter the products by using the filter() function that takes an object of type Predicate as parameter (in other words, a Functional Interface), meaning that we can use a Lamba expression like in this example.

The mapToInt() function takes a parameter of type ToIntFunction which is also a functional interface, this is why we used the lambda expression p-> p.getPrice() . The mapToInt() function returns an object of type IntStream . And finally the sum() function calculates the sum of all the prices of the products.

Convert a Collection into Stream and vice-versa

filter() functions are sometimes combined with collect() function that converts a stream into a Collection. Example :

// In this example, the products List is converted to a stream, and then to a List
List<Product> listOfProducts = products.stream().filter(p-> p.getPrice()>10).collect(Collectors.toList());

 

Streams Vs Collections

Streams and Collections seem to be similar, but in reality they have different goals: Collections are mainly concerned with the efficient management of, and access to, their elements. However Streams do not give a direct access to their elements or manipulate them, but are instead concerned with declaratively describing their source and the computational operations which will be performed in aggregate on that source.

The BaseStream.iterator() and BaseStream.spliterator() operations can be used to perform a controlled traversal.

Sequential and parallel streams

Stream pipelines can be sequential or parallel. Using the Collection.stream() function will create a sequential stream, and the use of Collection.parallelStream() will create a stream with a parallel mode of execution.

 

What is new in Java 9

Java 9 is a major feature release. The following summarizes features and enhancements in Java  language version 9.

  • Allow @SafeVargs annotation on private instance methods
  • Allow final variables to be used as resources in the try-with-resources statement
  • Allow the use of diamond with anonymous classes if the type of the inferred type is denotable.
  • Complete the removal of underscore from the set of legal identifier names. This was started in java version 8.
  • Add support for private interface methods.

Reference : https://docs.oracle.com/javase/9/language/toc.htm#JSLAN-GUID-16A5183A-DC0D-4A96-B9D8-AAC9671222DD

Example of replacing anonymous inner class by a Lambda expression in Java 8

One  of the facilities that Java 8 offers is the reduction of the numbers of lines of code in some situations. In this example, we will replace an anonymous inner class by a Lamba Expression. Reducing the code used to compare two players to almost one single line.

First of all, we will need a Player object. A player has a name and a score. Please note that we will use the Integer wrapper type instead of the primitive int type, in order to benefit from the compareTo method offered by the Integer object.

package com.tutoref;

public class Player {
 
 private String name;
 private Integer score;
 
 // Default constructor to allow instanciation without parameters
 public Player(){}
 
 // Constructor with parameters
 public Player(String name, int score){
  this.name = name;
  this.score = score;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public Integer getScore() {
  return score;
 }
 
 public void setScore(Integer score) {
  this.score = score;
 }
 
 
 @Override
 public String toString() {
  // To customise the displaying of a player
  return "[name : "+this.name+", score : "+this.score+"]";
 }
}

 

Comparison using an anonymous inner class

The playerComparator inner class is used to tell the program how to compare two players, based on their score.

In this example we create the playerComparator of type Coparator<Player>, after that create and add some players to the ListArray collection, then we use the Collections.sort method ot sort the players based on their score.

package com.tutoref;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PlayerSort {

 public static void main(String[] args) {
  
  // Create a comparator object
  Comparator<Player> playerComparator = new Comparator<Player> () {
   @Override
   public int compare(Player player1, Player player2) {
    return player1.getScore().compareTo(player2.getScore());
   }
  };
  
  
  // Create a list of players
  List<Player> playerList = new ArrayList<Player>();
  
  // add some players
  playerList.add(new Player("Messi",500));
  playerList.add(new Player("Ronaldo",750));
  playerList.add(new Player("Roberto",900));
  playerList.add(new Player("Albert",450));
  playerList.add(new Player("Michel",750));
  playerList.add(new Player("Mario",750));
  
  // Sort the list based on the score 
  // using the comparator object
  Collections.sort(playerList,playerComparator);
  
  // display the sorted list of players (in ascendant order)
  System.out.println(playerList);
 }

}

 

COMPARISON Using Lambda Expression

This example will replace the  anonymous inner class, reducing it to one line of code.

package com.tutoref;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PlayerSortUsingLambdaExp {

 public static void main(String[] args) {
  
  // Create a list of players
  List<Player> playerList = new ArrayList<Player>();
  
  // add some players
  playerList.add(new Player("Messi",500));
  playerList.add(new Player("Ronaldo",750));
  playerList.add(new Player("Roberto",900));
  playerList.add(new Player("Albert",450));
  playerList.add(new Player("Michel",750));
  playerList.add(new Player("Mario",750));
  
  Collections.sort(playerList, (p1, p2) -> p1.getScore().compareTo(p2.getScore()) );
  
  // display the sorted list of players (ascendent order)
  System.out.println(playerList);
 }

}

The compactor interface is also a Functional Interface, this is why it can be replaced by a lambda expression. You can imagine that we passed the function making the comparison as parameter.

Example of a Functional Interface

Also called a SAM (Single Abstract Method). An FI is an interface containing :

  • One and only one abstract method;
  • Zero or many static methods
  • Zero or many default methods

There are many built-in functional interfaces included in Java 8. The Predicate FI is one of them.

Here is an example of a functional interface :

@FunctionalInterface
interface MostBasicFi{
   boolean test();

   // a static method
   public static void foo1(){
    // add some logic...
   }

   // a default method
   public static void foo2(){
    // add some logic...
   }
   
}

 

 

How to read and write files in Java 8

This tutorial presents the different options available to read and write files in Java .

Reading files

Using  FileInputStream and BufferedReader

In the following example we will create a BufferedReader from InputStreamReader. Use this option when you need to read byte streams and transform them into character streams. You can specify the encoding charset.  Examples : Reading a zip file, image, network resources, or simply text files etc.

File file = new File("myFile.zip");
try {
  FileInputStream fis = new FileInputStream(file);
  BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  String line;
  while((line=br.readLine())!=null){
    System.out.println(line);
  }
  br.close();
} catch (FileNotFoundException e) {
  // File not found
  e.printStackTrace();
} catch (IOException e) {
  // Error when reading the file
  e.printStackTrace();
}

 

Using FileReader and BufferedReader

This example is similar the previous one, except the fact that we build the BufferedReader from a FileReader.

Use this option when you have to read character based content. You cannot specify the encoding charset for this method and it will use the platform encoding.

File file = new File("myFile.txt");
try {
  BufferedReader br = new BufferedReader(new FileReader(file));
  String line;
  while((line=br.readLine())!=null){
    System.out.println(line);
  }
  br.close();
} catch (FileNotFoundException e) {
  // File not found
  e.printStackTrace();
} catch (IOException e) {
  // Error when reading the file
  e.printStackTrace();
}

 

Using Files.newBufferedReader() in Java 7 and above

This example is similar to the first one (Using FileInputStreamAND BufferedReader) .  It uses the java.nio package and the try-with-resources exception management structure. It works starting from java 7.

The necessary imports

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

The code

Path path = Paths.get("c:/tmp/myfile.csv");
Charset charset = Charset.forName("UTF-8");
try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
  String line = null;
  while ((line = reader.readLine()) != null) {
    System.out.println(line);
  }
} catch (IOException e) {
  e.printStackTrace();
}

 

Using Lambda expression in Java 8

One of the benefits of Lambda Expression is the reduction of the number of lines of code. In this example, we will read and print the content of a file in one line using Lambda Expressions and java.nio.file.Files class :

Files.lines(new File("c:/myfile.txt").toPath()).forEach(System.out::println);

(for simplicity we didn’t include the exception management for the IOException exception)

Writing files

Using FileOutputStream

try {
  File fout = new File("myOutFile.txt");
  FileOutputStream fos = new FileOutputStream(fout);
  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
  bw.write("Write somthing to the file ...");
  bw.newLine();
  bw.close();
} catch (FileNotFoundException e){
  // File was not found
  e.printStackTrace();
} catch (IOException e) {
  // Problem when writing to the file
  e.printStackTrace();
}

 

Using FileWriter

You better use FileWriter or PrintWriter if you plan to write some character based content or a text file.

try {
  FileWriter fw = new FileWriter("myOutFile.txt");
  fw.write("Example of content");
  fw.close();
} catch (FileNotFoundException e) {
  // File not found
  e.printStackTrace();
} catch (IOException e) {
  // Error when writing to the file
  e.printStackTrace();
}

 

Using PrintWriter

Similar to FileWriter but with some specifities. Here is the Oracle’s PrintWriter definition:

Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

try {
  PrintWriter pw = new PrintWriter("myOutFile.txt");
  pw.write("Example of content");
  pw.close();
} catch (FileNotFoundException e) {
  // File not found
  e.printStackTrace();
} catch (IOException e) {
  // Error when writing to the file
  e.printStackTrace();
}

 

Using OutputStreamWriter

try {
  File fout = new File("myOutFile.txt");
  FileOutputStream fos = new FileOutputStream(fout);
  OutputStreamWriter osw = new OutputStreamWriter(fos);
  osw.write("Soe content ...");
  osw.close();
} catch (FileNotFoundException e) {
  // File not found
  e.printStackTrace();
} catch (IOException e) {
  // Error when writing to the file
  e.printStackTrace();
}

 

 

Array Example in Java 8

Declaring and instantiating an array

// declaration and instanciation
String [] oneDimentionArray = new String[10];
String [][] multiDimentionArray = new String[10][];
String [][] multiDimentionArray2 = new String[10][20];

Note that you can also put the square brackets after the variable name :

// Brackets after the variable name
String oneDimentionArray[] = new String[10];

 

Affecting values to an array (Initialisation)

// One dimension array
oneDimentionArray[0]="Value 1";
oneDimentionArray[2]="Value 2";

// two dimension array
multiDimentionArray[0][1] = "An other value";

Trying to access oneDimentionArray[1] will throw the ArrayOutOfBoundsException as it was not set.

Declaring and initialization

When creating arrays of Objects (and not primitives) , you can use curly brackets to initialize you array with the values this way :

String [] array = new String [] {"A", "B", "C"};

 

Iterating trough arrays

To read array values you can read a specific index :

System.out.println("Cell 0 value : "+myTable[0]);

You can also iterate through the array using the old for loop:

for(int i = 0 ; i < myTable.lenght ; i++){
  System.out.println(myTable[i]);
}

Or the new enhanced for loop :

// we suppose that myArray is an array of Strings
for(String value: myArray){
   System.out.println(value)
}

Please note that you can find other ways of iterating through arrays.