Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners FileIO Java Compilation
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Chelsea
FileIO Java Compilation
1 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners


Expert:Chelsea
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 working directory, give an appropriate error message and terminate the program.

By "catenate" I mean:
* The file "third" contains all the lines of "first" followed by all the lines of "second".
* The files "first" and "second" are unchanged.
Answers
Hi Friend,

Try the following code:

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

public class ConcatenateFiles{
public static void main(String[]args) throws Exception{

FileWriter fw = new FileWriter(new File("third.txt"),true);
BufferedWriter out = new BufferedWriter(fw);
File f1 = new File("first.txt");
if(f1.exists()){
BufferedReader freader = new BufferedReader(new FileReader(f1));
String s;
while ((s = freader.readLine()) != null) {
out.write(s);
out.newLine();

}
}

else{
System.out.println("File does not exist");
System.exit(0);
}
File f2 = new File("second.txt");

if(f2.exists()){
BufferedReader freader = new BufferedReader(new FileReader(f2));
String s;
while ((s = freader.readLine()) != null) {
out.write(s);
out.newLine();

}
out.close();
}

else{
System.out.println("File does not exist");
System.exit(0);
}
}
}

Thanks

Is there a way to compile this program without the BufferReader thing because i do not know what that is exactly?

Advertisement
View Answers

March 12, 2010 at 2:28 PM


Hi Friend,

Try the following code:

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

public class ConcatenateFiles{
public static void main(String[]args) throws Exception{
Scanner scanner;
FileWriter fw = new FileWriter(new File("third.txt"),true);
BufferedWriter out = new BufferedWriter(fw);
File f1 = new File("second.txt");
if(f1.exists()){
scanner = new Scanner(f1);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
out.write(line);
out.newLine();

}
}

else{
System.out.println("File does not exist");
System.exit(0);
}
File f2 = new File("first.txt");

if(f2.exists()){
scanner = new Scanner(f2);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
out.write(line);
out.newLine();

}
out.close();
}

else{
System.out.println("File does not exist");
System.exit(0);
}

}
}

Thanks


Related Tutorials/Questions & Answers:
FileIO Java Compilation - Java Beginners
FileIO Java Compilation  Expert:Chelsea 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
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
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 Beginners
Java Compilation  I want to write a Java program that asks the user... java.util.Scanner; import java.io.*; public class FileIO { public static...: import java.util.*; import java.io.*; public class FileIO{ public static
java compilation error - Hibernate
java compilation error  hi i am getting org.hibernate.exception.GenericJDBCException: Cannot open connection exception when runnig the code why this exception is coming
java compilation error - Applet
java compilation error  I am getting compilation error in the following code.: public void paint(Graphics g) { switch(screen) { case 0: g.drawString(line1,2,10); break; case 1
Java compilation error - Java Beginners
Java compilation error  Method overridden problems in compile time error
java compilation - Java Beginners
java compilation  compiling a program that execute name,surname...); System.out.println("College/University " + college); } } For compilation : c:\>javac Academic.java c:\>java Academic Thanks
java compilation error - Applet
java compilation error  hi friends the following is the awt front design program error, give me the replay E:\ramesh>javac...://www.roseindia.net/java/example/java/awt/ Thanks
Java Compilation error - SQL
Java Compilation error  ava.lang.NullPointerException at beanUtils.SaleHeaderUtility.getFullRemainPatientList(SaleHeaderUtility.java:754) at forms.ProcessDialog.updatePatientBalances(ProcessDialog.java:119
java compilation error,Struts2 - Struts
java compilation error,Struts2  hi, From last 3 days i m trying for image uploading through struts2 by taking the reference of file upload sample code from roseindia.net site,But i m unable to insert it into database,its
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 error - Ant
java compilation error  hi, i have a application in which i m reading from an xml file and then storing its values in database.but when i started using ant its main program is not running as it is unable to detect the jar file
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  Hello madam/sir, i m a beginners in java. My problem is that i want to connect a login form to a frame containing 4 panels. how could i do it?Please help
Java Compilation - Java Beginners
Java Compilation  How do you design a Java application that inputs a single letter and prints out the corresponding digit on a telephone? It should be done in such a way that the letters and digits on a telephone are grouped
Java Compilation - Java Beginners
Java Compilation  Dear Sir, Thanks for giving the code for the question which i posted. I went through the program "Write a Java program to read... it as simple java code. Thanks
java compilation - Java Beginners
java compilation  import java.awt.*; import javax.swing.... for java ActoinButtonsPanel  Hi Friend, Try the following code: 1)java code: import java.awt.*; import javax.swing.*; public class
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  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  Dear Sir, Could you please send me the program code for the following java program? Q:Create a classname Math with its required variables and use add(),sub(),multiply(),divide(),modulo() methods? Thank
Java Compilation - Java Beginners
Java Compilation  I want to write a program that takes a positive integer from the keyboard. I want my program to sum all the integers from 1 up to and including the number entered. For example, if the user enters 5, the loop
Java Compilation - Java Beginners
Java Compilation  I want to write a program that takes a positive integer from the keyboard. I want my program to sum all the integers from 1 up to and including the number entered. For example, if the user enters 5, the loop
Java Compilation - Java Beginners
Java Compilation  I tried what was given to me but im still having problems. Here is what i want to do: Write a program that calculates a customer's monthly bill. It should ask user to enter the letter of the package
compilation error - Java Beginners
compilation error  sir what is the error inthis code after... : C:\>javac -d . Group.java 3)For execute the programm : C:\>java... not executing the java file currectly, 1- put the Employee.java file in c
java compilation error - JDBC
java compilation error   /* *Project:employee.java *Date:April 04,2007. *Purpose:To All Java Developers **/ package employee; import javax.swing.*; import java.awt.*; import javax.swing.border.
compilation error - Java Beginners
compilation error  Dear sir, When i compile some pgms i am getting below error.what may be the reason? "uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details."  Hi Friend
compilation error - Java Beginners
compilation error   import java.awt.*; import java.awt.event.*; import javax.swing.*; class BLayout implements ActionListener { JFrame f ; JPanel jp; JButton b[]; CardLayout cl; BLayout(String s) { jp = new JPanel
compilation error - Java Beginners
compilation error  i 'm not able to compile a program : prblem is there is one package in which 2 class person & dog reside. both classes r public still i'm not able to make object of Dog clss in Person class!! plezz help 
Java compilation error - Java Beginners
Java compilation error  hai, now only i have started to work with java.I have been given a task as execute unix codes using eclipse.But i dont know the source codes.Please help me to start the program... Regards, R.Punitham
 

 

 

DMCA.com