Adan Vivero
Adan Vivero

Reputation: 422

What is wrong with my IOException in my slurp method?

I am making a slurp method which in essence accepts a File and returns its contents as a String.

I keep getting exception errors and I am not sure why. I don't know if I'm throwing the wrong exception, but I need help on addressing why I am receiving an exception, since I have a hard time understanding them. The error traces back to the line

content = new String (Files.readAllBytes(Paths.get(file.getAbsolutePath())));

underneath the try { }.

/**
   * Investigate Files.readAllBytes as a means of reading the file.
   * Reading files can fail if the file cannot be opened.
   * Mark this method as dangerous with a throws clause:
   * public static ...(...) throws IOException {
   * @return
   * @throws IOException
   */
  public static String slurp() throws IOException
  {
    String content = "";
    File file = new File(content);

    try
    {
      content = new String (Files.readAllBytes(Paths.get(file.getAbsolutePath())));
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }

    return content;
  }

What the errors say:

java.io.IOException: Is a directory
    at java.base/sun.nio.ch.FileDispatcherImpl.read0(Native Method)
    at java.base/sun.nio.ch.FileDispatcherImpl.read(FileDispatcherImpl.java:48)
    at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:283)
    at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:250)
    at java.base/sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:223)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:65)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:109)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:103)
    at java.base/java.nio.file.Files.read(Files.java:3163)
    at java.base/java.nio.file.Files.readAllBytes(Files.java:3216)
    at hw2.FileUtilities.slurp(FileUtilities.java:46)
    at hw2.FileUtilities.main(FileUtilities.java:14)

Upvotes: 0

Views: 919

Answers (0)

Related Questions