CS116      Lab 9 - File I/O

Objectives:

  1. Before starting this lab you should have read chapter 12 in your text.
     
  2. Open DrJava
     
  3. A sound sample contains just a numeric value.  There is no reason why we can't take a sound file and convert it to a text file of numbers.  The following code does just that.

    Observe that we use the BufferedWriter class to write to a file.  You should import java.io.*  in order to use this class.

    Also observe that the code for using a file must be contained inside a try{}catch{} structure.  That way, if anything should go wrong in trying to access that file, the code inside the catch will be executed.

    Add this code to the Sound class and try it out.  Take a look at the contents of the text file that is produced.

     /**
    * Method to write out the values in the sound to a file as text
    * @param fileName the name of the file to write to
    */
    public void writeSamplesAsText(String fileName){
        int value = 0;
        // try to write the file
        try{
            // try to open the buffered writer
            BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));

            // loop through the samples
            for (int i=0; i<this.getLength(); i++){
                // get the int value
                value = this.getSampleValueAt(i);

                // write it as a string (text)
                writer.write(String.valueOf(value));
           
                // add the new line
                writer.newLine();
            }
            writer.close();
        }catch(Exception ex){
            SimpleOutput.showError("Error during write");
            ex.printStackTrace();
        }
    }
     
  4. We can now take a text file of numeric values and make a sound object out of it.  Add the following code and try it out.  (You may need to make the Sound object, s, longer if you have a really long file).

    Observe that lines are read from the file as Strings and then we use Integer.parseInt to convert the string into an integer value.

     /**
    * Method to create a sound from a text file
    * @param fileName the name of the file to read from
    * @return the created sound object
    */
    public static Sound createSoundFromTextFile(String fileName){
        String line = null;
        int value = 0;

        // create the sound to read into
        Sound s = new Sound(50000);

        // try to read from the file
        try{
            // create the buffered reader
            BufferedReader reader = new BufferedReader(new FileReader(fileName));

            // loop reading the values
            int index = 0;
            while((line=reader.readLine()) != null &&
                index <= s.getLength()){
                value = Integer.parseInt(line);
                s.setSampleValueAt(index,value);
                index++;
            }
            reader.close();
        }catch (FileNotFoundException ex){
            SimpleOutput.showError("Couldn't find file " + fileName);
            fileName = FileChooser.pickAFile();
            s = createSoundFromTextFile(fileName);
        }catch (Exception ex){
            SimpleOutput.showError("Error during read or write");
            ex.printStackTrace();
        }
        return s;
    }

     

  5.  
    Assignment:
    Write a class Code which has two static methods encode and decode.  Both these methods take two file names as parameters, a source file and a target file.  You should read text from the source file and in the encode method every character is changed by adding one to the character code.  You should write the encoded text to the target file.  The decode method reads the encoded text and decodes it back to plain text by reversing the process.

    You may find the String method toCharArray useful to convert a String to an array of characters.
    To convert an array of characters to a String use the static method String.copyValueOf.

    To do the encoding you can cast a char to an int, add 1, and cast back to a char.

    Hint:  Both methods will need to loop through and read the lines of code.  For each line that you read in, convert all the characters and then write it to the output file.  Then go and process the next line of input.


     

  6. Send Code.java  to me as well as a file of encoded text using GoucherLearn.