Java Tutorial: How To Select And Send All File In One Folder Using "for" Looping Method On NetBeans

Words
734
Reading
4 min
Listen
Play
8y

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

  • You will learn to create send file apllication
  • You will learn how to create action to select all file in folder using for looping
  • You will learn how to connect some class in one package with socket programming

Requirements

  • you must know understand the basic of java programming
  • you must know about socket programming
  • you must know how to implement for looping

Difficulty

  • Basic

Tutorial Contents

okay, on this occasion I will create a program to select and send all files in a folder at the same time using for looping method, here I will create three classes in one package, the first is to implement looping for that function to select all files in a folder, then I will create a program to send the file that has been selected before, and the last one I will create a program for recipients files that have been sent and will be stored into other folders. I purposely created three separate programs for easy to understanding.


  • okay, just start our tutorial:
  • create a new package as a place for all three classes we will create.

  • then create a class in the package you have created.
  • then you have to do is create a method for selecting files in a folder, type the program below in the package that you created earlier.
package tambah;
import java.io.File;
public class NewClass {
    public static void main(String[] args) {
      try {
        File fileDir=new File("F:\\doc\\");
        File [] listOfFiles=fileDir.listFiles();
       
            for (int i=0;i<=listOfFiles.length;i++){
                String nama2file=listOfFiles[i].getName();
                System.out.println(nama2file);
            }catch (Exception e);
                System.out.println("success");            
            }
    }
}
  • okay, I will explain a little about the above program.
  • package tambah; this is the declaration of the package name we have created earlier and make sure the three classes that we will make are in the same package.
  • public static void main(String[] args) { and this is the main function that must exist in any java programming.
  • here we also use try{ and }catch function
  • File fileDir=new File("F:\\doc\\"); this works for selecting the folders that we will send.
  • after we select the folder, then we will create a list of the existing files in the selected folder.
    File [] listOfFiles=fileDir.listFiles();
  • and this is the for looping process which will work to select all the files in the folder automatically.
for (int i=0;i<=listOfFiles.length;i++){
                String nama2file=listOfFiles[i].getName();
                System.out.println(nama2file);
            }catch (Exception e);
  • System.out.println("success"); this works to print a notification if the program has been successfully executed.
  • okay, the first program we have successfully created, then we will create a program for sending the selected file.
  • type the program below in the same package as the previous program.
package Tambah;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Sendt {
public static void main(String[] args)throws IOException {
    int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));
String IP = JOptionPane.showInputDialog("Input Your IP Server : ");
Socket sock=new Socket("localhost", 6066);
DataInputStream in= new DataInputStream(sock.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =new DataOutputStream(sock.getOutputStream());
out.writeUTF("send file succes, wait for client");
sock.close();
}}
  • this works for input and output process using Stream data.
import java.io.DataInputStream;
import java.io.DataOutputStream;
  • import java.net.Socket; we use this because we are in socket programming.
  • This works to connect this program with other programs by entering the server name and port
 int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));
String IP = JOptionPane.showInputDialog("Input Your IP Server : ");
Socket sock=new Socket("localhost", 6066);
  • out.writeUTF("send file succes, wait for client");this works to print a notification that the file has been sent and wait for the recipient's response.
  • sock.close(); this is to end the process of socket programming.

  • okay, the sender program we have successfully created, then we will create the recipient file.
  • type the program below in the same package as the previous program.
package Tambah;
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 programB {
    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");
        BufferedOutputStream bos = new BufferedOutputStream (fos);
        bos.flush();}
        private javax.swing.JTextArea txtPath;}
  • okay, I'll explain a bit about the above program.
  • this is the save process, here we will define the storage for the file that has been sent.
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); and this is the name of the server and port that serves to connect all the programs we have created.
  • byte[]mybytearray =new byte[1024]; and this is the determination of the capacity of files that can be sent.
  • okay, the three programs we have created, then we will run the program one by one.
  • first run the file selection program to be sent.
  • make sure the folder you choose is available in your computer directory.


  • then right click on the file and select run file.

  • then will appear list of files selected.

  • then run the sender file.
  • then a dialog box will appear asking us to enter the port.
    Screenshot_2.png

  • then if it appears this notification means the file has been sent and just waiting for the recipient's response.

  • last we will run the recipient program.
  • select your destination folder.
  • okay, the three programs we have run, then we will check whether the file has entered the destination folder.
    .

  • okay file has been successfully sent, it means our tutorial success.

Curriculum

Include a list of related tutorials you have already shared on Utopian that make up a Course Curriculum, if applicable.

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/649c77022957ac855132aef8a53cc335