简体   繁体   中英

Issues calling a method in a simple file copy java program

import java.io.*;
import java.util.StringTokenizer;

class FileCopy
{
        public static void main(String[] args) throws IOException
        {
                String infile = null;
                String outfile = null;
                BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));    

                if (args.length >= 2) //both files given via command line
                {
                        infile = args[0];
                        if (fileExists(infile) == false)
                        {
                                infile = getInputFile();
                        }
                        outfile = args[1];
                }
                else if (args.length == 1) //input file given via command line
                {
                        infile = args[0];
                        outfile = getOutputFile();
                }
                else //no files given on command line
                {
                        infile = getInputFile();
                        outfile = getOutputFile();
                }

                //create file objects to use
                File in = new File(infile);
                File out = new File(outfile);

                /*
                 *rest of code
                 */
        }

        //get the input file from the user if given file does not exist
        public static String getInputFile() throws IOException
        {
                BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                String fileName = null;
                boolean haveFile = false;

                do
                {
                        System.out.println("Enter a valid filename:");
                        System.out.print(">> ");
                        fileName = stdin.readLine();
                        haveFile = fileExists(fileName);
                }while(haveFile == false);

                return fileName;        
        }

        //get the output file and test things
        public static String getOutputFile()
        {
                return null;
        }

        //check the given file to see if it exists in the current working directory
        public static boolean fileExists(String n)
        {
                boolean exist = false;
                if (n.length() != 0)
                {
                        File tmp = new File(n);
                        exist = tmp.exists();
                }
                return exist;
        }
}

I'm having issues when calling fileExists in line 53. The syntax is correct as far as I can tell, but it just stays in the do loop indefinitely. Stepping through the program in Jcreator isn't turning up any solutions, although intriguingly the program steps right past line 53 without jumping to fileExists. Any ideas what is wrong?

After File tmp = new File(n); , add System.out.println(tmp.getAbsolutePath()); . Chances are it will immediately point out what is going wrong in the path filename.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM