-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyWriter.java
74 lines (68 loc) · 2.41 KB
/
MyWriter.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
63
64
65
66
67
68
69
70
71
72
73
74
package com.company;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.*;
import java.util.Scanner;
public class MyWriter extends Component {
public static String reader(String fileName) {
File inF = new File(fileName);
try {
Scanner in = new Scanner(inF);
String testo = in.nextLine();
while(in.hasNextLine()){
testo+="\n";
testo+=in.nextLine();
}
return testo;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static void writer(String fileName, String testo){
File inF = new File(fileName);
try {
BufferedWriter scritturaFile = new BufferedWriter(new FileWriter(inF));
scritturaFile.write(testo);
scritturaFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void fileMother(String fileName){
File inF = new File(fileName);
try {
BufferedWriter scritturaFile = new BufferedWriter(new FileWriter(inF));
scritturaFile.write("Share Notepad-- with all your friends! ");
scritturaFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public File find(){
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
fc.setFileFilter(filter);
int returnVal = fc.showOpenDialog(MyWriter.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile();
} else {
System.out.println("azione annullata");
}
return null;
}
public File save(){
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Specify file name and directory");
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
fc.setFileFilter(filter);
int userSelection = fc.showSaveDialog(MyWriter.this);
if (userSelection == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile();
} else {
System.out.println("azione annullata");
}
return null;
}
}