-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
47 lines (42 loc) · 1.28 KB
/
Test.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
import java.io.*;
import java.util.*;
public class Test {
public static void main(String [] args) {
// The name of the file to open.
String fileName = "temp.txt",s="hi";
try {
// Assume default encoding.
FileWriter fileWriter =
new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
Scanner r=new Scanner(System.in);
// Note that write() does not automatically
// append a newline character.
do
{
s=r.nextLine();
if(s.equals("nl"))
{
bufferedWriter.newLine();
}
else
bufferedWriter.write(s);
//bufferedWriter.write(" here is some text.");
//bufferedWriter.newLine();
//bufferedWriter.write("We are writing");
//bufferedWriter.write(" the text to the file.");
}while(!s.equals("exit"));
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}