- Java Data Science Cookbook
- Rushdi Shams
- 321字
- 2021-07-09 18:44:26
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:
- 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
- Include the JAR file in your project an external JAR in Eclipse.
How to do it...
- Say, you are trying to read the contents of a file located in your
C:/ drive
nameddummy.txt
. First, you need to create a file object for accessing this file as follows:File file = new File("C:/dummy.txt");
- 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 namedFileUtils
. 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, thefile
object, which is the file we will be reading, and then the encoding of the file, which in this example isUTF-8
:String text = FileUtils.readFileToString(file, "UTF-8");
- 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()); }
推薦閱讀
- 大規(guī)模數(shù)據(jù)分析和建模:基于Spark與R
- 數(shù)據(jù)之巔:數(shù)據(jù)的本質(zhì)與未來(lái)
- 區(qū)塊鏈:看得見(jiàn)的信任
- Ceph源碼分析
- 網(wǎng)站數(shù)據(jù)庫(kù)技術(shù)
- 數(shù)據(jù)庫(kù)查詢優(yōu)化器的藝術(shù):原理解析與SQL性能優(yōu)化
- 大數(shù)據(jù)時(shí)代系列(套裝9冊(cè))
- 數(shù)據(jù)中心經(jīng)營(yíng)之道
- 大數(shù)據(jù)技術(shù)體系詳解:原理、架構(gòu)與實(shí)踐
- 數(shù)據(jù)庫(kù)基礎(chǔ)與應(yīng)用
- Applying Math with Python
- 大數(shù)據(jù)網(wǎng)絡(luò)傳播模型和算法
- Hands-On Big Data Analytics with PySpark
- Working with OpenERP
- Reactive Programming in Kotlin