Thursday, January 11, 2018

function to print the repeated values in an array with it's count

public static void printRepeating(int arr[], int size)
{
int i,j;
System.out.println("The repeating elements are : ");
HashMap<Integer, Integer> hmap = new HashMap<>();

for (i = 0; i < size-1; i++)
{
int c=1;
for (j = i+1; j < size; j++)
{

if(arr[i]==arr[j]){
c++;
}
}
if(c>1&& !hmap.containsValue(arr[i])){

hmap.put(c,arr[i]);

}
}

for (int k:hmap.keySet()){
System.out.println(hmap.get(k)+" : count "+k);
}
}


Sunday, January 7, 2018

java program to download file from url


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class URLReader {

    public static void copyURLToFile(URL url, File file) {

        try {
            InputStream input = url.openStream();
            if (file.exists()) {
                if (file.isDirectory())
                    throw new IOException("File '" + file + "' is a directory");

                if (!file.canWrite())
                    throw new IOException("File '" + file + "' cannot be written");
            } else {
                File parent = file.getParentFile();
                if ((parent != null) && (!parent.exists()) && (!parent.mkdirs())) {
                    throw new IOException("File '" + file + "' could not be created");
                }
            }

            FileOutputStream output = new FileOutputStream(file);

            byte[] buffer = new byte[4096];
            int n = 0;
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
            }

            input.close();
            output.close();

            System.out.println("File '" + file + "' downloaded successfully!");
        }
        catch(IOException ioEx) {
            ioEx.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {

        //URL pointing to the file
        String sUrl = "https://singztechmusings.files.wordpress.com/2011/09/maven_eclipse_and_osgi_working_together.pdf";

        URL url = new URL(sUrl);

        //File where to be downloaded
        File file = new File("home\\sujith\\IdeaProjects\\downloadTest");

        URLReader.copyURLToFile(url, file);
    }

}

java program to download a file from an external url

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
public class FileDownloadTest {
public static void main(String[] args) {
try {
// Make sure that this directory exists
String dirName = "home\\sujith\\FileDownload";
System.out.println("Downloading \'Maven, Eclipse and OSGi working together\' PDF document...");
saveFileFromUrlWithJavaIO(
dirName + "\\maven_eclipse_and_osgi_working_together.pdf",
"https://singztechmusings.files.wordpress.com/2011/09/maven_eclipse_and_osgi_working_together.pdf");
System.out.println("Downloaded \'Maven, Eclipse and OSGi working together\' PDF document.");
System.out.println("Downloading \'InnoQ Web Services Standards Poster\' PDF document...");
saveFileFromUrlWithCommonsIO(
dirName + "\\innoq_ws-standards_poster_2007-02.pdf",
"http://singztechmusings.files.wordpress.com/2011/08/innoq_ws-standards_poster_2007-02.pdf");
System.out.println("Downloaded \'InnoQ Web Services Standards Poster\' PDF document.");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// Using Java IO
public static void saveFileFromUrlWithJavaIO(String fileName, String fileUrl)
throws MalformedURLException, IOException {
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
in = new BufferedInputStream(new URL(fileUrl).openStream());
fout = new FileOutputStream(fileName);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} finally {
if (in != null)
in.close();
if (fout != null)
fout.close();
}
}
// Using Commons IO library
// Available at http://commons.apache.org/io/download_io.cgi
public static void saveFileFromUrlWithCommonsIO(String fileName,
String fileUrl) throws MalformedURLException, IOException {
FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));
}
}

//https://mvnrepository.com/artifact/commons-io/commons-io/2.4

Friday, August 4, 2017

Android application using shared preference.




/***************************************************************************************
Program Name: Write an android application using shared preference.
Author: sujith
Date: 25-10-2016
***************************************************************************************/


ActivityMain.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
tools:context="com.example.shamanthrai.sharedpreference.MainActivity"
>
<
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shared Preference "
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="35dp"
/>
<
EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_marginTop="67dp"
android:hint="Name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<
EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingTop="20dp"
android:hint="Email"
/>
<
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/btnSave"
android:layout_below="@+id/editText3"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:onClick="Save"
/>
<
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get"
android:id="@+id/btnGet"
android:layout_toRightOf="@+id/btnSave"
android:layout_below="@id/editText3"
android:layout_marginTop="50dp"
android:layout_centerVertical="true"
android:onClick="Get"
android:layout_marginLeft="40dp"
/>
<
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/btnClear"
android:layout_toRightOf="@+id/btnGet"
android:layout_below="@id/editText3"
android:layout_marginTop="50dp"
android:layout_marginLeft="30dp"
android:onClick="Clear"
/>
</
RelativeLayout>




MainActivity.java:

package com.example.shamanthrai.sharedpreference;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
SharedPreferences
sharedpreferences;
TextView
name,email;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Email = "emailKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
name = (TextView) findViewById(R.id.editText);
email = (TextView) findViewById(R.id.editText3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Email)) {
email.setText(sharedpreferences.getString(Email, ""));
}
}
public void Save(View view){
String n=
name.getText().toString();
String e=
email.getText().toString();
Editor editor=
sharedpreferences.edit();
editor.putString(
Name,n);
editor.putString(
Email,e);
editor.commit();
Toast.
makeText(MainActivity.this,"Saved!!", Toast.LENGTH_LONG).show();
}
public void Clear(View view)
{
name=(TextView)findViewById(R.id.editText);
email=(TextView)findViewById(R.id.editText3);
name.setText("");
email.setText("");
}
public void Get(View view){
name=(TextView)findViewById(R.id.editText);
email=(TextView)findViewById(R.id.editText3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Email)) {
email.setText(sharedpreferences.getString(Email, ""));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}




OUTPUT: