8 Replies Latest reply: Apr 17, 2008 10:04 AM by 807601 RSS

    Reading single chars

    807601
      What can I use to read single chars from a notepad file?
        • 1. Re: Reading single chars
          807601
          mixalissen wrote:
          What can I use to read single chars from a notepad file?
          Y'all wantah bookmark this: [http://java.sun.com/javase/6/docs/api/]
          • 2. Re: Reading single chars
            807601
            [http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream.html]
            Look at the readChar()...readByte()..etc.
            • 3. Re: Reading single chars
              807601
              Praveen_Forum wrote:
              [http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream.html]
              Look at the readChar()...readByte()..etc.
              I wouldn't use a DataInputStream. If one is reading chars from a "Notepad" file that sounds like text, not binary, input. Use a Reader, not an InputStream like DataInputStream. In particular, DataInputStream's readChar method reads exactly two bytes of data and assumes they are the bytes of a unicode value (bigendian).

              <aside>
              Hands up, who thinks Praveen_Forum is a troll? His answers are consistently either wrong or off-topic.
              </aside>
              • 4. Re: Reading single chars
                807601
                BigDaddyLoveHandles wrote:
                Hands up, who thinks Praveen_Forum is a troll? His answers are consistently either wrong or off-topic.
                Hey, my answers are pretty often wrong. And I don't think I'm a troll. That's just how I learn...by saying my first guess and having those much more knowledgeable call me an idiot :)
                • 5. Re: Reading single chars
                  807601
                  I've been seeing a lot of people using or recommending DataInputStream inappropriately lately. Often they're just using read() or read(byte[]), so they're not getting wrong results, like the OP would if they used readChar() in this case, but it's obvious they've got the wrong idea about what DataInputStream is for. Why is that class suddenly the first thing all these n00bs reach for?
                  • 6. Re: Reading single chars
                    807601
                    Can anyone make things clear
                    DataInputStream Vs Reader and why reader??
                    • 7. Re: Reading single chars
                      807601
                      Praveen_Forum wrote:
                      Can anyone make things clear
                      DataInputStream Vs Reader and why reader??
                      You're talking about the difference between binary files (like .exe, .wav, .ppt) and text files. DataInputStream expects the file to be in a specific format that reflects the structure of a Java object in memory--the kind of file that would only be written by a Java program. Its readChar(), readLine() and readUTF() methods may read text, but they do it in very specific ways that are incompatible with most text file formats. (See DataInput for details.)

                      A Reader, on the other hand, is designed to read text that was written in one of the standard formats, or encodings, that are known to every application that deals with text: editors, browsers, databases, whatever. A FileReader uses whatever encoding is the default on the platform the code is running on; if you need to specify a certain encoding, like UTF-8, you use an InputStreamReader:
                      Reader isr = new InputStreaReader(new FileInputStream("theFile.txt"), "UTF-8");
                      A basic Reader provides methods for reading single characters or several at a time, but you usually want to wrap it in a BufferedReader for efficiency and for the convenience of its readLine() method:
                      BufferedReader br = new BufferedReader(isr);
                      String line = null;
                      while ((line = br.readLine()) != null)
                      {
                        // etc.
                      }
                      • 8. Re: Reading single chars
                        807601
                        This helps me a lot ThankQ
                        uncle_alice wrote:
                        Praveen_Forum wrote:
                        Can anyone make things clear
                        DataInputStream Vs Reader and why reader??
                        You're talking about the difference between binary files (like .exe, .wav, .ppt) and text files. DataInputStream expects the file to be in a specific format that reflects the structure of a Java object in memory--the kind of file that would only be written by a Java program. Its readChar(), readLine() and readUTF() methods may read text, but they do it in very specific ways that are incompatible with most text file formats. (See DataInput for details.)

                        A Reader, on the other hand, is designed to read text that was written in one of the standard formats, or encodings, that are known to every application that deals with text: editors, browsers, databases, whatever. A FileReader uses whatever encoding is the default on the platform the code is running on; if you need to specify a certain encoding, like UTF-8, you use an InputStreamReader:
                        Reader isr = new InputStreaReader(new FileInputStream("theFile.txt"), "UTF-8");
                        A basic Reader provides methods for reading single characters or several at a time, but you usually want to wrap it in a BufferedReader for efficiency and for the convenience of its readLine() method:
                        BufferedReader br = new BufferedReader(isr);
                        String line = null;
                        while ((line = br.readLine()) != null)
                        {
                        // etc.
                        }