• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Stream » How to Reading and Writing on Files
Next →
← Prev

How to Reading and Writing on Files

By Dinesh Thakur

Java makes available a set of classes and methods that allows you to read from and write to files. Java supports both streams and methods to read from and write to file respectively.

 

Example: Read file using FilelnputStream

 

import java.io.*;

class ShowFile

{

public static void main(String ar[]) throws IOException

{

int ch;

FileInputStream finObj;

try

{

finObj = new FileInputStream(“ShowFile.java”);

}

catch(FileNotFoundException e)

{

System.out.println(“File Not Found”);

return;

}

do

{

ch = finObj.read();

if(ch != -1)

System.out.print((char) ch);

} while(ch != -1);

finObj.close();

}

}

 

Output

 

 Reading and Writing on Files

Print contents of own source file, read character by character and write character by character.

 

Example: Read file using FileReader

 

import java.io.*;

class FileRead

{

public static void main(String arg[]) throws IOException

{

FileReader frObj = new FileReader(“FileRead.java”);

BufferedReader brObj = new BufferedReader(frObj);

String str;

while ((str = brObj.readLine ())!=null)

System.out.println(str);

frObj.close();

}

}

 

Output

 File Read

Read the file content line by line, and print in the same manner.

 

Example: Writing data using FileOutputStream

 

import java.io.*;

class CopyFile

{

public static void main(String arg[]) throws IOException

{

int i;

FileInputStream finObj;

FileOutputStream foutObj;

try

{

try

{

finObj = new FileInputStream(arg[0]);

}

catch(FileNotFoundException e)

{

System.out.println(“File ” + arg[0] + ” not Found”);

return;

}

 

try

{

foutObj = new FileOutputStream(arg[1]);

}

catch(FileNotFoundException e)

{

System.out.println(“File” +arg[1]+” not found”);

return;

}

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println(“Argument missing”);

return;

}

 

try

{

do

{

i = finObj.read();

if(i != -1)

foutObj.write(i);

}while(i != -1);

}

catch(IOException e)

{

System.out.println(“Some file error occurd”);

}

finObj.close();

foutObj.close();

}

}

 

Output:

 

 CopyFile

Copy content of a.txt to b.txt

 

It is clear form the output window given above that content of a.txt has been copied to b.txt.

 

Example: Writing data using FileWriter

 

import java.io.*;

public class WriteFile

{

public static void main(String[] args) throws IOException

{

File inputFileObj = new File(“a.txt”);

File outputFileObj = new File(“b.txt”);

FileReader finObj = new FileReader(inputFileObj);

FileWriter foutObj = new FileWriter(outputFileObj);

int c;

while ((c = finObj.read()) != -1)

foutObj.write(c);

finObj.close();

foutObj.close();

}

}

 

Output:

 

 Write File

Copy content of a.txt to b.txt

You’ll also like:

  1. Reading and Writing a Line of Text
  2. How to Writing on Console Output
  3. What is Files & Types of Files? Types of File Operations.
  4. Random Access Files
  5. Reading File in Java using FileInputStream Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW