HOW TO CREATE ACTION TO AUTO SELECT FILE USING "if" CONDITIONAL ON JAVA

in #utopian-io6 years ago

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

  • You will learn how to create option select files using other options using if conditional
  • You will learn how to create simple GUI on NetBeans
  • You will learn how to create program to send and receive file

Requirements

  • you must know the basic of java programming
  • you must understand how to conect two program in one package
  • you must know how to using if conditional on java programing

Difficulty

  • Basic

Tutorial Contents

okay, on this occasion I will make a tutorial about how to make automatic file selection using if condition on java programming to send file , we will make two programs here to send and receive file, in program sender file we will apply
if condition in order to make the process of selecting the file you want to send automatically.
okay we just go the tutorial:

  • first we will create a package in which we will create two classes.

  • okay next we will make the first program to send the file.

  • then select java category and java class type as shown below.

  • then type the program below for the file sender program, I will explain below.
package apalication;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;
public class sendFile {
    public static void main(String[] args) throws IOException {
        String pilihan=JOptionPane.showInputDialog("masukan nilai 1 dan 2");
        int a=Integer.parseInt(pilihan);
        if (a==1){
        ServerSocket ServerSocket=new ServerSocket (6066);
        Socket sock =ServerSocket.accept();
        File myfile=new File ("D:\\data1.txt");
        byte[]mybytearray=new byte [(int)myfile.length()];
        BufferedInputStream bis =new BufferedInputStream(new FileInputStream(myfile));
        bis.read(mybytearray,0,mybytearray.length);
        OutputStream os=sock.getOutputStream();
        os.write(mybytearray,0,mybytearray.length);
        os.flush();
        sock.close();}
            else {
        ServerSocket ServerSocket=new ServerSocket (6066);
        Socket sock =ServerSocket.accept();
        File myfile=new File ("F:\\data2.txt");
        byte[]mybytearray=new byte [(int)myfile.length()]; 
        BufferedInputStream bis =new BufferedInputStream(new FileInputStream(myfile));
        bis.read(mybytearray,0,mybytearray.length);
        OutputStream os=sock.getOutputStream();
        os.write(mybytearray,0,mybytearray.length);
        os.flush();
        sock.close();
        }}}

  • below is an explanation of the above program:
  • package apalication; This is the declaration of the package name we have created before.
  • BufferedReader (new InputStreamReader (System.in)); This Function To Get An Input From The Keyboard
  • Import java.io.*;is a command to input data from the input device as an example keyboard, while the output is a command or function to display data to the user.
  • import java.io.FileInputStream; This works for the process memaasukkan file.
  • import java.io.IOException; So Throws IOException Is A Method That Reads String Data Input.
  • The java.net.ServerSocket class is used by the Server to listen to connections, whilejava.net.Socketis used by the Client for initialization of the connection.
  • import javax.swing.JOptionPane; This is for input process using JOptionPane.
  • public class sendFile This is the process of declaration of the sender file name which will be called on the recipient program.
  • String pilihan=JOptionPane.showInputDialog("your option file 1 or 2"); this to display a dialog box that will be selected at the time the program is run.
  • int a = Integer.parseInt (optional); This is a declaration of variables a which will be used in conditioning if for the process of creating automatic file selection options.
  •      ServerSocket ServerSocket=new ServerSocket (6066);
         Socket sock =ServerSocket.accept();
         File myfile=new File ("D:\\data1.txt");
    

this is if conditional which means if we choose number: 1 then that will be selected is file with name data1 on D partition of your computer.

  •       ServerSocket ServerSocket=new ServerSocket (6066);
          Socket sock =ServerSocket.accept();
          File myfile=new File ("F:\\data2.txt");
    

this is the second option that is the opposite of the first condition.

  • if the selected is the second condition then that will be automatically selected is a file with name: data2.txt contained on partition D in your computer.

  • Okay the first program we have finished, then we will create a second program as the program receiver file that is sent.
  • type the program below and save in the same package with the first program.
package aplication;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
import javax.swing.JFileChooser;
public class receive {
    public static void main(String[] argv) throws Exception{
        JFileChooser pilih=new JFileChooser();
       pilih.setDialogTitle("Tentukan Tempat Penyimpanan");
       int userSelection = pilih.showSaveDialog(null);
       if(userSelection == JFileChooser.APPROVE_OPTION){
           File fileToSave = pilih.getSelectedFile();
           System.out.println("Save as file : "+ fileToSave.getAbsolutePath());
                  }
        Socket sock=new Socket("localhost", 6066);
        byte[]mybytearray =new byte[1024];
        InputStream is =sock.getInputStream();
        FileOutputStream fos=new FileOutputStream("E:\\data.txt");
        BufferedOutputStream bos = new BufferedOutputStream (fos);
        bos.flush();}
        private javax.swing.JTextArea txtPath;}
  • int userSelection = pilih.showSaveDialog(null);this serves to display the option of file storage where previously sent.
  • System.out.println (" Save as file: "+ fileToSave.getAbsolutePath ()); serves to display a warning that the file has been saved or not.
  • okay both programs we have created, then we try to run both programs.
  • make sure that in the folder you choose there are two files with the same name in the program.

  • first run the file sender program, how to right click on the program and select run file.
  • then the dialog box will appear like this then we select option 1 or option 2, each will send a different file.
  • then after we select the option, then we run the second program, the same way as before right click and select run file it will appear like this then select the storage location.
  • then we see whether the file is submitted right or wrong.

    okay file received is in accordance with the file that we send, we just choose
  • means the program has been running in accordance with the conditions we have made before.

OK ,,, TUTORIAL IS COMPLETE

Curriculum

Proof of Work Done

Insert here the full url of the code used in the tutorial, under your GitHub or a relevant gist, e.g. https://gist.github.com/andilapulga/000e1bf6e7c92778775b0ab4c7be91d2

Sort:  

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Work on improving your post for English mistakes and incorrectly written words and sentences, as those could lead your author to lose track of your purpose.
  • There are parts of the code that have little explanation, try to explain as much as possible.
  • Don't put the title in caps lock.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian rules and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post,Click here


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you so much sir @portugalcoin

Hey @portugalcoin
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Hey @lapulga
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

gran aporte muchas gracias, muy fácil de comprender y de seguir. Saludos cordiales

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.033
BTC 61726.60
ETH 3041.60
USDT 1.00
SBD 3.86