-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain9.java
More file actions
135 lines (133 loc) · 2.56 KB
/
Main9.java
File metadata and controls
135 lines (133 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main;
import lab9.*;
import java.io.*;
import java.util.Scanner;
public class Main9
{
public static void filecopy()
{
FileInputStream ob=null;
FileOutputStream obx=null;
try
{
ob= new FileInputStream("abc.txt");
obx=new FileOutputStream("xyz.txt");
byte[] buffer=new byte[1024];
int i;
while((i=ob.read(buffer))!=-1)
{
obx.write(buffer,0,i);
//System.out.println((char)i);
//i=ob.read();
}
}
catch(Exception e)
{
System.out.println("\nIOException");
}
finally{
try{
ob.close();
obx.close();
}
catch(Exception e)
{
System.out.println("\nIOException");
}
}
}
public static void mergefiles()
{
FileInputStream ob1=null;
FileInputStream ob2=null;
FileOutputStream obj=null;
try{
ob1= new FileInputStream("abc.txt");
ob2= new FileInputStream("xyz.txt");
obj=new FileOutputStream("final.txt");
byte[] buffer=new byte[1024];
int i;
while((i=ob1.read(buffer))!=-1)
{
obj.write(buffer,0,i);
}
while((i=ob2.read(buffer))!=-1)
{
obj.write(buffer,0,i);
}
}
catch(Exception e)
{
System.out.println("\nIOException");
}
finally{
try{
ob1.close();
ob2.close();
obj.close();
}
catch(Exception e)
{
System.out.println("\nIOException");
}
}
}
public static void Studentmain()
{
Stu S1 = new Stu();
FileOutputStream fout=null;
ObjectOutputStream out=null;
S1.setEn_no(123);
S1.setName("neha");
System.out.println(S1.en_no+" "+S1.name);
try
{
fout=new FileOutputStream("student.txt");
out=new ObjectOutputStream(fout);
out.writeObject(S1.toString());
System.out.print("objects successfully written");
}
catch(Exception e)
{
System.out.print(e);
}
finally
{
try
{
fout.close();
}
catch(Exception e)
{
System.out.println("exception occured");
}
}
}
public static void main(String a[])
{
int ch;
Scanner o=new Scanner(System.in);
System.out.println("1.Copy Content from one file to other\n2.Merge two files\n3. Write student details in file\n");
System.out.println("Enter your choice: ");
ch = o.nextInt();
switch(ch)
{
case 1:{
filecopy();
break;
}
case 2:{
mergefiles();
break;
}
case 3:{
Studentmain();
break;
}
default:{
System.out.println("\nEnter valid choice");
break;
}
}
}
}