SlideShare a Scribd company logo
Java Program using Recursion:
Write a program that reads in a file and outputs it to another file. The output file created should
be identical to the input file but with its lines in reverse order. Use recursion. Obtain the names
of the input and output files from the command line. For the input file, use a file that contains the
integers 1 to 10, each on a seperate line.
Solution
I think the given code is working correctly
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class Reversefile {
public static void main(String[] args) {
FileInputStream fis = null;
RandomAccessFile raf = null;
String characterEncoding = "utf-8";
if(args.length==3) {
characterEncoding = args[2];
}
try{
File in = new File(args[0]);
fis = new FileInputStream(in);
Reader r = new InputStreamReader(fis,characterEncoding);
File out = new File(args[1]);
raf = new RandomAccessFile(out, "rw");
raf.setLength(in.length());
char[] buff = new char[1];
long position = in.length();
while((r.read(buff))>-1) {
Character c = buff[0];
String s = c+"";
byte[] bBuff = s.getBytes(characterEncoding);
position = position-bBuff.length;
raf.seek(position);
raf.write(bBuff);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (Exception e2) {
}
try {
raf.close();
} catch (Exception e2) {
}
}
}
}

More Related Content

Similar to Java Program using RecursionWrite a program that reads in a file .pdf

File Input and Output in Java Programing language
File Input and Output in Java Programing languageFile Input and Output in Java Programing language
File Input and Output in Java Programing language
BurhanKhan774154
 
In JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdfIn JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdf
suresh640714
 
Java file
Java fileJava file
Java file
sonnetdp
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
In JavaWrite a program that reads a text file that contains a gra.pdf
In JavaWrite a program that reads a text file that contains a gra.pdfIn JavaWrite a program that reads a text file that contains a gra.pdf
In JavaWrite a program that reads a text file that contains a gra.pdf
mumnesh
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
theodorelove43763
 
I need help with this Java assignment. Please include comments throu.pdf
I need help with this Java assignment. Please include comments throu.pdfI need help with this Java assignment. Please include comments throu.pdf
I need help with this Java assignment. Please include comments throu.pdf
sales98
 
Write a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdfWrite a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdf
atulkapoor33
 
Change the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdfChange the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdf
secunderbadtirumalgi
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
f3apparelsonline
 
Functions in python
Functions in pythonFunctions in python
Functions in python
Santosh Verma
 
Java session08
Java session08Java session08
Java session08
Niit Care
 
Write a program CountWords in Java that reads text from a file and b.pdf
Write a program CountWords in Java that reads text from a file and b.pdfWrite a program CountWords in Java that reads text from a file and b.pdf
Write a program CountWords in Java that reads text from a file and b.pdf
rushabhshah600
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
Vikram Nandini
 
JAVA
JAVAJAVA
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
Sowri Rajan
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
DHARUNESHBOOPATHY
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
sangeeta borde
 

Similar to Java Program using RecursionWrite a program that reads in a file .pdf (20)

File Input and Output in Java Programing language
File Input and Output in Java Programing languageFile Input and Output in Java Programing language
File Input and Output in Java Programing language
 
In JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdfIn JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdf
 
Java file
Java fileJava file
Java file
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
In JavaWrite a program that reads a text file that contains a gra.pdf
In JavaWrite a program that reads a text file that contains a gra.pdfIn JavaWrite a program that reads a text file that contains a gra.pdf
In JavaWrite a program that reads a text file that contains a gra.pdf
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
 
I need help with this Java assignment. Please include comments throu.pdf
I need help with this Java assignment. Please include comments throu.pdfI need help with this Java assignment. Please include comments throu.pdf
I need help with this Java assignment. Please include comments throu.pdf
 
Write a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdfWrite a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdf
 
Change the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdfChange the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdf
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Java session08
Java session08Java session08
Java session08
 
Write a program CountWords in Java that reads text from a file and b.pdf
Write a program CountWords in Java that reads text from a file and b.pdfWrite a program CountWords in Java that reads text from a file and b.pdf
Write a program CountWords in Java that reads text from a file and b.pdf
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
JAVA
JAVAJAVA
JAVA
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 

More from duttakajal70

Explain the relevance of the rules against hearsay to digital eviden.pdf
Explain the relevance of the rules against hearsay to digital eviden.pdfExplain the relevance of the rules against hearsay to digital eviden.pdf
Explain the relevance of the rules against hearsay to digital eviden.pdf
duttakajal70
 
Electric and magnetic fields in an electromagnetic wave are alway.pdf
Electric and magnetic fields in an electromagnetic wave are alway.pdfElectric and magnetic fields in an electromagnetic wave are alway.pdf
Electric and magnetic fields in an electromagnetic wave are alway.pdf
duttakajal70
 
C# code pleaseWrite a program that creates a link list object of 1.pdf
C# code pleaseWrite a program that creates a link list object of 1.pdfC# code pleaseWrite a program that creates a link list object of 1.pdf
C# code pleaseWrite a program that creates a link list object of 1.pdf
duttakajal70
 
During our laboratory exercise on cellular the Krebs cycle in.pdf
During our laboratory exercise on cellular the Krebs cycle in.pdfDuring our laboratory exercise on cellular the Krebs cycle in.pdf
During our laboratory exercise on cellular the Krebs cycle in.pdf
duttakajal70
 
25. On January 3, Pippin Corporation purchased 1,900 shares of the c.pdf
25. On January 3, Pippin Corporation purchased 1,900 shares of the c.pdf25. On January 3, Pippin Corporation purchased 1,900 shares of the c.pdf
25. On January 3, Pippin Corporation purchased 1,900 shares of the c.pdf
duttakajal70
 
Define filtration and active transport.SolutionFiltration is t.pdf
Define filtration and active transport.SolutionFiltration is t.pdfDefine filtration and active transport.SolutionFiltration is t.pdf
Define filtration and active transport.SolutionFiltration is t.pdf
duttakajal70
 
Why wouldnt you want to post a payment against an invoice directly.pdf
Why wouldnt you want to post a payment against an invoice directly.pdfWhy wouldnt you want to post a payment against an invoice directly.pdf
Why wouldnt you want to post a payment against an invoice directly.pdf
duttakajal70
 
Cisco routers and switches use two configuration files. Where is the.pdf
Cisco routers and switches use two configuration files. Where is the.pdfCisco routers and switches use two configuration files. Where is the.pdf
Cisco routers and switches use two configuration files. Where is the.pdf
duttakajal70
 
Which of the following is a simple compressible system A mixtur.pdf
Which of the following is a simple compressible system  A mixtur.pdfWhich of the following is a simple compressible system  A mixtur.pdf
Which of the following is a simple compressible system A mixtur.pdf
duttakajal70
 
Which of the following statements is (are) trueI Walls Street i.pdf
Which of the following statements is (are) trueI  Walls Street i.pdfWhich of the following statements is (are) trueI  Walls Street i.pdf
Which of the following statements is (are) trueI Walls Street i.pdf
duttakajal70
 
What would be the most effective method of reducing the incidence of .pdf
What would be the most effective method of reducing the incidence of .pdfWhat would be the most effective method of reducing the incidence of .pdf
What would be the most effective method of reducing the incidence of .pdf
duttakajal70
 
What is a micelle What is a vesicle How are these structures the s.pdf
What is a micelle What is a vesicle How are these structures the s.pdfWhat is a micelle What is a vesicle How are these structures the s.pdf
What is a micelle What is a vesicle How are these structures the s.pdf
duttakajal70
 
To determine her breathing rate, Miranda divides up her day into thre.pdf
To determine her breathing rate, Miranda divides up her day into thre.pdfTo determine her breathing rate, Miranda divides up her day into thre.pdf
To determine her breathing rate, Miranda divides up her day into thre.pdf
duttakajal70
 
On January 29th, 2014, the Royal Caribbean cruise ship Explorer of.pdf
On January 29th, 2014, the Royal Caribbean cruise ship Explorer of.pdfOn January 29th, 2014, the Royal Caribbean cruise ship Explorer of.pdf
On January 29th, 2014, the Royal Caribbean cruise ship Explorer of.pdf
duttakajal70
 
Name and describe the six main types of lymphatic cells, discuss the.pdf
Name and describe the six main types of lymphatic cells, discuss the.pdfName and describe the six main types of lymphatic cells, discuss the.pdf
Name and describe the six main types of lymphatic cells, discuss the.pdf
duttakajal70
 
Let X1, X2, . . . , Xn be n independent continuous random variables,.pdf
Let X1, X2, . . . , Xn be n independent continuous random variables,.pdfLet X1, X2, . . . , Xn be n independent continuous random variables,.pdf
Let X1, X2, . . . , Xn be n independent continuous random variables,.pdf
duttakajal70
 
In major league baseball, there are 5 teams in the Eastern division .pdf
In major league baseball, there are 5 teams in the Eastern division .pdfIn major league baseball, there are 5 teams in the Eastern division .pdf
In major league baseball, there are 5 teams in the Eastern division .pdf
duttakajal70
 
Im off by a minus sign, can you find my error In a previous probl.pdf
Im off by a minus sign, can you find my error In a previous probl.pdfIm off by a minus sign, can you find my error In a previous probl.pdf
Im off by a minus sign, can you find my error In a previous probl.pdf
duttakajal70
 
If the characteristic followed in the pedigree is X-linked recessiv.pdf
If the characteristic followed in the pedigree is X-linked recessiv.pdfIf the characteristic followed in the pedigree is X-linked recessiv.pdf
If the characteristic followed in the pedigree is X-linked recessiv.pdf
duttakajal70
 
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdfcreate a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
duttakajal70
 

More from duttakajal70 (20)

Explain the relevance of the rules against hearsay to digital eviden.pdf
Explain the relevance of the rules against hearsay to digital eviden.pdfExplain the relevance of the rules against hearsay to digital eviden.pdf
Explain the relevance of the rules against hearsay to digital eviden.pdf
 
Electric and magnetic fields in an electromagnetic wave are alway.pdf
Electric and magnetic fields in an electromagnetic wave are alway.pdfElectric and magnetic fields in an electromagnetic wave are alway.pdf
Electric and magnetic fields in an electromagnetic wave are alway.pdf
 
C# code pleaseWrite a program that creates a link list object of 1.pdf
C# code pleaseWrite a program that creates a link list object of 1.pdfC# code pleaseWrite a program that creates a link list object of 1.pdf
C# code pleaseWrite a program that creates a link list object of 1.pdf
 
During our laboratory exercise on cellular the Krebs cycle in.pdf
During our laboratory exercise on cellular the Krebs cycle in.pdfDuring our laboratory exercise on cellular the Krebs cycle in.pdf
During our laboratory exercise on cellular the Krebs cycle in.pdf
 
25. On January 3, Pippin Corporation purchased 1,900 shares of the c.pdf
25. On January 3, Pippin Corporation purchased 1,900 shares of the c.pdf25. On January 3, Pippin Corporation purchased 1,900 shares of the c.pdf
25. On January 3, Pippin Corporation purchased 1,900 shares of the c.pdf
 
Define filtration and active transport.SolutionFiltration is t.pdf
Define filtration and active transport.SolutionFiltration is t.pdfDefine filtration and active transport.SolutionFiltration is t.pdf
Define filtration and active transport.SolutionFiltration is t.pdf
 
Why wouldnt you want to post a payment against an invoice directly.pdf
Why wouldnt you want to post a payment against an invoice directly.pdfWhy wouldnt you want to post a payment against an invoice directly.pdf
Why wouldnt you want to post a payment against an invoice directly.pdf
 
Cisco routers and switches use two configuration files. Where is the.pdf
Cisco routers and switches use two configuration files. Where is the.pdfCisco routers and switches use two configuration files. Where is the.pdf
Cisco routers and switches use two configuration files. Where is the.pdf
 
Which of the following is a simple compressible system A mixtur.pdf
Which of the following is a simple compressible system  A mixtur.pdfWhich of the following is a simple compressible system  A mixtur.pdf
Which of the following is a simple compressible system A mixtur.pdf
 
Which of the following statements is (are) trueI Walls Street i.pdf
Which of the following statements is (are) trueI  Walls Street i.pdfWhich of the following statements is (are) trueI  Walls Street i.pdf
Which of the following statements is (are) trueI Walls Street i.pdf
 
What would be the most effective method of reducing the incidence of .pdf
What would be the most effective method of reducing the incidence of .pdfWhat would be the most effective method of reducing the incidence of .pdf
What would be the most effective method of reducing the incidence of .pdf
 
What is a micelle What is a vesicle How are these structures the s.pdf
What is a micelle What is a vesicle How are these structures the s.pdfWhat is a micelle What is a vesicle How are these structures the s.pdf
What is a micelle What is a vesicle How are these structures the s.pdf
 
To determine her breathing rate, Miranda divides up her day into thre.pdf
To determine her breathing rate, Miranda divides up her day into thre.pdfTo determine her breathing rate, Miranda divides up her day into thre.pdf
To determine her breathing rate, Miranda divides up her day into thre.pdf
 
On January 29th, 2014, the Royal Caribbean cruise ship Explorer of.pdf
On January 29th, 2014, the Royal Caribbean cruise ship Explorer of.pdfOn January 29th, 2014, the Royal Caribbean cruise ship Explorer of.pdf
On January 29th, 2014, the Royal Caribbean cruise ship Explorer of.pdf
 
Name and describe the six main types of lymphatic cells, discuss the.pdf
Name and describe the six main types of lymphatic cells, discuss the.pdfName and describe the six main types of lymphatic cells, discuss the.pdf
Name and describe the six main types of lymphatic cells, discuss the.pdf
 
Let X1, X2, . . . , Xn be n independent continuous random variables,.pdf
Let X1, X2, . . . , Xn be n independent continuous random variables,.pdfLet X1, X2, . . . , Xn be n independent continuous random variables,.pdf
Let X1, X2, . . . , Xn be n independent continuous random variables,.pdf
 
In major league baseball, there are 5 teams in the Eastern division .pdf
In major league baseball, there are 5 teams in the Eastern division .pdfIn major league baseball, there are 5 teams in the Eastern division .pdf
In major league baseball, there are 5 teams in the Eastern division .pdf
 
Im off by a minus sign, can you find my error In a previous probl.pdf
Im off by a minus sign, can you find my error In a previous probl.pdfIm off by a minus sign, can you find my error In a previous probl.pdf
Im off by a minus sign, can you find my error In a previous probl.pdf
 
If the characteristic followed in the pedigree is X-linked recessiv.pdf
If the characteristic followed in the pedigree is X-linked recessiv.pdfIf the characteristic followed in the pedigree is X-linked recessiv.pdf
If the characteristic followed in the pedigree is X-linked recessiv.pdf
 
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdfcreate a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
create a Tic Tac Toe game using JSP Scripting elements and a JSP pag.pdf
 

Recently uploaded

Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 

Recently uploaded (20)

Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 

Java Program using RecursionWrite a program that reads in a file .pdf

  • 1. Java Program using Recursion: Write a program that reads in a file and outputs it to another file. The output file created should be identical to the input file but with its lines in reverse order. Use recursion. Obtain the names of the input and output files from the command line. For the input file, use a file that contains the integers 1 to 10, each on a seperate line. Solution I think the given code is working correctly import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.io.Reader; public class Reversefile { public static void main(String[] args) { FileInputStream fis = null; RandomAccessFile raf = null; String characterEncoding = "utf-8"; if(args.length==3) { characterEncoding = args[2]; } try{ File in = new File(args[0]); fis = new FileInputStream(in); Reader r = new InputStreamReader(fis,characterEncoding); File out = new File(args[1]); raf = new RandomAccessFile(out, "rw"); raf.setLength(in.length()); char[] buff = new char[1]; long position = in.length(); while((r.read(buff))>-1) { Character c = buff[0]; String s = c+""; byte[] bBuff = s.getBytes(characterEncoding);
  • 2. position = position-bBuff.length; raf.seek(position); raf.write(bBuff); } } catch (Exception e) { e.printStackTrace(); } finally { try { fis.close(); } catch (Exception e2) { } try { raf.close(); } catch (Exception e2) { } } } }