0

I'm doing example from the book

"Java The Complete Reference Ninth Edition"

that demonstrates FileInputStream using try-with resources. In the output I've got "I/O Error: java.io.FileNotFoundException: C:\Users\user\Documents\NetBeansProjects\JavaExam\FileInputStreamDemo.java (Can't find file)". The code:

package javaexam;
import java.io.*;

class FileInputStreamDemo {
    public static void main(String args[]) {
        int size;

        // Use try-with-resources to close the stream.
        try ( FileInputStream f = 
                new FileInputStream("C:\\Users\\user\\Documents\\NetBeansProjects\\JavaExam\\FileInputStreamDemo.java"))  
        {

            System.out.println("Total Available Bytes: " + (size = f.available()));
            int n = size/40;
            System.out.println("First " + n + " bytes of the file one read() at a time");
            for (int i = 0; i < n; i++) {
                System.out.print((char) f.read());
            }
            System.out.println("\nStill Available: " + f.available());
            System.out.println("Reading the next " + n + " with one read(b[])");
            byte b[] = new byte[n];
            if (f.read(b) != n) {
                System.err.println("couldn't read " + n + " bytes.");
            }
            System.out.println(new String(b, 0, n));
            System.out.println("\nStill Available: " + (size = f.available()));
            System.out.println("Skipping half of remaining bytes with skip()");
            f.skip(size/2);
            System.out.println("Reading " + n/2 + " into the end of array");
            if (f.read(b, n/2, n/2) != n/2) {
                System.err.println("couldn't read " + n/2 + " bytes.");
            }
            System.out.println(new String(b, 0, b.length));
            System.out.println("\nStill Available: " + f.available());
        } catch (IOException e) {
            System.out.println("I/O Error: " + e);
        }
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Eugenia Ozirna
  • 505
  • 2
  • 14
  • 1
    Most obvious question: can you find `C:\Users\user\Documents\NetBeansProjects\JavaExam\FileInputStreamDemo.java` on the computer on which you are running this code? – Andy Turner Jan 14 '16 at 16:32
  • By the way, quoting [@TJCrowder](http://stackoverflow.com/a/2417546/3788176): "With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms". You don't need to use `\\ ` in your string literal, you can use `/` instead. – Andy Turner Jan 14 '16 at 16:33
  • Of course, I can find this file on my computer by the that very same path I specified. – Eugenia Ozirna Jan 14 '16 at 17:30
  • Following your advice, I changed this string literal \\ to that one \, and I got the whole line marked out by red colour with a remark "illegal escape character". – Eugenia Ozirna Jan 14 '16 at 17:33
  • If you read the comment carefully, I said `/`, not \. – Andy Turner Jan 14 '16 at 19:47
  • After careful reading, I changed i to /... it didn't helped.. Thanks for advice. – Eugenia Ozirna Jan 14 '16 at 19:55

2 Answers2

0

I think your issue might be that you're using backslashes. I'm not certain, though. Try using a BufferedReader instead.

File file = new File("C:/Users/user/Documents/NetBeansProjects/JavaExam/FileInputStreamDemo.java");
BufferedReader bufferedreader = new BufferedReader(new FileReader(file));

Using this you should be able to read it a lot more easily, and line by line instead of byte by byte.

ViperLordX
  • 125
  • 1
  • 10
  • Thanks for advice, I've tried it, but that resulted in the following remark: incompatible types: try-with-resources not applicable to variable type (File cannot be converted to AutoCloseable) – Eugenia Ozirna Jan 14 '16 at 17:39
  • You can't use it the same way you would use a FileInputStream. You use it like an iterator. You test if it has another line, then get the next line if it does. – ViperLordX Jan 15 '16 at 04:04
0

The file you are looking should be in the src folder under package javaexam. Otherwise it won't be compiled. By the Java conventions each public class should have the name of the file and reside in the folder with the name of the package.

new FileInputStream("C:\\Users\\user\\Documents\\NetBeansProjects\\JavaExam\\src\\javaexam\\FileInputStreamDemo.java"))
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • It IS in the src folder under package javaexam. – Eugenia Ozirna Jan 14 '16 at 17:35
  • then you should use it in the path beside the things that may be you might have looking for the file of another user and don't have administrative privileges. And it will be completely wrong if you use this path on Linux or Mac. – Roman C Jan 14 '16 at 17:46
  • What path do you mean? Is it in Project Properties? – Eugenia Ozirna Jan 14 '16 at 17:49
  • path to the file `"C:\\Users\\user\\Documents\\NetBeansProjects\\JavaExam\\src\\javaexam\\"`. Are you on Windows machine? Open `cmd` window and use `cd C:\Users\user\Documents\NetBeansProjects\JavaExam\src\javaexam` if it worked then use `dir` and find `FileInputStreamDemo.java`. – Roman C Jan 14 '16 at 17:53
  • I've done exactly as you described, using dir in cmd I found this file... so what's next? – Eugenia Ozirna Jan 14 '16 at 18:01
  • next you should try to read it `type FileInputStreamDemo.java` – Roman C Jan 14 '16 at 18:14
  • I've tried to read it, and got a message that a file could not be read, and a window of choosing the programs for reading this file. Only when I choose netbeans, the file opens in the compiler. – Eugenia Ozirna Jan 14 '16 at 18:59
  • may be netbeans was using this file when you tried to read it – Roman C Jan 14 '16 at 19:52