0% found this document useful (0 votes)
31 views10 pages

Project 4: 1 OUTPUT: First Run

This document contains the source code and output logs from 5 runs of a program that copies an existing text file to a new file. The program prompts the user to enter the names of the existing and new files. If the original file does not exist or is not readable, the program exits. If the new file already exists, the user is prompted to either exit, overwrite, or enter a new file name. The existing file is then copied line by line to the new file.

Uploaded by

Elena Walls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Project 4: 1 OUTPUT: First Run

This document contains the source code and output logs from 5 runs of a program that copies an existing text file to a new file. The program prompts the user to enter the names of the existing and new files. If the original file does not exist or is not readable, the program exits. If the new file already exists, the user is prompted to either exit, overwrite, or enter a new file name. The existing file is then copied line by line to the new file.

Uploaded by

Elena Walls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Project 4

Julio Martinez
5.24.2015

OUTPUT: First Run

run:
Enter the name of the existing file: newFile.txt
Enter the name of the new file: monkey.txt
newFile.txt does not exist.
Exiting program.
BUILD SUCCESSFUL (total time: 23 seconds)

OUTPUT: Second Run

run:
Enter the name of the existing file: myFile.txt
Enter the name of the new file: myNewMonkeyFile.txt
Copying myFile.txt into myNewMonkeyFile.txt
BUILD SUCCESSFUL (total time: 22 seconds)

OUTPUT: Third Run

run:
Enter the name of the existing file: myFile.txt
Enter the name of the new file: myNewMonkeyFile.txt
myNewMonkeyFile.txt already exists
Choose from one of the following choices:
1 Exit the program
2 Overwrite the existing file
3 Enter a new name for the file
Enter a Number: 1
Exiting Program
BUILD SUCCESSFUL (total time: 22 seconds)

OUTPUT: Fourth Run

run:
Enter the name of the existing file: myFile.txt
Enter the name of the new file: myNewMonkeyFile.txt
myNewMonkeyFile.txt already exists
Choose from one of the following choices:
1 Exit the program
2 Overwrite the existing file
3 Enter a new name for the file
Enter a Number: 2
Overwriting myNewMonkeyFile.txt
Copying myFile.txt into myNewMonkeyFile.txt
BUILD SUCCESSFUL (total time: 22 seconds)

OUTPUT: Fifth Run

run:
Enter the name of the existing file: myFile.txt
Enter the name of the new file: myNewMonkeyFile.txt
myNewMonkeyFile.txt already exists
Choose from one of the following choices:
1 Exit the program
2 Overwrite the existing file
3 Enter a new name for the file
Enter a Number: 3
Enter a new name for the file: yetAnotherFile.txt
Copying myFile.txt into yetAnotherFile.txt
BUILD SUCCESSFUL (total time: 27 seconds)

SOURCE CODE

package p r o j e c t 4 ;
import
import
import
import

java .
java .
java .
java .

u t i l . Scanner ;
i o . FileNotFoundException ;
io . PrintWriter ;
io . File ;

