-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHelper.java
More file actions
46 lines (43 loc) · 1.57 KB
/
FileHelper.java
File metadata and controls
46 lines (43 loc) · 1.57 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
import java.io.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
import java.io.FileWriter;
public class FileHelper {
// Parameter is the full file path of where the file is on your computer
public static String readFromFile(String sFilePath) {
try {
String = str;
File i_file = new File(sFilePath);
Scanner i_scanner = new Scanner(i_file);
while (i_scanner.hasNextLine()) {
String sLine = i_scanner.nextLine();
System.out.println(sLine);
}
i_scanner.close();
System.out.println("Your file has been read.");
return str;
} catch (FileNotFoundException e) {
System.out.println("There was an error with your file:");
e.printStackTrace();
}
}
// Parameter output path is the full file path of where you want the file to be
// on your computer
// Parameter text is the text you want in the output files contents
public static void writeToFile(String sOutputPath, String sText) {
try {
FileWriter o_file = new FileWriter(sOutputPath);
o_file.write(sText);
o_file.close();
System.out.println("Your file has been written.");
} catch (IOException e) {
System.out.println("There was an error with your file:");
e.printStackTrace();
}
}
public static int countCharacters(String fileName) {
return readFromFile(fileName).length();
}
}