This discussion is archived
5 Replies Latest reply: Mar 7, 2007 5:15 AM by 807606 RSS

Using FileInputStream to view File Details.

807606 Newbie
Currently Being Moderated
Hi,

I am unable to view the contents of this XML file
using FileInputStream.It doest display anything at all.


Am I missing anything? Can someone please help?
File f = new File("C:/Order.xml");
          
FileInputStream fis = new FileInputStream(f);
        
StringBuffer sb = new StringBuffer();
byte b[] = new byte[1];
int line;
        
        while ((line = fis.read())!= -1) 
        {
            sb.append(new String(b));
        }

        System.out.println(sb.toString());
  • 1. Re: Using FileInputStream to view File Details.
    807606 Newbie
    Currently Being Moderated
    what does that byte[] b have to do with anything? you're ignoring the actual data read from the file and just adding blank strings to your buffer until the end of the file is reached
  • 2. Re: Using FileInputStream to view File Details.
    807606 Newbie
    Currently Being Moderated
    You're reading into a variable called line, then adding to your string buffer from a variable called b. You're not using the value you've read into line.

    When reading text files, I always use a BufferedReader, I find it more convenient. You may want to go look for an example of how to.
  • 3. Re: Using FileInputStream to view File Details.
    807606 Newbie
    Currently Being Moderated
    You're reading into a variable called line, then
    adding to your string buffer from a variable called
    b. You're not using the value you've read into line.

    When reading text files, I always use a
    BufferedReader, I find it more convenient. You may
    want to go look for an example of how to.
    in fact, why bother reading XML as raw text at all? granted, you may have a good reason for it, but much more probably you want to handle it as an XML document rather than raw text. manipulating XML as raw text is extremely tiresome, error-prone and difficult
  • 4. Re: Using FileInputStream to view File Details.
    807606 Newbie
    Currently Being Moderated
    OK.This is what I have done.
    while ((line = fis.read(b)) != -1) 
            {
                  sb.append(new String(b));
            }
            System.out.println(sb.toString());
    
              }catch(Exception e){
                   
              }
    But its showing me :

    1

    and the XML file contents,

    Why is 1 being shown?
  • 5. Re: Using FileInputStream to view File Details.
    807606 Newbie
    Currently Being Moderated
    import java.io.*;
    
    public class FullyReadAFile
    {
        static public byte[] readFully(String source) throws IOException
        {
            return readFully(new File(source));
        }
        
        static public byte[] readFully(File source) throws IOException
        {
            final DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(source)));
            final byte[] buffer = new byte[(int)source.length()];
            dis.readFully(buffer);
            dis.close();
            return buffer;
        }
        
        static public String readFullyAsString(String source, String fileEncoding) throws IOException, UnsupportedEncodingException
        {
            return readFullyAsString(new File(source), fileEncoding);
        }
        
        static public String readFullyAsString(File source, String fileEncoding) throws IOException, UnsupportedEncodingException
        {
            return new String(readFully(source), fileEncoding);
        }
        
        public static void main(String[] args) throws Exception
        {
            final String fileContent = readFullyAsString(System.getProperty("user.home") + "/user.sql", "UTF-8");
            System.out.println(fileContent);
        }
    }