21 Jan 2008 present: IP, UK, WO, RR Commands learnt: HOw to read in data from a file.dat: 1) we can define an imagined channel between a java program and a file on the same directory 2) use Scanner to define the name of the channel if the direction is in, then two commands can be used easily: i) boolean xxxx.hasNextLine(); ii) we can read the next line by xxxx.nextLine(); our guess is: there is a pointer/marker which will moves to a new line (next line) each time after you call xxxx.nextLine(); 3) Note we need to use 'throws Exception' How to write to a file.dat: 1) we still need to use 'throws Exception' 2) similart to Scanner, we use PrintStream PrintStream outChannle = new PrintStream(new FileOutputStream("fileX")); // where fileX can be a file to be created, i.e. fileX does not have to be in the current directory already. 3) we use xxxx.print(); and xxxx.println() to write the data into the file. 4) note: fileX can be in a existing directory, this command cannot create a new directory. 5) note: fileX will be cleared before writing to it. Question: 1) Given a file : "testfile.dat", we want to write a method to change the first line to "HELLO": Algorithm I 1. define a inChannel, read in the data 2) define a outChannel and write out "HELLO" to the first line of the same file Algorithm II 1. define a outChannel and write out "HELLO" to the first line of the same file 2) Given a file, we want to first display the content, then remove the second line of the file. Algorithm: 1) define a inChannel, and read the data, then System.out.println(); 2) define a outChannel, and find the location, i.e the second line, then write ""; ?? Question: How can we update certain line of an existing file? 3) Write a method to print a interesting pattern to a text tile.txt