SlideShare a Scribd company logo
Know how to redirect input and output, and know how to append to an existing file. Example:
run/usr/local/task with input redirected from blah.txt. and the output appended to file tylk.data
Solution
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
public static void main(String[] args) throws IOException {
File dir = new File("run/user/local/task.");
String source = dir.getCanonicalPath() + File.separator + "blah.txt";
String dest = dir.getCanonicalPath() + File.separator + "tylk.data";
File fin = new File(source);
FileInputStream fis = new FileInputStream(fin);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
FileWriter fstream = new FileWriter(dest, true);
BufferedWriter out = new BufferedWriter(fstream);
String aLine = null;
while ((aLine = in.readLine()) != null) {
out.write(aLine);
out.newLine();
}
// do not forget to close the buffer reader
in.close();
// close buffer writer
out.close();
}
}

More Related Content

PDF
2024 Trend Updates: What Really Works In SEO & Content Marketing
DOCX
Know the similarities and differences between the various electromagne.docx
DOCX
Kidding has uncovered evidence that seems to indicate that Rocket Man.docx
DOCX
Kathryn and her husband earned a considerable fortune during their wor.docx
DOCX
Katy-'s Catering Co- places an order with Sharpe-'s Supply House for 1.docx
DOCX
Kare Kars provides shuttle service between four hotels near a medical.docx
DOCX
Learning Goal- To recognize acids- bases- and conjugate pairs- By the.docx
DOCX
la) Define briefly the following terms- MSDS- IDLH Sublimation- Emulsi.docx
2024 Trend Updates: What Really Works In SEO & Content Marketing
Know the similarities and differences between the various electromagne.docx
Kidding has uncovered evidence that seems to indicate that Rocket Man.docx
Kathryn and her husband earned a considerable fortune during their wor.docx
Katy-'s Catering Co- places an order with Sharpe-'s Supply House for 1.docx
Kare Kars provides shuttle service between four hotels near a medical.docx
Learning Goal- To recognize acids- bases- and conjugate pairs- By the.docx
la) Define briefly the following terms- MSDS- IDLH Sublimation- Emulsi.docx

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
RMMM.pdf make it easy to upload and study
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Basic Mud Logging Guide for educational purpose
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Lesson notes of climatology university.
PDF
01-Introduction-to-Information-Management.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
RMMM.pdf make it easy to upload and study
TR - Agricultural Crops Production NC III.pdf
Cell Structure & Organelles in detailed.
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Basic Mud Logging Guide for educational purpose
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Lesson notes of climatology university.
01-Introduction-to-Information-Management.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Ad
Ad

Know how to redirect input and output- and know how to append to an ex.docx

  • 1. Know how to redirect input and output, and know how to append to an existing file. Example: run/usr/local/task with input redirected from blah.txt. and the output appended to file tylk.data Solution import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; class Main { public static void main(String[] args) throws IOException { File dir = new File("run/user/local/task."); String source = dir.getCanonicalPath() + File.separator + "blah.txt"; String dest = dir.getCanonicalPath() + File.separator + "tylk.data"; File fin = new File(source); FileInputStream fis = new FileInputStream(fin); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); FileWriter fstream = new FileWriter(dest, true);
  • 2. BufferedWriter out = new BufferedWriter(fstream); String aLine = null; while ((aLine = in.readLine()) != null) { out.write(aLine); out.newLine(); } // do not forget to close the buffer reader in.close(); // close buffer writer out.close(); } }