|
44 | 44 | <p> |
45 | 45 | Java includes a class called <c>File</c> in the <c>io</c> library. The class can be imported with the following line. Be sure to capitalize <c>File</c>. |
46 | 46 | </p> |
47 | | - <program xml:id = "file-class-import-example" interactive="activecode" language="java"> |
| 47 | + <program xml:id = "file-class-import-io-example" interactive="activecode" language="java"> |
48 | 48 | <code> |
49 | 49 | import java.io.File; |
50 | 50 | import java.io.IOException;public class Main { |
|
94 | 94 | <p> |
95 | 95 | 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>. |
96 | 96 | </p> |
97 | | - <datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="no" include-source="yes"> |
| 97 | + <datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="yes"> |
98 | 98 | <pre> |
99 | 99 | empty file |
100 | 100 | </pre> |
101 | 101 | </datafile> |
102 | | - <program interactive="activecode" language="java"> |
| 102 | + <program xml:id="create-file-exp-1" interactive="activecode" language="java" add-files="my-file-8-2-1"> |
103 | 103 | <code> |
104 | | - import java.io.File;public class Main { |
| 104 | + import java.io.File; |
| 105 | + public class Main { |
105 | 106 | public static void main(String[] args) { |
106 | 107 | File myFile = new File("myfile.txt"); |
107 | 108 | System.out.println(myFile); |
|
123 | 124 | <p> |
124 | 125 | First, lets look at the equivalent Python code: |
125 | 126 | </p> |
126 | | - <datafile label="my-file-8-2-2" filename="myfile.txt" xml:id= "my-file-8-2-2" editable="no" include-source="yes"> |
| 127 | + <datafile label="my-file-8-2-2" filename="myfile.txt" xml:id= "my-file-8-2-2" editable="yes"> |
127 | 128 | <pre> |
128 | 129 | empty file |
129 | 130 | </pre> |
130 | 131 | </datafile> |
131 | | - <program interactive="activecode" language="python"> |
| 132 | + <program xml:id="creat-file-exp-2" interactive="activecode" language="python" add-files="my-file-8-2-2"> |
132 | 133 | <code> |
133 | 134 | filename = "newfile.txt" |
134 | 135 | print(f"Attempting to write to '{filename}' using 'w' mode...") |
|
145 | 146 | <p> |
146 | 147 | Now, let's look at Java code that accomplishes the same task: |
147 | 148 | </p> |
148 | | - <datafile label="my-file-8-2-3" filename="myfile.txt" xml:id= "my-file-8-2-3" editable="no" include-source="yes"> |
| 149 | + <datafile label="my-file-8-2-3" filename="myfile.txt" xml:id= "my-file-8-2-3" editable="yes"> |
149 | 150 | <pre> |
150 | 151 | empty file |
151 | 152 | </pre> |
152 | 153 | </datafile> |
153 | | - <program interactive="activecode" language="java"> |
| 154 | + <program xml:id="create-file-exp3" interactive="activecode" language="java" add-files="my-file-8-2-3"> |
154 | 155 | <code> |
155 | 156 | import java.io.File; |
156 | 157 | import java.io.IOException; |
|
181 | 182 | <p> |
182 | 183 | Let's take a look at how we can use Java to read file contents. We'll start again with library imports and building a class, this time importing the Scanner and FileNotFoundException classes. We will call this class ReadFile: |
183 | 184 | </p> |
184 | | - <datafile label="my-file" filename="myfile.txt" xml:id= "my-file" editable="no" include-source="yes"> |
| 185 | + <datafile label="my-file" filename="myfile.txt" xml:id= "my-file" editable="no"> |
185 | 186 | <pre> |
186 | 187 | 1 |
187 | 188 | 2 |
|
0 commit comments