File tree Expand file tree Collapse file tree 10 files changed +52
-56
lines changed Expand file tree Collapse file tree 10 files changed +52
-56
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- John T Smith 90
2
- Eric K Jones 85
1
+ Andrew 90
2
+ Eric 80
Original file line number Diff line number Diff line change
1
+ Programming 90
2
+ Eric 80
Original file line number Diff line number Diff line change 1
1
package file ;
2
2
3
+ import java .io .File ;
3
4
import java .util .Scanner ;
4
5
5
6
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" );
9
7
10
- // Create a Scanner for the file
8
+ public static void main (String [] args ) throws Exception {
9
+ File file = new File ("scores.txt" );
11
10
Scanner input = new Scanner (file );
12
- // input.useDelimiter(".");
13
11
14
- // Read data from a file
15
12
while (input .hasNext ()) {
16
- String firstName = input .next ();
17
- String mi = input .next ();
18
- String lastName = input .next ();
13
+ String name = input .next ();
19
14
int score = input .nextInt ();
20
- System .out .println (
21
- firstName + " " + mi + " " + lastName + " " + score );
15
+ System .out .println ("name:" + name + " score:" + score );
22
16
}
23
17
24
- // Close the file
25
18
input .close ();
26
19
}
27
20
}
Original file line number Diff line number Diff line change 4
4
import java .io .PrintWriter ;
5
5
import java .util .Scanner ;
6
6
7
+ //java ReplaceText sourceFile targetFile oldString newString
7
8
public class ReplaceText {
8
9
9
10
public static void main (String [] args ) throws Exception {
10
- // Check command line parameter usage
11
11
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" );
14
13
System .exit (1 );
15
14
}
16
15
17
- // Check if source file exists
18
16
File sourceFile = new File (args [0 ]);
19
17
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! " );
21
19
System .exit (2 );
22
20
}
23
21
24
- // Check if target file exists
25
22
File targetFile = new File (args [1 ]);
26
23
if (targetFile .exists ()) {
27
- System .out .println ("Target file " + args [1 ] + " already exists" );
24
+ System .out .println ("Target file " + args [1 ] + " already exists! " );
28
25
System .exit (3 );
29
26
}
30
27
31
28
try (
32
- // Create input and output files
33
29
Scanner input = new Scanner (sourceFile );
34
30
PrintWriter output = new PrintWriter (targetFile );
35
31
) {
Original file line number Diff line number Diff line change 5
5
public class TestFileClass {
6
6
7
7
public static void main (String [] args ) {
8
- File file = new File ("andrew.txt" );
8
+ File file = new File ("test/ andrew.txt" );
9
9
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 ()));
20
19
}
21
20
}
Original file line number Diff line number Diff line change 1
1
package file ;
2
2
3
+ import java .io .File ;
3
4
import java .io .FileNotFoundException ;
5
+ import java .io .PrintWriter ;
4
6
5
7
public class WriteData {
6
8
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! " );
11
13
System .exit (0 );
12
14
}
13
15
14
- // Create a file
15
- java .io .PrintWriter output = new java .io .PrintWriter (file );
16
+ PrintWriter output = new PrintWriter (file );
16
17
17
- // Write formatted output to the file
18
- output .print ("John T Smith " );
18
+ output .print ("Andrew Programming " );
19
19
output .println (90 );
20
- output .print ("Eric K Jones " );
21
- output .println (85 );
22
20
23
- // Close the file
21
+ output .print ("Eric " );
22
+ output .println (80 );
23
+
24
24
output .close ();
25
25
}
26
+
26
27
}
Original file line number Diff line number Diff line change 1
1
package file ;
2
2
3
+ import java .io .File ;
4
+ import java .io .FileNotFoundException ;
5
+ import java .io .PrintWriter ;
6
+
3
7
public class WriteDataWithAutoClose {
4
8
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" );
7
11
if (file .exists ()) {
8
- System .out .println ("File already exists" );
12
+ System .out .println ("File already exists! " );
9
13
System .exit (0 );
10
14
}
11
15
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" );
18
19
output .println (90 );
19
- output .print ("Eric K Jones " );
20
- output .println (85 );
20
+
21
+ output .print ("Eric" );
22
+ output .println (80 );
21
23
}
24
+
22
25
}
26
+
23
27
}
28
+
Original file line number Diff line number Diff line change
1
+ This is a test!
You can’t perform that action at this time.
0 commit comments