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 [Link].*;
public class Sample {
public static void main(String args[])
{
File f = new
File("C:\\Users\\Lenovo\\Desktop\\College Related Coding
Stuffs\\Dir1");
if ([Link]()) {
[Link]("Directory is created");
}
else {
[Link]("Directory cannot be
created");
}
}
}
Output
Question -:
Reading from and Writing into files in Java
Solution -:
Code
import [Link];
import [Link];
public class Sample {
public static void main(String[] args) {
try {
File myObj = new
File("C:\\Users\\Lenovo\\Desktop\\College Related Coding
Stuffs\\Dir1\\[Link]");
if ([Link]()) {
[Link]("File created: " +
[Link]());
} else {
[Link]("File already
exists.");
}
} catch (IOException e) {
[Link]("An error occurred.");
[Link]();
}
}
}
import [Link];
import [Link];
public class Sample {
public static void main(String[] args) {
try {
FileWriter myWriter = new
FileWriter("[Link]");
[Link]("Files in Java might be
tricky, but it is fun enough!");
[Link]();
[Link]("Successfully wrote to the
file.");
} catch (IOException e) {
[Link]("An error occurred.");
[Link]();
}
}
}
import [Link];
import [Link];
import [Link];
public class Sample {
public static void main(String[] args) {
try {
File myObj = new File("[Link]");
Scanner myReader = new Scanner(myObj);
while ([Link]()) {
String data = [Link]();
[Link](data);
}
[Link]();
} catch (FileNotFoundException e) {
[Link]("An error occurred.");
[Link]();
}
}
}
Output
Question -:
Copying one file contents into another in Java
Solution -:
Code
import [Link];
import [Link];
class Sample {
public static void main(String[] args) {
byte[] array = new byte[50];
try {
FileInputStream sourceFile = new
FileInputStream("[Link]");
FileOutputStream destFile = new
FileOutputStream("newFile");
[Link](array);
[Link](array);
[Link]("The [Link] file is
copied to newFile.");
[Link]();
[Link]();
}
catch (Exception e) {
[Link]();
}
}
}
Output
Question -:
Use File input Stream, File Output Stream and File Reader
and File Writer in Java
Solution -:
Code
import [Link];
public class Sample{
public static void main(String args[]){
try{
FileOutputStream fout=new
FileOutputStream("C:\\Users\\Lenovo\\Desktop\\College
Related Coding Stuffs\\Dir1\\filename");
[Link](65);
[Link]();
[Link]("Success...");
}catch(Exception e){[Link](e);}
}
}
import [Link];
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=[Link]();
[Link]((char)i);
[Link]();
}catch(Exception e){[Link](e);}
}
}
import [Link];
public class Sample {
public static void main(String args[])throws
Exception{
FileReader fr=new
FileReader("C:\\Users\\Lenovo\\Desktop\\College Related
Coding Stuffs\\Dir1\\[Link]");
int i;
while((i=[Link]())!=-1)
[Link]((char)i);
[Link]();
}
}
import [Link].*;
class CustomFilterWriter extends FilterWriter {
CustomFilterWriter(Writer out) {
super(out);
}
public void write(String str) throws IOException {
[Link]([Link]());
}
}
public class Sample {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("[Link]");
CustomFilterWriter filterWriter = new
CustomFilterWriter(fw);
[Link]("I LOVE MY COUNTRY");
[Link]();
FileReader fr = new FileReader("[Link]");
BufferedReader bufferedReader = new
BufferedReader(fr);
int k;
while ((k = [Link]()) != -1) {
[Link]((char) k);
}
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
Output