I can't pass the test with this code.

/* Add new functionality to the program.
Old task: The program should read from the keyboard two file names and to copy the first file to the location specified by the second name.
    New task: The program should read from the keyboard two file names and to copy the first file to the location specified by the second name.
    If the file (that is going to be copied) with the specified name doesn’t exist, the program should display to the screen the message «File does not exist»
     and try to read the file name from the console once again before reading the name of second (destination) file.
*/

public class Solution
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        FileInputStream fileInputStream = null;
        try
        {
            String sourceFileName = reader.readLine();
            fileInputStream = new FileInputStream(sourceFileName);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File does not exist");
            String sourceFileName = reader.readLine();
            fileInputStream = new FileInputStream(sourceFileName);
        }

        String destinationFileName = reader.readLine();
        FileOutputStream fileOutputStream = new FileOutputStream(destinationFileName);

        while (fileInputStream.available() > 0)
        {
            int data = fileInputStream.read();
            fileOutputStream.write(data);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }
}

asked 06 Aug '17, 16:24

genek's gravatar image

genek
172
accept rate: 0%

edited 06 Aug '17, 16:25


It is necessary to put at the end point "File does not exist." !!!

link

answered 15 Feb, 14:14

lacoste89's gravatar image

lacoste89
1
accept rate: 0%

Необходимо в конце сообщения поставить точку "File does not exist." !!!

(15 Feb, 14:15) lacoste89
String sourceFileName = null;
        while (true){
            sourceFileName = reader.readLine();
            try
            {
                FileInputStream fileInputStream1 = new FileInputStream(sourceFileName);
                fileInputStream1.close();
                break;
            }
            catch (FileNotFoundException e)
            {
                System.out.println("File does not exist.");
            }
        }
link

answered 15 Feb, 14:17

lacoste89's gravatar image

lacoste89
1
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×11
×9
×2

Asked: 06 Aug '17, 16:24

Seen: 545 times

Last updated: 15 Feb, 14:17