I’m trying to do this Java program were it moves files to a folder, but I can\'t
ID: 3912497 • Letter: I
Question
I’m trying to do this Java program were it moves files to a folder, but I can't ask the user for the source path, directory path and the email to send to.
This program moves file to a new folder and wen it finish moving files it will send an email.
But I'm trying to ask the user for the size of the memory, source folder, directory folder and the email to send to. The ones that are not working are source folder, directory folder and email.
Before you can run this program you need to download activation.jar and javax.mail-1.4.4.jar files.
I'll appreciate the help.
My Code:
package MoveFile2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MoveFile
{
public static void main(String[] args)
{
//variables for Moving Files
File srcFolder; //source folder
File destFolder; //destination folder
File[] oldFiles = srcFolder.listFiles(); //get a list of all files in the folder
File newFile;
BufferedReader reader;
BufferedWriter writer;
long size_limit; //500KB
int count = 0;
char[] buffer = new char[1024];
int n;
destFolder.mkdirs();
//variables to send the email
String host ="smtp.gmaaiil.com";
String user = "user@gmaaiil.com"; //User email address
String pass = "User123"; //Password of the email
String to; //Receiver
String from = "user@gmaaiil.com"; //Message send from
String subject = "Hi User!"; //Subject of the email
String messageText = "Moved " + count + " file(s) to " + destFolder.getAbsolutePath(); //Message text of the email
boolean sessionDebug = false;
Scanner myScanner;
myScanner = new Scanner(System.in);
System.out.println("Enter de memory: ");
size_limit = myScanner.nextLong();
System.out.println("Enter de sorce folder: ");
srcFolder = myScanner.nextLine();
System.out.println("Enter de destination folder: ");
destFolder = myScanner.nextLine();
System.out.println("Enter the email: ");
to = myScanner.toString();
for(int i = 0; i < oldFiles.length; i++)
{
if(!oldFiles[i].isFile() || oldFiles[i].length() <= size_limit) //skip files that are smaller than limit
continue;
count++;
newFile = new File(destFolder.getAbsolutePath() + File.separator + oldFiles[i].getName());
try
{
while (newFile.createNewFile())
{
reader = new BufferedReader(new FileReader(oldFiles[i]));
writer = new BufferedWriter(new FileWriter(newFile));
while ((n = reader.read(buffer)) != -1 )
{
writer.write(buffer, 0, n);
}
reader.close();
writer.close();
oldFiles[i].delete();
}
}
catch (IOException ioEx)
{
System.out.println(ioEx.getMessage());
}
}
try
{
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.required", "true");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)}; //address of sender
msg.setRecipients(Message.RecipientType.TO, address); //receiver email
msg.setSubject(subject); msg.setSentDate(new Date()); //message send date
msg.setText(messageText); //actual message
Transport transport=mailSession.getTransport("smtp"); //server through which we are going to send msg
transport.connect(host, user, pass); //we need because we have to authenticate sender email and password
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("Email send successfully to " + to);
}
catch(Exception ex)
{
System.out.println(ex); //if any error occur then error message will print
}
System.out.println("Moved " + count + " file(s) to " + destFolder.getAbsolutePath());
System.exit(0);
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MoveFile
{
public static void main(String[] args)
{
//variables for Moving Files
File srcFolder = null; //source folder
File destFolder = null; //destination folder
File newFile;
BufferedReader reader;
BufferedWriter writer;
long size_limit; //500KB
int count = 0;
char[] buffer = new char[1024];
int n;
//variables to send the email
String host ="";
String user = ""; //User email address
String pass = "User123"; //Password of the email
String to; //Receiver
String from = ""; //Message send from
String subject = "Hi User!"; //Subject of the email
boolean sessionDebug = false;
Scanner myScanner;
myScanner = new Scanner(System.in);
System.out.println("Enter de memory: ");
size_limit = myScanner.nextLong();
System.out.println("Enter de sorce folder: ");
srcFolder = new File(myScanner.nextLine());
System.out.println("Enter de destination folder: ");
destFolder = new File(myScanner.nextLine());
System.out.println("Enter the email: ");
to = myScanner.toString();
File[] oldFiles = srcFolder.listFiles(); //get a list of all files in the folder
destFolder.mkdirs();
String messageText = "Moved " + count + " file(s) to " + destFolder.getAbsolutePath(); //Message text of the email
for(int i = 0; i < oldFiles.length; i++)
{
if(!oldFiles[i].isFile() || oldFiles[i].length() <= size_limit) //skip files that are smaller than limit
continue;
count++;
newFile = new File(destFolder.getAbsolutePath() + File.separator + oldFiles[i].getName());
try
{
while (newFile.createNewFile())
{
reader = new BufferedReader(new FileReader(oldFiles[i]));
writer = new BufferedWriter(new FileWriter(newFile));
while ((n = reader.read(buffer)) != -1 )
{
writer.write(buffer, 0, n);
}
reader.close();
writer.close();
oldFiles[i].delete();
}
}
catch (IOException ioEx)
{
System.out.println(ioEx.getMessage());
}
}
try
{
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.required", "true");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)}; //address of sender
msg.setRecipients(Message.RecipientType.TO, address); //receiver email
msg.setSubject(subject); msg.setSentDate(new Date()); //message send date
msg.setText(messageText); //actual message
Transport transport=mailSession.getTransport("smtp"); //server through which we are going to send msg
transport.connect(host, user, pass); //we need because we have to authenticate sender email and password
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("Email send successfully to " + to);
}
catch(Exception ex)
{
System.out.println(ex); //if any error occur then error message will print
}
System.out.println("Moved " + count + " file(s) to " + destFolder.getAbsolutePath());
System.exit(0);
}
}
Note: Changed the code to string to File, your tring to assign the string to File reference which is giving error.
Please enable less secure apps settings in email account inorder to access it from the program
Comment here if you have any concerns
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.