publicclassDogLauncher { publicstaticvoidmain(String[] args) { Dog d; d = newDog(); /** new is a way to introduce 实例 */ d.weightInPounds = 20; d.makeNoise(); } }
```b = a;``` make the arrow in the box b the same as a. (just pass the bits into the new scope)
Arrays are also objects!
## Lab6: File systems in Java ### Current working dictionary the directory from where you execute that Java program use `System.getProperty("user.dir")` to access in Bash or the terminal, `cwd` would give you the CWD. ### Absolute & Relative Paths `.` refers to CWD. `..` refers to the parent of CWD. parent's parent dictionary: `cd ../..` ### File & Directory Manipulation in Java #### files DO NOT create the new file. Just say: when we changed `f`, we refers to the `dummy.txt`. ```java File f = new File("dummy.txt");
create the new file:
1
f.createNewFile();
check if dummy.txt exists:
1
f.exists()
Writing the file in java is pretty ugly, so the class Utils.java was applied in this lab and Gitlet. For example, write a string to the file:
1
Utils.writeContents(f, "Hello World");
Directories
it also represents as File objects.
1
Filed=newFile("dummy");
to create:
1
d.mkdir();
Serializable
How to save more complicated state in our program? such as the Model object in our 2048 program? we can use java.io.Serializable interface to translate an object to a series of bytes that can then be stored in the file.