Java Compilation

Java Compilation

I want to write a Java program that asks the user for the names of 3 files. I want the first file to be opened for reading and the other two files to be opened for writing.

I want the program to read the contents of the first file, change all characters to upper case, and store the results in the second file at the same time append the same data to the third file. I want the second file to be a copy of the first file, except that all the characters will be uppercase. I want the third file to hold the context of all input files that my program has processed.

I want to create 3 input files that can be used to test the program:
1st input file
Monday
Tuesday

2nd input file
Wednesday
Thursday

3rd input file
Friday
Saturday

My program:
import java.util.Scanner;
import java.io.*;

public class FileIO
{
public static void main(String[] args)throws IOException
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter input filename: ");
String input = kb.nextLine();
File myFile = new File(input);

if(!myFile.exists())
{
System.out.println("no input file");
System.exit(0);
}

Scanner inputFile = new Scanner(myFile);
System.out.print("Enter output filename: ");
String output = kb.nextLine();

PrintWriter outputFile = new PrintWriter(output);

while(inputFile.hasNext())
{
outputFile.println(inputFile.nextLine().toUpperCase());
}

System.out.print("Enter another output filename(append): ");
output1 = kb.nextLine();

FileWriter fwriter = new FileWriter(output1, true);
PrintWriter fileOutput = new PrintWriter(fwriter);

while(inputFile.hasNext())
{
fileOutput.println(inputFile.nextLine().toUpperCase());
}
inputFile.close();
outputFile.close();

}
}
View Answers

March 4, 2010 at 2:34 PM

Hi Friend,

Try the following code:

import java.util.*;
import java.io.*;

