how to read files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eyeofsoul
    New Member
    • Sep 2007
    • 31

    #1

    how to read files

    hello..can anybody give me a guide on how to read any file using java..i am working in encrypting files..any file..can anybody give a hand.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by eyeofsoul
    hello..can anybody give me a guide on how to read any file using java..i am working in encrypting files..any file..can anybody give a hand.
    How to read a file depends on the type of the files you are reading.
    How you read text files is different from how you read binary files and different from how you would read .pdf files.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by eyeofsoul
      hello..can anybody give me a guide on how to read any file using java..i am working in encrypting files..any file..can anybody give a hand.
      As you're working on encryption, I guess you will want to read and write the files in binary format. To get you started, this Article should give you a rough idea.

      You might however not want to use the Scanner class, as it interprets what it reads, but FileInputStream s and BufferedReaders instead.

      Greetings,
      Nepomuk

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by nepomuk
        You might however not want to use the Scanner class, as it interprets what it reads, but FileInputStream s and BufferedReaders instead.

        Greetings,
        Nepomuk
        Readers do quite a bit of interpretation as well; better stick to FileInputStream s
        for reading raw bytes.

        kind regards,

        Jos

        Comment

        • eyeofsoul
          New Member
          • Sep 2007
          • 31

          #5
          i think i got the idea.first i must convert the file into binary right?!but how do i convert the files into binary?..for example i want to convert a jpeg file into binary.how can it be done?

          Comment

          • saharf
            New Member
            • Sep 2007
            • 4

            #6
            Originally posted by eyeofsoul
            hello..can anybody give me a guide on how to read any file using java..i am working in encrypting files..any file..can anybody give a hand.
            hi. here is a way that you can read the content of a file, but before you should specify your file name
            FileInputStream fis = new FileInputStream ("E:/Sahar/source codes/exam.txt");
            int num = fis.available() ;
            byte b[] = new byte[num];
            fis.read(b);
            String content = new String(b , 0 , num);
            System.out.prin tln("content : "+ content);

            in the 1st line, you shud write d name of file u want work on it
            in d 2nd line: u r asking to find d number of bytes that can b read from fiel
            in d 3rd line: making a byte array with d size of ur file
            .......: reading byte by byte of file
            .......: making a string to write d result of reading file
            .......: and finally writing d result as a string

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by eyeofsoul
              i think i got the idea.first i must convert the file into binary right?!but how do i convert the files into binary?..for example i want to convert a jpeg file into binary.how can it be done?
              No, that's not it; at the lowest level every file is just a sequence of bytes; it's us,
              humans who want to interpret the sequence of bytes, e.g. if all of them represent
              printable characters and newline (and/or carriage return) characters, it's us humans
              that call such a sequence a 'text file'. If the sequence has some other structure,
              defined by us, we might call them jpeg files etc. etc.

              A FileInputStream simply reads those bytes from a file and that's the class you
              need if you just want read bytes from a file sequentially.

              So you don't 'convert a file to binary' because every file already is a binary thing.

              kind regards,

              Jos

              Comment

              • eyeofsoul
                New Member
                • Sep 2007
                • 31

                #8
                Originally posted by saharf
                hi. here is a way that you can read the content of a file, but before you should specify your file name
                FileInputStream fis = new FileInputStream ("E:/Sahar/source codes/exam.txt");
                int num = fis.available() ;
                byte b[] = new byte[num];
                fis.read(b);
                String content = new String(b , 0 , num);
                System.out.prin tln("content : "+ content);
                thanks you all for giving me the picture of reading a file,,hehe..fin ally i can read anyfile that i want.hehe..this code really help..

                Comment

                Working...