Java Compilation

Ads
 

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
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
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 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
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
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 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
java  diff bt core java and java
java
java  what is java
java
java  after compilation of program files ,following error messages were showen on command prompt.identify the causes for those specific errors. i)testing.java:8:package system does not exist. System.out.println("No of items
java
java  after compilation of progra files, following error messages were shown on command prompt.identify the causes for those specific errors. i) System.out.println("No of items:"tooa1Item); ^ 1 error ii
JAVA
JAVA  how the name came for java language as "JAVA
java
java   why iterator in java if we for loop

Ads