0% found this document useful (0 votes)
3 views

files lab prgm

The document contains multiple Java programs demonstrating file operations using the FileWriter and FileReader classes. It includes writing characters and strings to files, appending data, reading characters from a file, copying content between files, and displaying file content. Each program is structured with appropriate error handling and file management techniques.

Uploaded by

vaarsh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

files lab prgm

The document contains multiple Java programs demonstrating file operations using the FileWriter and FileReader classes. It includes writing characters and strings to files, appending data, reading characters from a file, copying content between files, and displaying file content. Each program is structured with appropriate error handling and file management techniques.

Uploaded by

vaarsh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Write a Java Program write an array of characters to a file using FileWriter Class

import java.io.*;
import java.util.*;
public class democlass
{
public static void main(String[] args) throws IOException
{
File obj = new File("satish.txt");
FileWriter fout = new FileWriter(obj);
char c[]= {'s','a','t','i','s','h'};
if(obj.canWrite())
{
fout.write(c);
}
fout.close();
}
}

2. Write a Java Program to write a String to a file using FileWriter Class

import java.io.*;
import java.util.*;
public class democlass
{
public static void main(String[] args) throws IOException
{
File obj = new File("satish.txt");
FileWriter fout = new FileWriter(obj);
String s = "satish is writing here";
if(obj.canWrite())
{
fout.write(s);
}
fout.close();
}
}

3. Write a Java Program to append data to a file using FileWriter Class

import java.io.*;
import java.util.*;
public class democlass
{
public static void main(String[] args) throws IOException
{
File obj = new File("output8.txt");
FileWriter fout = new FileWriter(obj,true);
try
{
1
String str="new message here";
fout.write(str);
System.out.println("file writing successful");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
fout.close();
}
}
}

4. Write a program to read an Array of Characters from a File using FileReader Class in Java

import java.io.*;
import java.util.*;
public class democlass
{
public static void main(String[] args) throws IOException
{
File obj = new File("satish.txt");
FileReader fin = new FileReader(obj);
char c[]=new char[4];
fin.read(c);
for(char k:c)
{
System.out.println(k);
}
}
}

5. Java program to copy content from one file to another

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

public class CopyFromFileaToFileb


{
public static void copyContent(File a, File b)
throws Exception
{
FileInputStream in = new FileInputStream(a);
FileOutputStream out = new FileOutputStream(b);

try {

2
int n;

// read() function to read the byte of data


while ((n = in.read()) != -1)
{
// write() function to write the byte of data
out.write(n);
}
}
finally {
if (in != null) {

// close() function to close the stream


in.close();
}
// close() function to close the stream
if (out != null) {
out.close();
}
}
System.out.println("File Copied");
}

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


{
Scanner sc = new Scanner(System.in);

// get the source file name


System.out.println( "Enter the source filename from where you have to
read/copy :");
String a = sc.nextLine();

// source file
File x = new File(a);

// get the destination file name


System.out.println("Enter the destination filename where you have to
write/paste :");
String b = sc.nextLine();

// destination file
File y = new File(b);

// method called to copy the contents from x to y


copyContent(x, y);
}
}

6. Write a java program to Print File Content, Display File

3
import java.io.*;
public class Display_File
{
public static void main(String args[]) throws IOException
{
File f_name = new File("file.txt");

FileInputStream in_file = new FileInputStream("file.txt");


int len = (int) f_name.length();

byte bytes[] = new byte[len];

System.out.println("Size of File : " + in_file.read(bytes));

String f = new String(bytes);


System.out.println("File Content is : " + f);

in_file.close();
}
}

Output
Size of File : 21
File Content is : This is Java Programs

You might also like