public class FileIO{
public static void main(String args[]) throws IOException {
try {
Scanner input=new Scanner(System.in);
System.out.print("Enter input filename: ");
String in=input.nextLine();
File f1=new File(in);
System.out.print("Enter output filename: ");
String out1=input.nextLine();
File f2=new File(out1);
System.out.print("Enter another output filename(append): ");
String out2=input.nextLine();
File f3=new File(out2);
BufferedReader reader = new BufferedReader(new FileReader(f1));
String line=null;
while ((line=reader.readLine())!=null){
FileWriter fstream = new FileWriter(f2,true);
BufferedWriter output = new BufferedWriter(fstream);
output.write(line.toUpperCase());
output.newLine();
output.close();

FileWriter fstream1 = new FileWriter(f3,true);
BufferedWriter output1 = new BufferedWriter(fstream1);
output1.write(line.toUpperCase());
output1.newLine();
output1.close();
}
reader.close();
}catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
java Compilation
java program compilation time following error is occur in my system Note...java Compilation  import java .util.*; class ArrayListDemo... in Java 5 and later if you're using collections without type specifiers (e.g.
java Compilation
java program compilation time following error is occur in my system Note...java Compilation  import java .util.*; class ArrayListDemo... in Java 5 and later if you're using collections without type specifiers (e.g.
Advertisements
java Compilation
java program compilation time following error is occur in my system Note...java Compilation  import java .util.*; class ArrayListDemo... in Java 5 and later if you're using collections without type specifiers (e.g.
Java compilation error - Java Beginners
Java compilation error  Method overridden problems in compile time error
java compilation error - Hibernate
java compilation error  hi, i have made an registration page whosevalues should be inserted into database. i have used hibernate and eclipse it is working fine. but when i make a war file and deploy it using ant and then run
compilation error - Java Beginners
compilation error  class s { public static... is the reason?  Hi Friend, In Java there are some specific literal... not in the Hexadecimal or Octal. Here is fraction of Java code example which will help you
Java Compilation - Java Beginners
Java Compilation  How would i write a complete java program which, using a do-while loop, reads integers from the keyboard and sums them until the sum hits or exceeds 30. Then print out the sum. is it like this: import
Java Compilation - Java Beginners
Java Compilation  I want to write a complete java program that catenates the file named "first" with the named "second" and produces a file named "third". If either input file "first" or "second" does not exist in the current
Java Compilation - Java Beginners
Java Compilation  I want to write a Java program that asks the user for the names of 3 files. I want the first file to be opened for reading and the other two files to be opened for writing. I want the program to read
java compilation error - Java Beginners
java compilation error  i have downloaded java software(version 6... from where i should download java or what to do now?  Have you set the java_home and path in your environmental variables in "My Computer
Java Compilation error. Hibernate code problem. Struts first example - Hibernate
Java Compilation error. Hibernate code problem. Struts first example  Java Compilation error. Hibernate code problem. Struts first example
Does the object type known during compilation in Java?.
Does the object type known during compilation in Java?.  class A { } class B extends class A { } class checkType { public static void main(String[] args) { A ObjA = new B();/// Here ObjA is of type A--> this fact
compilation problem
compilation problem  struts 2.0 : i have included all 5 jars in lib folder,but its giving compilation problem that package open symphony doesnot exist ( though i imported it) & cant recognise SUCCESS etc symbol.i think i
compilation problem
compilation problem  struts 2.0 : i have included all 5 jars in lib folder,but its giving compilation problem that package open symphony doesnot exist ( though i imported it) & cant recognise SUCCESS etc symbol.i think i
compilation fail
compilation fail  package jsptube.tutorials.servletexample; import javax.servlet.GenericServlet; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; import
compilation errors
compilation errors  package jsptube.tutorials.servletexample; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import
compilation error
compilation error  Hi my program below is not compiling,when I try to compile it i'm getting the error message "tool completed with exit code 1".The loop has to be at the end of the program can you help me. Blockquoteimport
command prompt is compilation is not working
command prompt is compilation is not working   Dear java users can anyone help me to run the program in command line prompt i tried by selecting... and the class file is also getting generated when i run the class file using java
result after compilation
c file compilation
program created and compilation error of connection rightly created n tested
program created and compilation error of connection rightly created n tested  // I have made connection as the data source dsn2 and i have also created table named Tab1 in SQL server 2008..it is compiled correctly but is giving
Hibernate Configuration
org.hibernate.SessionFactory . Various XML mapping files or Java Annotations compilation build...The configuration of Hibernate is handled by the instance of org.hibernate.cfg.Configuration . It symbolizes the complete mapping between Java data types
Java
Java  What is a compilation unit
java & c
java & c  why c language is called top down approach? why java is called bottom to top approach?   In C, compilation stats from the very top of the program code. In Java, on the other hand, we have user defined data
java fundamental question related to string
java fundamental question related to string  public class Myclass...??); } } Output? a) the pgm will throw ClassCastException b) compilation fails since s==sb illegal c) compilation fails since s.equals(sb) illegal d
java fundamental question
java fundamental question  public class WrapTest3 { Public static void main(String args[]) { String s="98.6"; // insert code here } } Which will cause compilation error: a) float f1=Float.floatValue(s); b) float f2=new Float
java fundamental question
java fundamental question  public class TestVarArgs { Public void display(Double i, double ‚?¶ j) { System.out.println(‚??Double& var args‚... Double & Double c) compilation error due to ambiguous method calls d
php java
php java  why PHP is becoming more popular than java   PHP is more popular than Java because of following reasons: 1)PHP web hosting... Java for web development: Lower barrier to entry Dynamic typing Simpler hello
What are the features of web designing
of website solely depends upon the layout, structure and compilation of company
Which is better .Net or Java?
or Ahead of Time Compilation while Java has Just In Time compilation. Java...When a developer needs to choose between Java or .NET to develop a new..., it can also be used on other operating system. Java on the other hand
Java Generics
replace a suitable java type during compilation with the help of these parametric... .style1 { list-style-type: square; } Java Generics The Generics... compilation. The main reason of using Generics code is to assures type safety

Ads