/
Purpose : Copy e x i s t i n g t e x t f i l e t o a new f i l e
Date o f l a s t r e v i s i o n : May . 2 4 . 2 0 1 5
@version Project 4 c l a s s
@author J u l i o M a r t i n e z
/
public c l a s s P r o j e c t 4 {

public s t a t i c void main ( S t r i n g [ ] a r g s )


{
/Part ( 1 )
Prompts u s e r t o i n p u t e x i s t i n g f i l e and
new f i l e t o be c o p i e d t o
/
Scanner keyboard = new Scanner ( System . i n ) ;
System . out . p r i n t ( Enter t h e name o f t h e e x i s t i n g f i l e : ) ;
S t r i n g f i l e N a m e = keyboard . n e x t L i n e ( ) ;
System . out . p r i n t ( Enter t h e name o f t h e new f i l e : ) ;
S t r i n g newFileName = keyboard . n e x t L i n e ( ) ;
System . out . p r i n t l n ( ) ;
// C r e a t e s F i l e o b j e c t f o r e x i s t i n g f i l e
F i l e f i l e O b j e c t = new F i l e ( f i l e N a m e ) ;
/Part ( 2 )
Checks f o r e x i s t e n c e o f o r i g i n a l f i l e
and c h e c k s i f o r i g i n a l f i l e i s r e a d a b l e
I f e i t h e r d o e s not pass , program i s a b o r t e d
O t h e r w i s e t h e program c o n t i n u e s

/
if (! fileObject . exists ())
{
System . out . p r i n t l n ( f i l e N a m e +
System . out . p r i n t l n ( \ n E x i t i n g
System . e x i t ( 0 ) ;
}
e l s e i f ( ! f i l e O b j e c t . canRead ( ) )
{
System . out . p r i n t l n ( f i l e N a m e +
System . out . p r i n t l n ( \ n E x i t i n g
System . e x i t ( 0 ) ;
}

d o e s not e x i s t . ) ;
program . ) ;

i s not r e a d a b l e . ) ;
program . ) ;

// C r e a t e s F i l e o b j e c t f o r t h e new f i l e
F i l e n e w F i l e O b j e c t = new F i l e ( newFileName ) ;
// While l o o p b e l o w w i l l s t o p running when ( s t o p == t r u e )
boolean s t o p = f a l s e ;
/Part ( 3 )
Checks f o r e x i s t e n c e o f new f i l e t o c r e a t e / copy t o
i f t h e new f i l e a l r e a d y e x i s t s t h e program g i v e s t h e
3 d ir ec t ed options to user
/
while ( ! s t o p )
{
// i f f i l e e x i s t s t h i s prompts t h e u s e r t o s e l e c t 1 ,2 or 3
i f ( newFileObject . e x i s t s ( ) )
{
System . out . p r i n t l n ( newFileName + a l r e a d y e x i s t s ) ;
System . out . p r i n t l n ( \ nChoose from one o f t h e f o l l o w i n g
+ choices : );
System . out . p r i n t l n ( 1
E x i t t h e program ) ;
System . out . p r i n t l n ( 2
Overwrite the e x i s t i n g f i l e ) ;
System . out . p r i n t l n ( 3
Enter a new name f o r t h e f i l e ) ;
System . out . p r i n t ( \ nEnter a Number : ) ;
// t a k e s i n p u t 1 ,2 or , 3 from u s e r
int c h o i c e = keyboard . n e x t I n t ( ) ;
7

// 3 O p t i o n s v i a a s w i t c h s t a t e m e n t
switch ( c h o i c e )
{
case 1 :
{
System . out . p r i n t l n ( \ n E x i t i n g Program ) ;
System . e x i t ( 0 ) ;
break ;
}
case 2 :
{
System . out . p r i n t l n ( \ n O v e r w r i t i n g
+ newFileName ) ;
// s e t s t o p t o t r u e t o s t o p running w h i l e l o o p
s t o p = true ;
break ;
}
case 3 :
{
keyboard . n e x t L i n e ( ) ;
System . out . p r i n t ( \ nEnter a new name f o r t h e
+ file : );
newFileName = keyboard . n e x t L i n e ( ) ;
n e w F i l e O b j e c t = new F i l e ( newFileName ) ;
break ;
/ s t o p i s s t i l l f a l s e , so t h i s c h e c k s a g a i n
i f f i l e e x i s t s /
}
}
}
else
{
/ I f we e n t e r t h e e l s e c a s e t h e n t h i s means
t h e f i l e d o e s not e x i s t , so we s e t s t o p t o t r u e
t o e x i t t h e w h i l e l o o p and c o n t i n u e on /
s t o p = true ;
}
}
/ S i n c e we have r e a c h e d t h i s p o i n t , we a r e r e a d y t o
8

copy e x i s t i n g f i l e t o new f i l e , so we inform t h e u s e r


/
System . out . p r i n t l n ( Copying + f i l e N a m e + i n t o +
newFileName ) ;
/Part ( 4 ) C r e a t e s / O v e r w r i t e s and Opens New F i l e
/
P r i n t W r i t e r outputStream = null ;
try
{
outputStream = new P r i n t W r i t e r ( newFileName ) ;
}
catch ( FileNotFoundException e )
{
System . out . p r i n t l n ( E r r o r o p e n i n g t h e f i l e + newFileName ) ;
System . e x i t ( 0 ) ;
}
/Part ( 5 ) Opens and Connects t o O r i g i n a l F i l e f o r Reading
/
Scanner inputStream = null ;
try
{
inputStream = new Scanner ( f i l e O b j e c t ) ;
}
catch ( FileNotFoundException e )
{
System . out . p r i n t l n ( E r r o r o p e n i n g t h e f i l e + f i l e N a m e ) ;
System . e x i t ( 0 ) ;
}
/Part ( 6 ) c o p i e s t h e o r i g i n a l f i l e t o t h e new f i l e
/
while ( inputStream . hasNextLine ( ) )
{
S t r i n g l i n e = inputStream . n e x t L i n e ( ) ;
outputStream . p r i n t l n ( l i n e ) ;
}
/Part ( 7 ) we make s u r e we c l o s e t h e i n p u t and
9

o u t p u t stream i n s t e a d o f a l l o w i n g a d e f a u l t c l o s e
/
inputStream . c l o s e ( ) ;
outputStream . c l o s e ( ) ;
}
}

10

You might also like