Search code examples
javaio

Read file in java


I have file in my computer which have .file extension , I want to read it 9 character by 9 character. I know that I can read file by this code, but what should I do when my file is not .txt?does java support to read .file s with this code?

                InputStream is = null;
                InputStreamReader isr = null;
                BufferedReader br = null;
                is = new FileInputStream("c:/test.txt");
                // create new input stream reader
                isr = new InputStreamReader(is);
                // create new buffered reader
                br = new BufferedReader(isr);
                // creates buffer
                char[] cbuf = new char[is.available()];
                for (int i = 0; i < 90000000; i += 9) {
                // reads characters to buffer, offset i, len 9
                br.read(cbuf, i, 9);}

Solution

  • The extension of a file is totally irrelevant. Extensions like .txt are mere conventions to help your operating system choose the right program when you open it.

    So you can store text in any file (.txt, .file, .foobar if you are so inclined...), provided you know what kind of data it contains, and read it accordingly from your program.

    So yes, Java can read .file files, and your code will work fine if that file contains text.