-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWriting.java
More file actions
executable file
·50 lines (40 loc) · 1.59 KB
/
FileWriting.java
File metadata and controls
executable file
·50 lines (40 loc) · 1.59 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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Scanner;
public class FileWriting {
Scanner sc = new Scanner(System.in);
// public void createFile() {
public String createFile(String customerName) {
System.out.print("Enter file name to create: ");
String fileName = customerName + ".txt";
File f = new File("C:\\java\\prj\\Hyper2", fileName);
String absolutePath = f.getAbsolutePath();
System.out.println("absoluteAddress: " + absolutePath);
try {
if (!f.exists()) {
f.createNewFile();
System.out.println("created");
} else {
f.setWritable(true);
System.out.println("File already created");
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
return absolutePath;
}
public void fileWriter(String absolutePath, String customerName, String tableStr, long tPrice) {
Calendar calendar = Calendar.getInstance();
try {
FileWriter fw = new FileWriter(absolutePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Date " + calendar.getTime() + "\n" + "Customer name:\t" + customerName + "\n" + tableStr + "\nTotal price: " + tPrice + " $");
bw.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}