Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Task-7 File IO/contentsFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
weailufkwbakugkqvdwqku
3 changes: 3 additions & 0 deletions Task-7 File IO/copy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Binary file added Task-7 File IO/dateStore.txt
Binary file not shown.
3 changes: 3 additions & 0 deletions Task-7 File IO/details
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Empty file added Task-7 File IO/file5
Empty file.
Empty file added Task-7 File IO/file6
Empty file.
Empty file added Task-7 File IO/file_new
Empty file.
3 changes: 3 additions & 0 deletions Task-7 File IO/lineByLineCopy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
16 changes: 16 additions & 0 deletions Task-7 File IO/src/__1__CountNoOfCharacters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Scanner;

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

try(Scanner scanner = new Scanner(System.in); ){
String inputString = scanner.nextLine();

char[] inputStringCharacters = inputString.toCharArray();
System.out.println("No of characters recieved via System input : "+inputStringCharacters.length);

}catch(Exception e) {
System.out.println(e);
}
}
}
16 changes: 16 additions & 0 deletions Task-7 File IO/src/__2__CountNoOfWords.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Scanner;

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

try(Scanner scanner = new Scanner(System.in); ){
String inputString = scanner.nextLine();

String[] inputStringWords = inputString.split(" ");
System.out.println("No of words recieved via System input : "+inputStringWords.length);

}catch(Exception e) {
System.out.println(e);
}
}
}
64 changes: 64 additions & 0 deletions Task-7 File IO/src/__3__FileOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.Scanner;
import java.io.File;

class FileOperations {
Operations operation;
public Operations getAction() {
return operation;
}
}

abstract class Operations{
public abstract void operate(File file);
}

class DeleteAFile extends Operations{
public void operate(File file) {
if(file.exists()){
file.delete();
System.out.println("File deleted successfully..");
}else{
System.out.println("File not found...");
}
}
}

class RenameAFile extends Operations{
public void operate(File file) {
if(file.exists()){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the new name for your file - ");
String name = scanner.nextLine();
scanner.close();
if(file.renameTo(new File(name))){
System.out.println("File renamed to "+name+" successfully..");
}else {
System.out.println("Error occured..");
}
}else{
System.out.println("File not found...");
}
}
}

class SizeOfFile extends Operations{
public void operate(File file) {
if(file.exists()){
System.out.println("Size of file : "+ file.length());
}else{
System.out.println("File not found...");
}
}
}

public class __3__FileOperations {
public static void main(String[] args) throws Exception {
File file = new File("file5");

FileOperations fop = new FileOperations();
fop.operation = (Operations) Class.forName("RenameAFile").newInstance();
fop.getAction().operate(file);

}

}
15 changes: 15 additions & 0 deletions Task-7 File IO/src/__4__ListContents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.io.File;

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

File path = new File(System.getProperty("user.dir")+"/src");

System.out.println("No of files and directories in this path are : "+path.list().length+"\n");

for(int i=0;i<path.list().length; i++) {
System.out.println((i+1)+". "+path.list()[i]);
}

}
}
21 changes: 21 additions & 0 deletions Task-7 File IO/src/__5__ImageByteByByteCopy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class __5__ImageByteByByteCopy {
public static void main(String[] args) {
try (FileInputStream fileInputStream = new FileInputStream("siriusStar.jpeg");
FileOutputStream fileOutputStream = new FileOutputStream("siriusStar1.jpeg", true);) {
int n = 0;

byte imageBytes[] = new byte[1024];

while((n = fileInputStream.read(imageBytes)) != -1) {
fileOutputStream.write(imageBytes, 0, n);
}

} catch (IOException e) {
e.printStackTrace();
}
}
}
14 changes: 14 additions & 0 deletions Task-7 File IO/src/__6__FileViewer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.io.FileInputStream;

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

try(FileInputStream fis=new FileInputStream("details")){
int letter;
while((letter = fis.read())!=-1)
System.out.print((char)letter);
}catch(Exception e) {
System.out.println(e);
}
}
}
19 changes: 19 additions & 0 deletions Task-7 File IO/src/__7__CopyCharByChar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.io.FileReader;
import java.io.FileWriter;

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

try (FileReader in = new FileReader("details"); FileWriter out = new FileWriter("copy.txt")) {
char charBuffer[] = new char[8];
int noOfCharacters = 0;
while ((noOfCharacters = in.read(charBuffer)) != -1) {
String s = new String(charBuffer, 0, noOfCharacters);
out.write(s);
}
System.out.println("Copied...");
} catch (Exception e) {
System.out.println(e);
}
}
}
32 changes: 32 additions & 0 deletions Task-7 File IO/src/__8__CopyFileLineByLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.IOException;

public class __8__CopyFileLineByLine {
public static void main(String[] args) throws IOException, FileNotFoundException {

BufferedReader inputStream = null;
PrintWriter outputStream = null;

try {
inputStream = new BufferedReader(new FileReader("details"));
outputStream = new PrintWriter(new FileWriter("lineByLineCopy.txt"));

String line;
while ((line = inputStream.readLine()) != null) {
outputStream.println(line);
}
System.out.println("Copied line by line ...");
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
30 changes: 30 additions & 0 deletions Task-7 File IO/src/__9__SerializeAndDeserialize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class __9__SerializeAndDeserialize {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {

//Writing date object to file
try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("dateStore.txt"))) {
Date date = new Date();
outputStream.writeObject(date);

} catch (Exception e) {
System.out.println(e);
}

//Reading object from file, casting to date
try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("dateStore.txt"))) {
Date date = (Date) inputStream.readObject();
System.out.println(date);

} catch (Exception e) {
System.out.println(e);
}
}
}