Saturday, February 9, 2019

Formatter and scanner in Java.Util package


 import java.io.File;  
 import java.io.FileNotFoundException;  
 import java.util.Formatter;  
 import java.util.Scanner;  
 public class CSVFile {  
  Formatter formatter;  
  Scanner scanner;  
  public void createFile() {  
  try {  
   formatter = new Formatter("muttu1.txt");  
  } catch (FileNotFoundException exception) {  
   exception.printStackTrace();  
  }  
  }  
  public void writeFile() {  
  formatter.format("%s,%s,%s", "muttu", "pavu", "navu");  
  }  
  public void closeFile() {  
  formatter.close();  
  }  
  public void readFile() throws FileNotFoundException {  
  scanner = new Scanner(new File("muttu1.csv"));  
  while (scanner.hasNext()) {  
   String x = scanner.next();  
   System.out.println(x);  
  }  
  }  
  public static void main(String[] args) throws FileNotFoundException {  
  CSVFile csvFile = new CSVFile();  
  csvFile.createFile();  
  csvFile.writeFile();  
  csvFile.closeFile();  
  csvFile.readFile();  
  csvFile.closeFile();  
  }  
 }  

No comments:

Post a Comment