1 2 Previous Next 20 Replies Latest reply: Oct 30, 2007 4:08 AM by 807603 RSS

    BufferedReader problem

    758751
      Iam having a problem in my java application. My application is reading an xml file then saving it to a file using bufferedreader, but when i open the saved file, it is incomplete. i have tried to print the string that iam saving and it is complete, but the file contents is incomplete or cut. please help thx.
        • 1. Re: BufferedReader problem
          807603
          I assume you mean you are writing with a Writer not Reader. Close the BufferedWriter and not the Writer (or anything in the chain) it is wrapping.

          Edited by: sabre150 on Oct 30, 2007 8:55 AM
          • 2. Re: BufferedReader problem
            758751
            Yes, a bufferedwriter. My piece of code:

            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
                 BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\temp.xml"));
                 String cpy = "",str;
                   while( (str=in.readLine()) != null) {
                      //System.out.println(str);
                      cpy = cpy + "" + str.trim();
                   }
                   bw.write(cpy);
            • 3. Re: BufferedReader problem
              807603
              So close bw or anything still in the buffer may be lost!

              P.S. What a goddamed awful way to copy a file.

              Edited by: sabre150 on Oct 30, 2007 8:59 AM
              • 4. Re: BufferedReader problem
                758751
                thx for the reply. i will try it. btw, have you tried to parse an xml file? if yes, what parser are the fastest and more reliable?
                • 5. Re: BufferedReader problem
                  758751
                  why is it awful? can you suggest a better code for saving a file?
                  • 6. Re: BufferedReader problem
                    807603
                    Jepot wrote:
                    why is it awful? can you suggest a better code for saving a file?
                    Just read a block of bytes (not characters) and write them out. e.g.
                    BufferedinputStream is = new BufferedInputStream(urlConn.getInputStream());
                    byte[] buffer = new byte[4096];
                    BufferedOutputStream os = new BuffereeOutputStream(new FileOutputStream("your file"));
                    for (int count = 0; (count = is.read(buffer)) != -1;)
                    {
                        os.write(buffer, 0, count);
                    }
                    os.close();
                    is.close();
                    Edited by: sabre150 on Oct 30, 2007 9:04 AM
                    • 7. Re: BufferedReader problem
                      758751
                      iam using a string cause iam evaluating every line of the file before saving it. can you give a piece of code that uses bytes? if i use bytes can i still manipulate and process each line of it before writing to the file?

                      Edited by: Jepot on Oct 30, 2007 2:09 AM
                      • 8. Re: BufferedReader problem
                        807603
                        Jepot wrote:
                        thx for the reply. i will try it. btw, have you tried to parse an xml file? if yes, what parser are the fastest and more reliable?
                        I suspect your need for "fastest and more reliable" should give way to just using the inbuilt parser.
                        • 9. Re: BufferedReader problem
                          807603
                          Jepot wrote:
                          iam using a string cause iam evaluating every line of the file before saving it. can you give a piece of code that uses bytes? if i use bytes can i still manipulate and process each line of it before writing to the file?
                          No! But why do you need to "manipulate and process each line of it before writing to the file" if you are later going to read and process the file using an XML parser?
                          • 10. Re: BufferedReader problem
                            758751
                            because the xml file that is generated by the site contains a constant error so i need to replace every line.

                            the errors are:

                            the xml file always contains the string "12" at the beginning:

                            "12<?xml version = '1.0'?>......."


                            the attributes don't have a whitespace so iam getting a whitespace before attribute error:

                            <DATAROW num="3">

                            so i need to replace every "=" to " = "
                            • 11. Re: BufferedReader problem
                              807603
                              the attributes don't have a whitespace so iam getting a whitespace before attribute error:

                              <DATAROW num="3">

                              so i need to replace every "=" to " = "
                              I don't understand. The spaces are not needed.
                              • 12. Re: BufferedReader problem
                                807603
                                Jepot wrote:
                                because the xml file that is generated by the site contains a constant error so i need to replace every line.

                                the errors are:

                                the xml file always contains the string "12" at the beginning:

                                "12<?xml version = '1.0'?>......."
                                Then a would create a FilteredReader that changes "\n12" to "\n" and use that when creating the InputSource needed by the XML parser ( http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/InputSource.html ).
                                • 13. Re: BufferedReader problem
                                  EJP
                                  the xml file always contains the string "12" at the beginning:
                                  This would indicate that you are using the wrong character set when reading.
                                  the attributes don't have a whitespace so iam getting a whitespace before attribute error:

                                  <DATAROW num="3">

                                  so i need to replace every "=" to " = "
                                  I agree with sabre150.You don't need to do this. The problem if any is with whatever comes before 'num', not what's around '='.

                                  I suggest you have a look at the XML specification before proceeding any further.

                                  You should be able to just construct a Reader for this data and pass it directly to an XML DOM parser.
                                  • 14. Re: BufferedReader problem
                                    758751
                                    iam getting a whitespace error, and when i replace all "=" to " = " the error is resolved
                                    1 2 Previous Next