Skip to content

Commit b3310f6

Browse files
authored
Merge pull request #138 from coco3427/ch8.2
changes 8.2's introduction and uses a new opening paragraph
2 parents 3129d72 + be3a60d commit b3310f6

1 file changed

Lines changed: 1 addition & 28 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,8 @@
8484

8585
<section xml:id="create-file">
8686
<title>Creating Files</title>
87-
88-
<p>
89-
We will now create a <c>File</c> object. It is important to create a meaningful name for the <c>File</c> object. We will call ours <c>myFile</c>.
90-
</p>
91-
<datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="yes">
92-
<pre>
93-
empty file
94-
</pre>
95-
</datafile>
96-
<program xml:id="create-file-exp-1" interactive="activecode" language="java" add-files="my-file-8-2-1">
97-
<code>
98-
import java.io.File;
99-
public class Main {
100-
public static void main(String[] args) {
101-
File myFile = new File("myfile.txt");
102-
System.out.println(myFile);
103-
}
104-
}
105-
</code>
106-
</program>
107-
108-
<note>
109-
<p>
110-
<c>myFile</c> is the name of the object within the program, while <c>myfile.txt</c> is the name of the file itself and will be the file name if the operation that creates the file is successful.
111-
</p>
112-
</note>
113-
11487
<p>
115-
Now that we have created a new <c>File</c> object, we can create a file using the <c>createNewFile()</c> method from the <c>File</c> class. While the previous line of code creates an object within the program for the file, this method actually does the work of creating a file and saving it in the current working directory. This method returns a boolean value. If the method returns <c>true</c>, the file was successfully created. If the method returns <c>false</c>, there is already a file using the chosen file name. We can use this method's possible return values in tandem with an try/catch structure to determine if the file was created, or catch the error if a file with that file name already exists in the directory.
88+
Now lets learn how to make a file in Java. In Python files can be made using the <c>open()</c> function on a file path that doesn't exist yet. Similarly, in Java you create a file by using the <c>createNewFile()</c> method on a <c>File</c> object. This method actually does the work of creating a file and saving it in the current working directory, and returns a boolean value of either true or false if the file is successfully created. We can use this method's possible return values in tandem with an try/catch structure to determine if the file was created, or catch the error if a file with that file name already exists in the directory.
11689
</p>
11790

11891
<p>

0 commit comments

Comments
 (0)