CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Juan Gallardo
Level 10
Edmonton
  • 16.12.2018
  • 1173views
  • 2comments

Let's add my solution to the list of solutions that don't meet the third requirement

Question about the task Let's make the code do something useful!
Java Syntax,  Level 9,  Lesson 11
Under discussion

Task: The program reads in two file names. It copies the first file to the location specified by the second file name.
New task: The program reads in two file names. It copies the first file to the location specified by the second file name.
If the specified file (to be copied) does not exist, then the program should display "File does not exist." and read a file name one more time and only then read the destination file name.

Requirements:
  • The program must read file names.
  • The main method must handle exceptions thrown by the getInputStream method. If an exception occurs, you should display "File does not exist.".
  • The program must copy the contents of the first file to the second one.
  • The main method should call the getInputStream method.
  • Don't change the getInputStream method.
  • The main method should call the getOutputStream method.
  • Don't change the getOutputStream method.
package com.codegym.task.task09.task0929; import java.io.*; /* Let's make the code do something useful! */ class Counter { static int i = 0; } public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String sourceFileName = reader.readLine(); String destinationFileName = reader.readLine(); tryThis(sourceFileName, destinationFileName); } static void tryThis(String iS, String oS) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); if(Counter.i == 0) { try { copyFile(getInputStream(iS), getOutputStream(oS)); } catch (Exception e) { Counter.i++; System.out.println("File does not exist."); String iS2 = reader.readLine(); //String oS2 = reader.readLine(); tryThis(iS2, oS); } } } static void copyFile(InputStream iSt, OutputStream oSt) throws IOException { while (iSt.available() > 0) { int data = iSt.read(); oSt.write(data); } iSt.close(); oSt.close(); } public static InputStream getInputStream(String fileName) throws IOException { return new FileInputStream(fileName); } public static OutputStream getOutputStream(String fileName) throws IOException { return new FileOutputStream(fileName); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
17 December 2018, 00:34
Try this:
public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        InputStream input = null;
        for (int i = 0; i < 1; i++){
            try{
                input = getInputStream(reader.readLine());
                break;
            } catch (Exception e) {
                System.out.println("File does not exist.");
            }
        }
        OutputStream output = getOutputStream(reader.readLine());
        reader.close();
        while (input.available() > 0) {
            int data = input.read();
            output.write(data);
        }
        input.close();
        output.close();
    }


    public static InputStream getInputStream(String fileName) throws IOException {
        return new FileInputStream(fileName);
    }

    public static OutputStream getOutputStream(String fileName) throws IOException {
        return new FileOutputStream(fileName);
    }
}
0
Guadalupe Gagnon
Level 37 , Tampa, United States
17 December 2018, 00:38
I just moved all your code from the methods that you made to the main method as that is what the requirements called for. Also, I got rid of the counter class and put the code for that in a for loop as this is a structure that already is convenient for a counter. The requirements also specify that you need to read the source file, and if it doesn't read the source file one more time, then only after this read the destination file. Your code reads the source then the destination immediately following, which is probably the only thing failing your code. Hopefully this works, I'm not sure because it wont verify my code as the problem is already closed.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.