CodeGym
促销活动
CodeGym University
学习
课程
任务
调查和小测验
游戏
帮助
提醒时间表
社区
用户
论坛
聊天
文章
成功故事
活动
评论
订阅服务
浅色主题
问题
  • 评论
  • 关于我们
开始
开始学习
立即开始学习
  • 全部问题
PeaceLovePang
第 14 级
Warrington
  • 04.06.2020
  • 477次阅读
  • 1个评论

Can't find what the problem is, please help me look。

有关 任务的问题 让代码发挥它的用武之地!
Java 语法,  第 9 级,  课程 11
正在讨论


任务:程序读取两个文件名。它将第一个文件复制到第二个文件名指定的位置。
新任务:程序读取两个文件名。它将第一个文件复制到第二个文件名指定的位置。
如果指定的文件(待复制)不存在,那么该程序应显示“文件不存在”并再次读取文件名,然后才读取目标文件名称。

要求:
  • 程序必须读取文件名。
  • main 方法必须处理 getInputStream 方法抛出的异常。如果发生异常,则应显示“文件不存在”。
  • 程序必须将第一个文件的内容复制到第二个文件。
  • main 方法应调用 getInputStream 方法。
  • 不要更改 getInputStream 方法。
  • main 方法应调用 getOutputStream 方法。
  • 不要更改 getOutputStream 方法。
package zh.codegym.task.task09.task0929; import java.io.*; /* 让代码发挥它的用武之地! */ 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(); //fileInputStream = null; try{ InputStream fileInputStream1 = getInputStream(sourceFileName); OutputStream fileOutputStream = getOutputStream(destinationFileName); while (fileInputStream1.available() > 0) { int data = fileInputStream1.read(); fileOutputStream.write(data); } fileInputStream1.close(); fileOutputStream.close(); } catch (Exception e){ System.out.println("文件不存在"); sourceFileName = reader.readLine(); InputStream fileInputStream = getInputStream(sourceFileName); OutputStream fileOutputStream = getOutputStream(destinationFileName); while (fileInputStream.available() > 0) { int data = fileInputStream.read(); fileOutputStream.write(data); } fileInputStream.close(); fileOutputStream.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
评论 (1)
  • 受欢迎
  • 新
  • 旧
你必须先登录才能发表评论
Artem Divertitto Senior Android Developer 位于 United Tech
5 六月 2020, 06:45
destinationFileName you have to read only after sourceFileName
0
了解
  • 注册
  • Java 课程
  • 任务帮助
  • 定价
  • 游戏项目
  • Java 语法
社区
  • 用户
  • 文章
  • 论坛
  • 聊天
  • 成功故事
  • 活动
  • Affiliate Program
公司
  • 关于我们
  • 联系方式
  • 评论
  • 新闻发布室
  • CodeGym 教育
  • 常见问题
  • 支持
CodeGym CodeGym 是一个从零开始学习 Java 语言编程的在线课程。本课程是初学者掌握 Java 语言的绝佳方式。它包含 1200 多个可即时验证的任务,以及基本范围内的 Java 基础理论。为了帮助你在教育上取得成功,我们实现了一组激励功能:小测验、编码项目以及有关高效学习和 Java 语言开发人员职业方面的内容。
关注我们
界面语言
程序员不是天生的 © 2023 CodeGym
MastercardVisa
程序员不是天生的 © 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.