Results 1 to 3 of 3
  1. #1
    annedroid is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default need help getting file name from command line arguments

    The task I have to do is to read in a file name from the command prompt, do a histogram equalization on it and then save it as a new file with '-equalized' in the name, eg. if the original file was 'original.jpg' the new file would be 'original-equalized.jpg'.
    I can't seem to get it to find the file I've created. No matter what I type it just prints "file not found" to the screen. It doesn't actually have any of the equalization stuff in it yet, I just wanted to make sure I could save the file first. If I type the name of my image "fun.jpeg" directly into the code it works, but when I type that into the command prompt it won't run.
    Any suggestions as to what I'm doing wrong?

    Java Code:
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class filename {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		String inputFileName = args[0];
    		BufferedImage inputFile = null;
    		try {
    			inputFile = ImageIO.read(new File(inputFileName));
    		} catch (IOException e) {
    			System.out.println("file not found");
    		}
    		if (inputFile != null) {
    
    			BufferedImage outputFile = inputFile;
    			String outputFileName = "";
    			String extension = "";
    			for (int i = 0; i < inputFileName.length(); i += 1) {
    				int j = 0;
    				char c = inputFileName.charAt(j);
    				while (c != '.') {
    					outputFileName = outputFileName + c;
    					j += 1;
    					c = inputFileName.charAt(j);
    				}
    				while (j < inputFileName.length()) {
    					j += 1;
    					c = inputFileName.charAt(j);
    					extension = extension + c;
    
    				}
    			}
    			File f = new File(outputFileName + "-equalized" + extension);
    			try {
    				ImageIO.write(outputFile, "JPEG", f);
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    
    		}
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: need help getting file name from command line arguments

    I can't seem to get it to find the file I've created.
    So you should remove extraneous code and concentrate on finding the file based on the command line argument.

    You can use System.out.println() to print the File instance you are using, instead of (or as well as) sending it to ImageIO.read(). There is a getAbsolutePath() method of the File class that will help here. Does the specify the File specify the file you intended?

    Another thing that ought to be tested (by printing it to System.out) is the value of inputFileName. Is this the value you intend? Console programs parse and interpret the command line before your program sees it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: need help getting file name from command line arguments

    I'm going to move this to New To Java.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Command-Line Arguments
    By smvolt in forum Eclipse
    Replies: 6
    Last Post: 12-21-2011, 01:48 PM
  2. command line arguments
    By shruthichandru in forum New To Java
    Replies: 11
    Last Post: 11-20-2010, 02:02 AM
  3. command line arguments
    By evermore in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 05:08 AM
  4. Command-Line Arguments
    By mustachMan in forum New To Java
    Replies: 5
    Last Post: 02-26-2010, 03:13 AM
  5. arguments in command-line
    By girlet18 in forum New To Java
    Replies: 2
    Last Post: 01-21-2010, 02:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •