Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class RowDelimitedFileExample {
- private static final String INPUT_FILE = "C:/workspace/Sample.csv";
- private static final String ROW_DELIMITED_BY = "\n";
- private static final String FIELD_DELIMITED_BY = ",";
- public static void main(String[] args) {
- // Creating the file object
- File aFile = new File(INPUT_FILE);
- // Creating the variable of type Scanner
- Scanner theScanner = null;
- try {
- // Creating the scanner to read the lines in the ROW_DELIMITED file
- theScanner = new Scanner(aFile).useDelimiter(ROW_DELIMITED_BY);
- // Iterating through the ROWS
- while (theScanner.hasNext()) {
- String row = theScanner.next();
- // Splitting the ROW into Fields based on the FIELD_DELIMITER
- for (String field : row.split(FIELD_DELIMITED_BY)) {
- // Printing the FIELDS contents
- System.out.printf("%s|", field.trim());
- }
- // Printing a New-line
- System.out.print(System.lineSeparator());
- }
- // Catching the FileNotFound Exception
- } catch (FileNotFoundException e) {
- // Handling the Exception condition
- System.err.println("The file [" + INPUT_FILE + "] is not found !");
- } finally {
- // Handling closing of open resources
- theScanner.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement