CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Kaiyr Shayakhmetov
Level 22
  • 16.08.2020
  • 706views
  • 2comments

The code seems working but fails to pass verification

Question about the task Building a file
Java Core,  Level 8,  Lesson 11
Resolved

Let's build a file from various pieces.
Read file names from the console.
Each file has a name: <someName>.partN.

For example, Lion.avi.part1, Lion.avi.part2, ..., Lion.avi.part37.

The file names are supplied in random order. The word "end" is used to stop reading in file names.
In the folder where all the files are located, create a file without the "part" suffix, i.e. without ".<partN>".

For example, Lion.avi.

Use a buffer to copy all the bytes from the partial files to the created file.
Copy the first in the proper order, first the first part, then the second, ..., finally - the last part.
Close the streams.

Requirements:
  • The program must read file names from the console until the word "end" is entered.
  • Create a stream to write to the file without the "part" suffix (".<partN>") in the folder with all the "part" files.
  • Copy all the bytes from the *.partN files to the new file.
  • You should use a buffer for the reading and writing.
  • The file streams must be closed.
  • Don't use static variables.
package com.codegym.task.task18.task1825; import java.io.*; import java.util.*; /* Building a file */ public class Solution { public static void main(String[] args) throws IOException { String prefix="C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task18\\task1825\\"; //read file names from the console //read in file names Scanner scanner=new Scanner(System.in); ArrayList<String> fileNames=new ArrayList<>(); String temp; while (!(temp=scanner.nextLine()).equals("")){ if (temp.equals("end")) { scanner.close(); break; } else { fileNames.add(temp); System.out.println(temp); } } //extract the fileName without part# String tempFileName=fileNames.get(0); int index=tempFileName.indexOf("part"); String fileToWrite=tempFileName.substring(0,index-1); TreeMap<Integer, String> map=new TreeMap<>(); for (String fileName : fileNames) { String[] split = fileName.split("\\."); int key = Integer.parseInt(String.valueOf(split[2].charAt(split[2].length() - 1))); map.put(key, fileName); } FileOutputStream fileOutputStream=new FileOutputStream(new File(prefix+fileToWrite), true); BufferedOutputStream bos=new BufferedOutputStream(fileOutputStream, 1000); readFile(map, bos); bos.close();fileOutputStream.close(); } public static void readFile(TreeMap<Integer, String> map, BufferedOutputStream bos) throws IOException { String prefix="C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task18\\task1825\\"; for (Map.Entry<Integer, String> pair:map.entrySet()){ FileInputStream fileInputStream=new FileInputStream(prefix+pair.getValue()); BufferedInputStream reader=new BufferedInputStream(fileInputStream, 1000); byte[] buffer = new byte[reader.available()]; byte read; while ((read = (byte)reader.read(buffer, 0, buffer.length)) > 0) { bos.write(buffer, 0, read); } reader.close(); fileInputStream.close(); } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
17 August 2020, 18:02
#1 get rid of prefix in both locations. This code does not work on my computer and it should. It will not work for the validation. #2 along with the first point, splitting by decimal at line 38 wont result in exactly 3 values in the array on different computers. On my computer it throws an exception at that line and most likely will on any computer #3 line 39 only gets the last digit of "partN", however there could hundreds or thousands of parts. The code should be able to get the correct part number to add to the tree
0
Kaiyr Shayakhmetov
Level 22
18 August 2020, 13:16
Hi, Thank you so much. Tried your recommendations and finally managed to pass validation.
+1
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.