sriram
sriram

Reputation: 378

read content of File

I have written a program for reading the contents of a file.

import java.io.*;

public class FileLineReader {
  public static void main(String args[]) {
    try {
      FileReader reader =
          new FileReader("C:\\Users\\sriram\\Documents\\Java Programs\\" +
                         args[0]);
      BufferedReader buffer = new BufferedReader(reader); 
      String fileContent;
      while ((fileContent = buffer.readLine()) != null) {
        System.out.println(fileContent);
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

So basically it reads contents from a particular folder. If I give input as FileLineReader.class which is the .class file of the Java program the program outputs the bytecode but gives me a beep sound. For all other files it gives the output properly.

Can anyone tell me why?

Upvotes: 1

Views: 1729

Answers (2)

Adam Liss
Adam Liss

Reputation: 48330

FileLineReader.class is most likely a binary (compiled Java) file that happens to contain a byte with a value of 7, which is the ASCII code for the bell character. Many terminals will beep when an ASCII 7 is displayed.

Upvotes: 4

cen
cen

Reputation: 2943

If you are reading binary files they can contain a sequence of bytes that represent a random sound. When you send such a sequence to standard output the OS will play the sound.

Upvotes: -2

Related Questions