-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiles.java
62 lines (54 loc) · 1.59 KB
/
Files.java
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
// Creating a new File.
import java.io.File;
import java.io.IOException;
class HelloWorld {
public static void main(String[] args) {
try{
File myObj = new File("File Path");
if(myObj.createNewFile()){
System.out.println("File Created : " + myObj.getName());
}
else{
System.out.println("File already exists.");
}
}
catch(IOException e){
System.out.println("Error");
}
}
}
// Read the text from a file and write the same in another one.
import java.io.*;
import java.util.*;
class X{
public static void main(String args[]) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Provide source file name : ");
String file_1 = sc.next();
FileReader fin = new FileReader(file_1);
FileWriter fout = new FileWriter(file_2, true);
int x;
while((x = fin.read()) != -1){
fout.write(x);
}
System.out.println("Done");
fin.close();
fout.close();
}
}
// Reading a Text File into an ArrayList
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
public class ReadTextFile{
public static void main(String[] args){
BufferedReader br = new BufferedReader(new FileReader("TextFile_Path.txt"));
ArrayList<String> text = new ArrayList<>();
String line = br.readLine();
while(line != null){
text.add(line);
line = br.readLine();
}
br.close();
}
}