Wednesday, June 15, 2016

Java to read csv file

This example show how to read csv file using Java.


Prepare:
- Create a spreadsheet:


- Export as csv file:


- The exported csv file in text format:


Using java to read the csv file and print the content:
package javareadcsv;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaReadCSV {
    
    static String CsvFile = "/home/buddy/test/test.csv";
    static String FieldDelimiter = ",";

    public static void main(String[] args) throws IOException {
        BufferedReader br;
        
        try {
            br = new BufferedReader(new FileReader(CsvFile));
            
            String line;
            while ((line = br.readLine()) != null) {
                String[] fields = line.split(FieldDelimiter, -1);
                System.out.print(fields.length + "-");
                for(String s : fields){
                    System.out.print(s);
                    System.out.print(":");
                }
                System.out.println();
            }
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JavaReadCSV.class.getName())
                    .log(Level.SEVERE, null, ex);
        }
        
    }
    
}



To display the content with JavaFX TableView, read next post.

Tuesday, June 7, 2016

Java example using Supplier to get input from Scanner

Java example using java.util.function.Supplier to get input from java.util.Scanner.


JavaSupplier.java
package javasupplier;

import java.util.Scanner;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class JavaSupplier {

    public static void main(String[] args) {
        Supplier<String> msg  = ()-> "http://java-buddy.blogspot.com/";
        System.out.println(msg.get());
        System.out.println();
        
        Scanner scanner = new Scanner(System.in);
        Supplier<String> scannerNext = () -> scanner.next();
        System.out.println("Enter something, 'q' to quit");
        
        Stream.generate(scannerNext)
                .map(s -> {
                    System.out.println(s);
                    return s;
                })
                .allMatch(s -> !"q".equals(s));
    }
    
}



Sunday, April 17, 2016

Get my MAC address using NetworkInterface

Java example to get MAC address using NetworkInterface:



package javamyip;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaMyIP {

    public static void main(String[] args) {
        displayMyIP();
    }
    
    static void displayMyIP(){
        Enumeration<NetworkInterface> nets;
        try {
            nets = NetworkInterface.getNetworkInterfaces();
            for (NetworkInterface netint : Collections.list(nets)){
                System.out.printf(netint.getDisplayName() +"\n");
                Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
                for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                    System.out.printf("InetAddress: %s\n", inetAddress);
                }
                
                byte[] mac = netint.getHardwareAddress();
                if(mac != null){
                    StringBuilder macAddr = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        macAddr.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : "")); 
                    }
                    System.out.printf("Hardware address (MAC): [%s]\n", macAddr.toString());
                }
                
                System.out.printf("\n");
            }
        } catch (SocketException ex) {
            Logger.getLogger(JavaMyIP.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}