Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Data Science Cookbook

You're reading from   Java Data Science Cookbook Explore the power of MLlib, DL4j, Weka, and more

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787122536
Length 372 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Rushdi Shams Rushdi Shams
Author Profile Icon Rushdi Shams
Rushdi Shams
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Obtaining and Cleaning Data FREE CHAPTER 2. Indexing and Searching Data 3. Analyzing Data Statistically 4. Learning from Data - Part 1 5. Learning from Data - Part 2 6. Retrieving Information from Text Data 7. Handling Big Data 8. Learn Deeply from Data 9. Visualizing Data

Reading contents from text files all at once using Apache Commons IO

The same functionality described in the previous recipe can be achieved using Apache Commons IO API.

Getting ready

In order to perform this recipe, we will require the following:

  1. In this recipe, we will be using a Java library from Apache named Commons IO. Download the version of your choice from here: https://commons.apache.org/proper/commons-io/download_io.cgi
  2. Include the JAR file in your project an external JAR in Eclipse.

How to do it...

  1. Say, you are trying to read the contents of a file located in your C:/ drive named dummy.txt. First, you need to create a file object for accessing this file as follows:
            File file = new File("C:/dummy.txt");  
    
  2. Next, create a string object to hold the text contents of your file. The method we will be using from Apache Commons IO library is called readFileToString, which is a member of the class named FileUtils. There are many different ways you can call this method. But for now, just know that we need to send two arguments to this method. First, the file object, which is the file we will be reading, and then the encoding of the file, which in this example is UTF-8:
            String text = FileUtils.readFileToString(file, "UTF-8"); 
    
  3. The preceding two lines will be enough to read text file content and put that in a variable. However, you are not only a data scientist, you are a smart data scientist. Therefore, you need to add a few lines before and after the code just to handle exceptions thrown by Java methods if you try to read a file that does not exist, or is corrupted, and so on. The completeness of the preceding code can be achieved by introducing a try...catch block as follows:
           File file = new File("C:/dummy.txt"); 
           try { 
           String text = FileUtils.readFileToString(file, "UTF-8"); 
           } catch (IOException e) { 
           System.out.println("Error reading " + file.getAbsolutePath()); 
           } 
    
You have been reading a chapter from
Java Data Science Cookbook
Published in: Mar 2017
Publisher: Packt
ISBN-13: 9781787122536
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image