Jump to content

Java Help


nandrolone

Recommended Posts

18 hours ago, liverare said:

I waited before now because I don't want to do your work for you. I did release a thread on how to copy a script (packed in a jar file) to OSBot's script folder, which would help you find out some of that stuff. But in relation to your assignment, I've wrote the following:

Note: the "Take extra care about which streams you need if you want to handle both binary and text files" has sort of been addressed in that, the file you chose to copy will have its type factored into the the new file when copying, because I've used JFileChooser.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Test {
	
	public static void main(String[] args) throws IllegalArgumentException, FileNotFoundException, IOException {
		
		File source;
		File destination;
		String fileExtension;
		FileFilter fileFilter;
		
		// get source
		
		source = selectFileToBeCoppied();
		
		if (source == null) {
			System.err.println("No source file selected!");
			System.exit(0);
		}
		
		// create file filter
		
		fileExtension = getFileExtension(source);
		
		fileFilter = createFileFilter(fileExtension);
		
		// get destination
		
		destination = selectDestinationToCopyTo(fileFilter);
		
		if (destination == null) {
			System.err.println("No destination file selected!");
			System.exit(0);
		}
		
		// make sure destination includes extension (because users sometimes forget to include it in themselves)
		
		if (fileExtension != null && !destination.getName().endsWith("." + fileExtension)) {
			
			destination = new File(destination.getParentFile(), destination.getName() + "." + fileExtension);
		}
		
		// hope destination file doesn't exist OR request permission to overwrite file
		
		if (!destination.exists() || requestPermissionToOverwriteFile()) {
			
			copyFiles(source, destination);
		}
	}

	/**
	 * Allow the user to specify the file that is to be copied
	 * 
	 * @return selectedFile
	 */
	private static File selectFileToBeCoppied() {
		
		File selectedFile = null;
		JFileChooser fileChooser = new JFileChooser();
		
		if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			selectedFile = fileChooser.getSelectedFile();
		}
		
		return selectedFile;
	}

	/**
	 * If you chose to copy a text file (.txt) then it makes sense to save it as a
	 * text file also. Conversely, if the file has no file extension, then we want
	 * to also copy it without an extension.
	 * 
	 * @param fileExtension
	 *            - file extension
	 * @return fileFilter
	 */
	private static FileFilter createFileFilter(String fileExtension) {
		
		FileFilter fileFilter = null;
		
		if (fileExtension != null) {
			
			fileFilter = new FileNameExtensionFilter(fileExtension, fileExtension);
			
		} else {
			
			fileFilter = new FileFilter() {
				
				@Override
				public String getDescription() {
					
					return "No file type selected";
				}
				
				@Override
				public boolean accept(File f) {
					
					return !f.getName().contains(".");
				}
			};
		}
		
		return fileFilter;
	}
	
	/**
	 * Extract the extension of a file
	 * 
	 * @param file
	 *            - File to analyse
	 * @return fileExtension
	 */
	private static String getFileExtension(File file) {
		String fileExtension = null;
		String fileName = file.getName();
		int dotIndex = fileName.lastIndexOf(".");
		if (dotIndex >= 0) {
			fileExtension = fileName.substring(dotIndex + 1);
		}
		return fileExtension;
	}
	
	/**
	 * Allow the user to specify the destination to where the file is to be copied
	 * 
	 * @return selectedFile
	 */
	private static File selectDestinationToCopyTo(FileFilter fileFilter) {
		
		File selectedFile = null;
		JFileChooser fileChooser = new JFileChooser();
		
		fileChooser.setFileFilter(fileFilter);
		
		if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
			
			selectedFile = fileChooser.getSelectedFile();
		}
		
		return selectedFile;
	}
	
	/**
	 * Warn users when a file is being overwritten because of copying. This will
	 * happen if the destination file already exists. In such cases, you can ask the
	 * user whether they want to continue with the writing or abort.
	 * 
	 * @return <tt>User has granted permission to overwrite a file</tt>
	 */
	private static boolean requestPermissionToOverwriteFile() {
		return JOptionPane.showConfirmDialog(null, "File already exists.\nDo you want to replace it?", "Confirm",
				JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION;
	}
	
	/**
	 * Handle both binary and text files. Take extra care about which streams you
	 * need if you want to handle both binary and text files
	 * 
	 * @param source - File source (file/folder)
	 * @param destination - File destination
	 * @throws IllegalArgumentException - Bad arguments
	 * @throws FileNotFoundException - No file found
	 * @throws IOException - Failed to copy
	 */
	private static void copyFiles(File source, File destination)
			throws IllegalArgumentException, FileNotFoundException, IOException {
		
		byte[] buffer;
		int length;
		
		if (source.equals(destination)) {
			
			throw new IllegalArgumentException("Source and destination files cannot be the same");
			
		} else {
			
			buffer = new byte[1024];
			
			try (
					FileInputStream in = new FileInputStream(source);
					FileOutputStream out = new FileOutputStream(destination);
					) {
				
				while ((length = in.read(buffer)) > 0) {
					
					out.write(buffer, 0, length);
					
				}
			}
		}
		
	}
	
}

Damn overkill <3

 

Link to comment
Share on other sites

I kind of just look around at everybodies post to see how they do things, the issues they have, and what the solutions are. I’m also new and in the process of learning. Every now and then I see something like this and it just makes me :???:

   Idk if it’s the learning curve, but it’s fairly difficult to grasp some of the things that are talked about around here! Lol some people are posting long codes about foreign concepts like Gaussian things and what not - meanwhile I’m over here

 if(grounditem.closest(“bones”)!=null){grounditem.closest(“bones”).interact(“take”);}

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...