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

Lab Assignment - 4: Fall Semester 2020-21

This document contains the lab assignment solutions of Tanay Dubey (registration number 19BCT0185) for the course CSE1007 Java Programming. It includes solutions to questions on creating a directory in Java, reading from and writing to files, copying file contents, and using FileInputStream, FileOutputStream, FileReader and FileWriter. Code snippets are provided to demonstrate how to perform these file I/O operations in Java.

Uploaded by

Tanay Dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Lab Assignment - 4: Fall Semester 2020-21

This document contains the lab assignment solutions of Tanay Dubey (registration number 19BCT0185) for the course CSE1007 Java Programming. It includes solutions to questions on creating a directory in Java, reading from and writing to files, copying file contents, and using FileInputStream, FileOutputStream, FileReader and FileWriter. Code snippets are provided to demonstrate how to perform these file I/O operations in Java.

Uploaded by

Tanay Dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Assignment – 4

Fall Semester 2020-21

Name – Tanay Dubey


Registration Number – 19BCT0185
Course Code and Name – CSE1007 Java Programming
Faculty Name – Gopichand G
Slot – L27 + L28
Question -:
Creating a directory in Java
Solution -:
Code
import java.io.*;

public class Sample {

public static void main(String args[])


{
File f = new
File("C:\\Users\\Lenovo\\Desktop\\College Related Coding
Stuffs\\Dir1");
if (f.mkdir()) {
System.out.println("Directory is created");
}
else {
System.out.println("Directory cannot be
created");
}
}
}
Output
Question -:
Reading from and Writing into files in Java
Solution -:
Code
import java.io.File;
import java.io.IOException;

public class Sample {


public static void main(String[] args) {
try {
File myObj = new
File("C:\\Users\\Lenovo\\Desktop\\College Related Coding
Stuffs\\Dir1\\filename.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " +
myObj.getName());
} else {
System.out.println("File already
exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

import java.io.FileWriter;
import java.io.IOException;

public class Sample {


public static void main(String[] args) {
try {
FileWriter myWriter = new
FileWriter("filename.txt");
myWriter.write("Files in Java might be
tricky, but it is fun enough!");
myWriter.close();
System.out.println("Successfully wrote to the
file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Sample {


public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output
Question -:
Copying one file contents into another in Java
Solution -:
Code
import java.io.FileInputStream;
import java.io.FileOutputStream;

class Sample {
public static void main(String[] args) {

byte[] array = new byte[50];


try {
FileInputStream sourceFile = new
FileInputStream("filename.txt");
FileOutputStream destFile = new
FileOutputStream("newFile");

sourceFile.read(array);

destFile.write(array);
System.out.println("The filename.txt file is
copied to newFile.");

sourceFile.close();
destFile.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
Output
Question -:
Use File input Stream, File Output Stream and File Reader
and File Writer in Java
Solution -:
Code
import java.io.FileOutputStream;
public class Sample{
public static void main(String args[]){
try{
FileOutputStream fout=new
FileOutputStream("C:\\Users\\Lenovo\\Desktop\\College
Related Coding Stuffs\\Dir1\\filename");
fout.write(65);
fout.close();
System.out.println("Success...");
}catch(Exception e){System.out.println(e);}
}
}

import java.io.FileInputStream;
public class Sample {
public static void main(String args[]){
try{
FileInputStream fin=new
FileInputStream("C:\\Users\\Lenovo\\Desktop\\College
Related Coding Stuffs\\Dir1\\filename");
int i=fin.read();
System.out.print((char)i);

fin.close();
}catch(Exception e){System.out.println(e);}
}
}
import java.io.FileReader;
public class Sample {
public static void main(String args[])throws
Exception{
FileReader fr=new
FileReader("C:\\Users\\Lenovo\\Desktop\\College Related
Coding Stuffs\\Dir1\\filename.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}
import java.io.*;
class CustomFilterWriter extends FilterWriter {
CustomFilterWriter(Writer out) {
super(out);
}
public void write(String str) throws IOException {
super.write(str.toLowerCase());
}
}
public class Sample {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("Record.txt");
CustomFilterWriter filterWriter = new
CustomFilterWriter(fw);
filterWriter.write("I LOVE MY COUNTRY");
filterWriter.close();
FileReader fr = new FileReader("record.txt");
BufferedReader bufferedReader = new
BufferedReader(fr);
int k;
while ((k = bufferedReader.read()) != -1) {
System.out.print((char) k);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output

You might also like