Question: I want to know how to get this Java program to read the xml file that I created. This program moves file depending of the
I want to know how to get this Java program to read the xml file that I created. This program moves file depending of the size (memory) when is done moving files it will send an email.
I'm trying to get the variables assigned from the xml file and run it with the Java program.
Before you can run this program you need to download activation.jar and javax.mail-1.4.4.jar files.
Java Programe:
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
{
@SuppressWarnings("resource")
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 ="smtp.gmaail.com";
String user = "user123@gmaail.com"; //User email address
String pass = "User123"; //Password of the email
String to; //Receiver
String from = "user123@gmaail.com"; //Message send from
String subject = "Hi User!"; //Subject of the email
//String Message = "Moved " + count + " file(s) to " + destFolder.getAbsolutePath();
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.nextLine();
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);
}
}
XML file:
size_limit=1000
srcFolder=C:\\Users\\PedroE\\Desktop\\Test
destFolder=C:\\Users\\PedroE\\Desktop\\TestMove
to=user456@hotmail.com
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
