-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFileOperationsDemo.java
More file actions
74 lines (65 loc) · 2.08 KB
/
FileOperationsDemo.java
File metadata and controls
74 lines (65 loc) · 2.08 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
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOperationsDemo {
static boolean fileCopy(String source , String dest) throws IOException{
boolean isCopied = false;
final int EOF = -1;
File file = new File(source);
if(file.exists()){
long startTime = System.currentTimeMillis();
FileInputStream fs = new FileInputStream(file);
BufferedInputStream bs = new BufferedInputStream(fs,20000);
FileOutputStream fo = new FileOutputStream(dest);
BufferedOutputStream bo = new BufferedOutputStream(fo,20000);
int singleByte = bs.read();
while(singleByte!=EOF){
bo.write(singleByte);
singleByte = bs.read();
}
long endTime = System.currentTimeMillis();
System.out.println("Total time taken "+(endTime-startTime)+"ms");
bs.close();
bo.close();
fs.close();
fo.close();
return true;
}
else
{
return false;
}
}
static void writeFile(String path , String data) throws IOException{
//String path = "/Users/amit/Documents/TestFileHandlingFeb/test.txt";
FileOutputStream fo = new FileOutputStream(path,true);
fo.write(data.getBytes());
fo.close();
System.out.println("Done...");
}
static String readFile(String path) throws IOException{
//String path ="/Users/amit/Documents/JavaBatch9WE/FileHandling/src/FileOperationsDemo.java" ;
// Open a file
FileInputStream fs = new FileInputStream(path);
StringBuffer sb = new StringBuffer();
// read a file
int singleByte = fs.read(); // read singleByte
while(singleByte!=-1){
sb.append((char)singleByte);
///System.out.print((char)singleByte);
singleByte = fs.read();
}
fs.close(); // close the file
return sb.toString();
}
public static void main(String[] args) throws IOException {
fileCopy("/Users/amit/Documents/TestFileHandlingFeb/EkPal.mp3", "/Users/amit/Documents/TestFileHandlingFeb/EkPalCopy.mp3");
// // TODO Auto-generated method stub
// //writeFile();
// //readFile();
}
}