February 27, 2013

Delete File in java

How to delete a file in java



package blackberry;

import java.io.*;
 
public class DeleteFile

{
 
private static void deletefile(String file)
{
 
File f1 = new File(file);

boolean success = f1.delete();

if (!success)
{
 
System.out.println("Deletion failed.");

System.exit(0);
}

else
{
System.out.println("File deleted Successfully.");
}

}
 
public static void main(String[] ran)

{
 
    switch(args.length)
{
 
case 0: System.out.println("File has not mentioned.");
 
          System.exit(0);
 
case 1: deletefile(ran[0]);

         System.exit(0);
 
default : System.out.println("Multiple files are not allow.");
 
            System.exit(0);
  
 }
 
}

}

.ini file


Gutmann algorithm for secure file deletion read here

java blog

Event Listener example in java frame


Event Handling Interface in java with Examples


EventListener : A Listener is called when user invoke an event either through a user interface or through code. Listener are supported by java swing. There are container listener, caret listener, key event listener, window listener in java. You can implements multiple listener event to same class in java.


 import java.awt.*;
import java.awt.event.*;

public class Technology extends Frame implements WindowListener,ActionListener {
    TextField text = new TextField(20);
    Button b;
    private int numClicks = 0;

    public static void main(String[] args) {
        Technology myWindow = new Technology ("Duck Dynasty");
        myWindow.setSize(350,100);
        myWindow.setVisible(true);
    }

    public Technology(String title) {

        super(title);
        setLayout(new FlowLayout());
        addWindowListener(this);
        b = new Button("Click me");
        add(b);
        add(text);
        b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        numClicks++;
        text.setText("Button Clicked " + numClicks + " times");
    }

    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
    }

    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}

}



February 04, 2013

Java simple Date format example



How to parse date in simple date format


package

BlackBery;

import

java.text.SimpleDateFormat;

import

java.util.Date;

/**
* formatting a date in java examples
*/


/**
*

@author BlackBerry

*
*/
public

class ParsingDate {
/**

*

@param args 2010-11-12

*/

public static void main(String[] args) {
// TODO Todays days formating

SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(" Date for blackberry application : "+s.format(new Date()));
}
}



Getting the following error while parsing JSON date string to Java Date

Exception in thread "main" com.google.gson.JsonSyntaxException: 15-08-2020 10:10:10
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:791)
at com.google.gson.Gson.fromJson(Gson.java:757)
at com.google.gson.Gson.fromJson(Gson.java:706)
at com.google.gson.Gson.fromJson(Gson.java:678)
at GsonTest.main(GsonTest.java:23)
Caused by: java.text.ParseException: Unparseable date: "15-08-2020 10:10:10"
at java.base/java.text.DateFormat.parse(DateFormat.java:395)
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:105)


Solution : - 

Player class in java

public class Player {
private String name;
private Date dob;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}

GSON Test class

public class GsonTest {

/**
* @param args
*/
public static void main(String[] args) {
String data = "{'name':'MS Dhoni','dob':'15-08-2020 10:10:10'}";
Gson gson = new GsonBuilder()
   .setDateFormat("EEE, dd-MM-yyyy hh:mm:ss").create();
Player indianplayers= gson.fromJson(data, Player.class);
System.out.println("Cricket player name : "+emp.getName());
System.out.println("Retirement date :"+emp.getDob());
}

}

Thanking you

Updated on 15 Aug 2020


Facebook comments