Skip to content

Commit d15dbf7

Browse files
committed
修改文本I/O内容
1 parent 707be64 commit d15dbf7

File tree

10 files changed

+52
-56
lines changed

10 files changed

+52
-56
lines changed

andrew.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

scores.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
John T Smith 90
2-
Eric K Jones 85
1+
Andrew 90
2+
Eric 80

scores1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Programming 90
2+
Eric 80

src/main/java/file/ReadData.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
package file;
22

3+
import java.io.File;
34
import java.util.Scanner;
45

56
public class ReadData {
6-
public static void main(String[] args) throws Exception {
7-
// Create a File instance
8-
java.io.File file = new java.io.File("scores.txt");
97

10-
// Create a Scanner for the file
8+
public static void main(String[] args) throws Exception {
9+
File file = new File("scores.txt");
1110
Scanner input = new Scanner(file);
12-
// input.useDelimiter(".");
1311

14-
// Read data from a file
1512
while (input.hasNext()) {
16-
String firstName = input.next();
17-
String mi = input.next();
18-
String lastName = input.next();
13+
String name = input.next();
1914
int score = input.nextInt();
20-
System.out.println(
21-
firstName + " " + mi + " " + lastName + " " + score);
15+
System.out.println("name:" + name + " score:" + score);
2216
}
2317

24-
// Close the file
2518
input.close();
2619
}
2720
}
1.88 KB
Binary file not shown.

src/main/java/file/ReplaceText.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,28 @@
44
import java.io.PrintWriter;
55
import java.util.Scanner;
66

7+
//java ReplaceText sourceFile targetFile oldString newString
78
public class ReplaceText {
89

910
public static void main(String[] args) throws Exception {
10-
// Check command line parameter usage
1111
if (args.length != 4) {
12-
System.out.println(
13-
"Usage: java ReplaceText sourceFile targetFile oldStr newStr");
12+
System.out.println("Usage:java ReplaceText sourceFile targetFile oldStr newStr");
1413
System.exit(1);
1514
}
1615

17-
// Check if source file exists
1816
File sourceFile = new File(args[0]);
1917
if (!sourceFile.exists()) {
20-
System.out.println("Source file " + args[0] + " does not exist");
18+
System.out.println("Source file " + args[0] + " does not exist!");
2119
System.exit(2);
2220
}
2321

24-
// Check if target file exists
2522
File targetFile = new File(args[1]);
2623
if (targetFile.exists()) {
27-
System.out.println("Target file " + args[1] + " already exists");
24+
System.out.println("Target file " + args[1] + " already exists!");
2825
System.exit(3);
2926
}
3027

3128
try (
32-
// Create input and output files
3329
Scanner input = new Scanner(sourceFile);
3430
PrintWriter output = new PrintWriter(targetFile);
3531
) {

src/main/java/file/TestFileClass.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
public class TestFileClass {
66

77
public static void main(String[] args) {
8-
File file = new File("andrew.txt");
8+
File file = new File("test/andrew.txt");
99
System.out.println("Does it exists? " + file.exists());
10-
System.out.println("The file has " + file.length() + " bytes");
11-
System.out.println("Can it be read? " + file.canRead());
12-
13-
System.out.println("Can it be written? " + file.canWrite());
14-
System.out.println("Is it a directory? " + file.isDirectory());
15-
System.out.println("Is it a file? " + file.isFile());
16-
System.out.println("Is it absolute? " + file.isAbsolute());
17-
System.out.println("Is it hidden? " + file.isHidden());
18-
System.out.println("Absolute path is " + file.getAbsolutePath());
19-
System.out.println("Last modified on " + new java.util.Date(file.lastModified()));
10+
System.out.println("The file has "+file.length()+" bytes");
11+
System.out.println("Can it be read? "+file.canRead());
12+
System.out.println("Can it be write? "+file.canWrite());
13+
System.out.println("Is it a directory? "+file.isDirectory());
14+
System.out.println("Is it a file? "+file.isFile());
15+
System.out.println("Is it absolute? "+file.isAbsolute());
16+
System.out.println("Is it hidden? "+file.isHidden());
17+
System.out.println("Absolute path is ? "+file.getAbsolutePath());
18+
System.out.println("Last modified on ? "+new java.util.Date(file.lastModified()));
2019
}
2120
}

src/main/java/file/WriteData.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package file;
22

3+
import java.io.File;
34
import java.io.FileNotFoundException;
5+
import java.io.PrintWriter;
46

57
public class WriteData {
68

7-
public static void main(String[] args) throws Exception {
8-
java.io.File file = new java.io.File("scores.txt");
9-
if (file.exists()) {
10-
System.out.println("File already exists");
9+
public static void main(String[] args) throws FileNotFoundException {
10+
File file = new File("scores.txt");
11+
if(file.exists()){
12+
System.out.println("File already exists!");
1113
System.exit(0);
1214
}
1315

14-
// Create a file
15-
java.io.PrintWriter output = new java.io.PrintWriter(file);
16+
PrintWriter output = new PrintWriter(file);
1617

17-
// Write formatted output to the file
18-
output.print("John T Smith ");
18+
output.print("Andrew Programming ");
1919
output.println(90);
20-
output.print("Eric K Jones ");
21-
output.println(85);
2220

23-
// Close the file
21+
output.print("Eric ");
22+
output.println(80);
23+
2424
output.close();
2525
}
26+
2627
}
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package file;
22

3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.PrintWriter;
6+
37
public class WriteDataWithAutoClose {
48

5-
public static void main(String[] args) throws Exception {
6-
java.io.File file = new java.io.File("scores.txt");
9+
public static void main(String[] args) throws FileNotFoundException {
10+
File file = new File("scores.txt");
711
if (file.exists()) {
8-
System.out.println("File already exists");
12+
System.out.println("File already exists!");
913
System.exit(0);
1014
}
1115

12-
try (
13-
// Create a file
14-
java.io.PrintWriter output = new java.io.PrintWriter(file);
15-
) {
16-
// Write formatted output to the file
17-
output.print("John T Smith ");
16+
17+
try(PrintWriter output = new PrintWriter(file)){
18+
output.print("Andrew Programming1111111");
1819
output.println(90);
19-
output.print("Eric K Jones ");
20-
output.println(85);
20+
21+
output.print("Eric");
22+
output.println(80);
2123
}
24+
2225
}
26+
2327
}
28+

test/andrew.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test!

0 commit comments

Comments
 (0)