Write Data to CSV File in Java

Maruthi Krishna
Updated on 11-Sep-2019 14:24:40

17K+ Views

A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to write the contents of a .csv file using a Java program.Maven dependency    com.opencsv    opencsv    4.4    org.apache.commons    commons-lang3    3.9 The CSVWriter class of the com.opencsv package represents a simple csv writer. While instantiating this class you need to pass a Writer object representing the file, to which you want to write the data, as a parameter to its constructor.It provides methods named writeAll() and writeNext() to write data to a .csv file.Using the ... Read More

Read Data from CSV File in Java

Maruthi Krishna
Updated on 11-Sep-2019 14:16:41

4K+ Views

A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to read the contents of a .csv file using a Java program.Maven dependency    com.opencsv    opencsv    4.4    org.apache.commons    commons-lang3    3.9 The CSVReader class of the com.opencsv package represents a simple CSV reader. While instantiating this class you need to pass a Reader object representing the file to be read as a parameter to its constructor. It provides methods named readAll() and readNext() to read the contents of a .csv fileUsing the readNext() methodThe readNext() ... Read More

Create Directory in Project Folder Using Java

Maruthi Krishna
Updated on 11-Sep-2019 14:10:53

1K+ Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.The mkdir() method of this class creates a directory with the path represented by the current object.Therefore, to create a directory −Instantiate the File class by passing the path of the directory you need to create, as a parameter (String).Invoke the mkdir() method using the above-created file object.ExampleThe following Java example reads the path and name of the directory to be created, from the user, and creates it. Live Demoimport java.io.File; import java.util.Scanner; ... Read More

Move Files Using FileUtils in Java

Maruthi Krishna
Updated on 11-Sep-2019 14:07:37

828 Views

Using the File classThe class named File of the java.io package represents a file or directory (pathnames) in the system. This class provides various methods to perform various operations on files/directories.This class provides various methods to manipulate files, The rename() method of the File class accepts a String representing a destination file and, renames the abstract file path of the current file to the given one.This method actually moves the file from the source path to the destination path.Example Live Demoimport java.io.File; public class MovingFile {    public static void main(String args[]) {       //Creating a source file object ... Read More

Read Data From All Files in a Directory Using Java

Maruthi Krishna
Updated on 11-Sep-2019 14:04:20

12K+ Views

The class named File of the java.io package represents a file or directory (pathnames) in the system. This class provides various methods to perform various operations on files/directories.To get the list of all the existing files in a directory this class provides five different methods to get the details of all files in a particular folder −String[] list()File[] listFiles()String[] list(FilenameFilter filter)File[] listFiles(FilenameFilter filter)File[] listFiles(FileFilter filter)The ListFiles() methodThis method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The following Java program prints the name, path and, size ... Read More

Read Certain Number of Elements from a File in Java

Maruthi Krishna
Updated on 11-Sep-2019 13:59:07

788 Views

To read a fixed number of elements from a file you can either read a required number of data elements from the file and process them or, read the entire file into a collection or an array and process it for every n element.ExampleFollowing Java program, reads the contents of a file 10 words at a time and prints them in a separate line.import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadingData {    public static void main(String args[]) throws FileNotFoundException {       //Creating an object of the File to read data       File file = ... Read More

Python Program for Simple Interest

Pavitra
Updated on 11-Sep-2019 13:21:02

2K+ Views

In this article, we will learn about the calculation of simple interest in Python 3.x. Or earlier.Simple interest is calculated by multiplying the daily interest rate by the principal amount by the number of days that elapse between the payments.Mathematically, Simple Interest = (P x T x R)/100 Where, P is the principal amount T is the time and R is the rateFor example, If P = 1000, R = 1, T = 2 Then SI=20.0 Now let’s see how we can implement a simple interest calculator in Python.Example Live DemoP = 1000 R = 1 T = 2 # simple ... Read More

Python Program for Selection Sort

Pavitra
Updated on 11-Sep-2019 13:18:05

2K+ Views

In this article, we will learn about the Selection sort and its implementation in Python 3.x. Or earlier.In the selection sort algorithm, an array is sorted by recursively finding the minimum element from the unsorted part and inserting it at the beginning. Two subarrays are formed during the execution of Selection sort on a given array.The subarray, which is already sortedThe subarray, which is unsorted.During every iteration of selection sort, the minimum element from the unsorted subarray is popped and inserted into the sorted subarray.Let’s see the visual representation of the algorithm −Now let’s see the implementation of the algorithm ... Read More

Remove Nth Character from a String in Python

Pavitra
Updated on 11-Sep-2019 13:13:35

270 Views

In this article, we will learn about the solution to the problem statement given below −Problem statement − We are given a string, we have to remove the ith indexed character from the given string and display it.In any string in Python, indexing always starts from 0. Suppose we have a string “tutorialspoint” then its indexing will be done as shown below −T u t o r i a l s p o i n t 0 1 2 3 4 5 6 7 8 9 10 11 12 13Now let’s see the Python script for ... Read More

Product of Unique Prime Factors of a Number in Python

Pavitra
Updated on 11-Sep-2019 13:10:13

1K+ Views

In this article, we will learn about the solution to the problem statement given below −Problem statement − Given a number n, we need to find the product of all of its unique prime factors available and return it.For example, Input: num = 11 Output: Product is 11 Explanation: Here, the input number is 11 having only 1 prime factor and it is 11. And hence their product is 11.Approach 1Using a for loop from i = 2 to n+1 check whether i is a factor of n & then check if i is the prime number itself, if yes ... Read More

Advertisements