File Copier Java Exercise

Exercise

File Copier

Objetive

Create a program to copy a source file to a destination file. You must use FileStream and a block size of 512 KB. An example usage might be:

mycopy file.txt e:\file2.txt

The program should handle cases where the source file does not exist, and it should warn the user (but not overwrite) if the destination file already exists.

Example Code

public class Main
{
    public static void main(String[] args)
	{
		final int BUFFER_SIZE = 512 * 1024;

		byte[] data = new byte[BUFFER_SIZE];

		java.io.FileInputStream inFile = File.OpenRead("1.exe");
		java.io.FileOutputStream outFile = File.Create("1-copy.exe");

		int amountRead;

		do
		{
			amountRead = inFile.read(data, 0, BUFFER_SIZE);
			outFile.write(data, 0, amountRead);
		} while (amountRead == BUFFER_SIZE);

		inFile.close();
		outFile.close();
	}
}

More Java Exercises of File Management

Writing To A Text File
Create a program to ask the user for several sentences (until they just press Enter) and store them in a text file named "sentences.txt"...
Appending To A Text File
Create a program to ask the user for several sentences (until they just press Enter) and store them in a text file named "sentences.txt". If the file ...
Display File Contents
Create a program to display all the contents of a text file on screen (note: you must use a StreamReader). The name of the file will be entered in the...
Extended Texttohtml (Files)
Expand the TextToHtml class, so that ir can dump it result to a text file. Create a method ToFile, which will receive the name of the file as a parame...
Logger
Create a class Logger, with a static method Write, which will append a certain text to a file: Logger.Write("myLog.txt", "This text is being logged");...
More
Create a program which behaves like the Unix command "more": it must display the contents of a text file, and ask the user to press Enter each time th...
Text Replacer
Create a program to replace words in a text file, saving the result into a new file. The file, the word to search and word to replace it with must ...
Count Letters In A File
Create a program to count the amount of times that a certain character is inside a file (of any kind). The file and the letter can be asked to the ...
Reading A Binary File (1: BMP)
Create a Java program to check if a BMP image file seems to be correct. It must see if the first two bytes are B and M (ASCII codes 0x42 and 0x4D)....
Writing To A Binary File
Create a program which asks the user for his name, his age (byte) and the year in which he was born (int) and stores them in a binary file. Create ...
Java To Java
Create a basic Java to Java translator. It must accept a Java source files, and create an equivalent Java source file. It will receive the file nam...
Invert A Text File
Create a program to "invert" the contents of a text file: create a file with the same name ending in ".tnv" and containing the same lines as the origi...
Reading A Binay File (2 - GIF)
Create a Java program to check if a GIF image file seems to be correct. It must see if the first four bytes are G, I, F, 8. In case it seems cor...
Friends Database, Using Files
Expand the "friends database", so that it loads data from file at the beginning of each session (if the file exists) and saves the data to file when t...
Pascal To Java Translator
Create a basic Pascal to Java translator. It will accept program such as: example program; var i: integer; max: integer; begin writeLn("Ho...
Convert A Text File To Uppercase
Write a program to read a text file and dump its content to another file, changing the lowercase letters to uppercase. You must deliver only the "....
Convert Any File To Uppercase
Write a program to read a file (of any kind) and dump its content to another file, changing the lowercase letters to uppercase. You must deliver on...
File Inverter
Create a program to "invert" a file: create a file with the same name ending in ".inv" and containing the same bytes as the original file but in rever...
File Encrypter
Create a program to encrypt a text file into another text file. It must include the encrypter class you have created previously (in January 17th)...
Count Words
Create a Java program to count the amount of words stored in a text file...
BMP Width And Height, Binaryreader
Create a Java program to display the width and height of a BMP file using a BinaryReader. The structure of the header of a BMP file is: File typ...
TXT To HTML Translator
Create a "Text to HTML converter", which will read a source text file and create a HTML file from its contents. For example, if the file contains: Ho...
Invert Binary File V2
Create a program to "invert" a file using a "FileStream". The program should create a file with the same name ending in ".inv" and containing the same...
BMP Width & Height, Filestream
Create a Java program to display the width and height of a BMP file using a FileStream. Remember the structure of the BMP header: File type (let...
MP3 Reader
ID3 specifications apply to any file or audiovisual container, but they are primarily used with audio containers. There are three compatible versions ...
C To Java Converter
Create a program to convert simple C programs, such as the following one, to Java: Note: the resulting program must compile correctly. Test it with o...
File Splitter
Create a program to split a file (of any kind) into pieces of a certain size. It must receive the name of the file and the size as parameters. For exa...
Encrypt A BMP File
Create a program to encrypt/decrypt a BMP image file by changing the "BM" mark in the first two bytes to "MB" and vice versa. Use the advanced File...
CSV Converter
The CSV ("Comma Separated Values") is an exchange format used by many spreadsheet and database management systems. It consists of a series of comma-se...
File Comparer
Create a Java program to tell if two files (of any kind) are identical (have the same content)....
Display BPM On Console
The Netpbm format is a family of image file formats designed with simplicity in mind, rather than small size. They can represent color, grayscale, or ...
PCX Width And Height
Create a program that checks if a file is a PCX image and, if so, displays its width and height using the following specification: What is the PCX ...
Extract Text From A Binary File
Create a program that extracts only the alphabetic characters contained in a binary file and dumps them to a separate file. The extracted characters s...
Java To Pascal Converter
Create a program that converts simple Java programs, such as the following one, to the Pascal language....
Dump
Create a "dump" utility: a hex viewer that displays the contents of a file, with 16 bytes in each row and 24 rows in each screen. The program should p...
DBF Extractor
Create a program that displays the list of fields stored in a DBF file. The DBF format is used by the old dBase database manager and is still suppo...
Text Censorer
Create a program to censor text files. It should read a text file and dump its results to a new text file, replacing certain words with "[CENSORED]". ...
SQL To Text
You must create a Java program that is capable of parsing SQL INSERT commands and extracting their data into separate lines of text, as follows. If th...
PGM Viewer
The PGM format is one of the versions of NetPBM image formats. Specifically, it is the variant capable of handling images in shades of gray. Its he...
Display BMP On Console V2
Create a program to display a 72x24 BMP file on the console. You must use the information in the BMP header (refer to the exercise of Feb. 7th). Pay a...