Results 1 to 13 of 13
  1. #1
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default Reading file method stops on blank line - It should not

    Hello ALL

    I have the bellow method which scans filr and look for a string in each line. For some reason it works on Debuger but when I deploy it it does not :(

    Looking throug I can see that it stopes in the blank line see example.

    Can someone advice why is going one, why and what the solution is.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class fileScan {
    
        public static String validationMessage = "";
    
        /**
         * @param args the command line arguments
         */
        public static void scanTheFile(String fileTitle) {
            File file = new File("C:\\SyncDB\\test.txt);
            FileInputStream fin;
            BufferedInputStream bin = null;
    
            try {
                fin = new FileInputStream(file);
                bin = new BufferedInputStream(fin);
    
                byte[] contents = new byte[1024];
    
                int bytesRead = 0;
                String strFileContents;
    
                while ((bytesRead = bin.read(contents)) != -1) {
                    System.out.append("" + bytesRead);
                    strFileContents = new String(contents, 0, bytesRead);
                    System.out.println(strFileContents);
                    if (strFileContents.contains("Summary Information")) {
                        System.out.println("successfullySynchronized");
                        validationMessage = "successfullySynchronized";
                    } else if (strFileContents.contains("Error: The selected objects are identical")) {
                        System.out.println("databasesTheSame");
                        validationMessage = "databasesTheSame";
                    } else if (strFileContents.contains("Error: Synchronization")) {
                        System.out.println("synchronizationError");
                        validationMessage = "synchronizationError";
                    } else {
                        validationMessage = "unknown";
                    }
                }
    
            } catch (IOException ioe) {
                System.out.println("Exception while reading the file " + ioe);
                System.out.println(ioe.getStackTrace().toString());
    
                validationMessage = "exceptionReadingFile";
            } finally {
                try {
                    if (bin != null) {
                        bin.close();
                    }
                } catch (IOException ioe) {
                    System.out.println("Error while closing the stream :" + ioe);
                    validationMessage = "exceptionClosingStream";
                }
    
            }
        }
    
        public static String returnValidationMessage() {
            return validationMessage;
        }
    }
    The full file is as follow:
    Java Code:
    SQL Compare Command Line V8.50.25.11
    ==============================================================================
    Copyright  Red Gate Software Ltd 1999-2010
    
    Serial Number: 002-002-113393-F475
    
    Registering data sources
    Creating mappings
    Comparing
    Applying Command Line Items
    Creating SQL
    Synchronizing databases (from DB1 to DB2)
    Error: Synchronization of 'XX.XX.XX.XX.DB1' and
    'XX.XX.XX.XX.DB2' failed: Invalid object name
    'dbo.fnC_Ext_testsp'.

    but I can see scanning get only to this point

    Java Code:
    SQL Compare Command Line V8.50.25.11
    ==============================================================================
    Copyright  Red Gate Software Ltd 1999-2010
    
    Serial Number: 002-002-113393-F475
    
    [B]STOPPED HERE ON THE LINE BEFORE[/B]

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Reading file method stops on blank line - It should not

    Your posted code does not compile.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Reading file method stops on blank line - It should not

    To see what your code is doing change this line:
    System.out.println(strFileContents);
    to this
    System.out.println("strFC=" + strFileContents + "<");

    and look at what is printed.

  4. #4
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default Re: Reading file method stops on blank line - It should not

    SO the file is as follow:

    SQL Compare Command Line V8.50.25.11
    ================================================== ============================
    Copyright Red Gate Software Ltd 1999-2010

    Serial Number: 002-002-113393-F475

    Registering data sources
    Creating mappings
    Comparing
    Applying Command Line Items
    Creating SQL
    Synchronizing databases (from DB1 to DB2)
    Error: Synchronization of 'XX.XX.XX.XX.DB1' and
    'XX.XX.XX.XX.DB2' failed: Invalid object name
    'dbo.fnC_Ext_testsp'.
    and the results from CMD
    165
    strFC=SQL Compare Command Line V8.50.25.11
    ================================================== ============================
    Copyright Red Gate Software Ltd 1999-2010

    <
    Ignore the 165 I am checking the bytesRead (System.out.println("" + bytesRead);)

    Hope this helps

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Reading file method stops on blank line - It should not

    Is that what prints out when you make the change I suggested in post#3?
    Why is the 165 on a separate line from the rest of the printed output?
    When I execute the code, it is on the same line.
    Does the test file have a leading blank line?

    Is the code you posted the same as what you are executing?
    I do NOT think it is because the code you posted does NOT compile.
    Post the code you are currently using for testing. Its a waste of time for me if I have different code.

  6. #6
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default Re: Reading file method stops on blank line - It should not

    Yes Norm I did the changed as suggested in post#3 :)

    The 126 is because I put System.out.append("" + bytesRead); without that the result is as fallow:

    strFC=SQL Compare Command Line V8.50.25.11
    ================================================== ============================
    Copyright Red Gate Software Ltd 1999-2010

    <
    The blank line is after this String "Copyright Red Gate Software Ltd 1999-2010"

    Just to be 100% clear the code is as follow:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;

    /**
    *
    * @author arek.jarosiewicz
    */
    public class fileScan {

    public static String validationMessage = "";

    /**
    * @param args the command line arguments
    */
    public static void scanTheFile(String fileTitle) {
    File file = new File("C:\\SyncDB\\" + fileTitle);
    FileInputStream fin;
    BufferedInputStream bin = null;

    try {
    fin = new FileInputStream(file);
    bin = new BufferedInputStream(fin);

    byte[] contents = new byte[1024];

    int bytesRead = 0;
    String strFileContents;

    while ((bytesRead = bin.read(contents)) != -1) {
    strFileContents = new String(contents, 0, bytesRead);
    System.out.println("strFC=" + strFileContents + "<");
    if (strFileContents.contains("Summary Information")) {
    System.out.println("successfullySynchronized");
    validationMessage = "successfullySynchronized";
    } else if (strFileContents.contains("Error: The selected objects are identical")) {
    System.out.println("databasesTheSame");
    validationMessage = "databasesTheSame";
    } else if (strFileContents.contains("Error: Synchronization")) {
    System.out.println("synchronizationError");
    validationMessage = "synchronizationError";
    } else {
    validationMessage = "unknown";
    }
    }

    } catch (IOException ioe) {
    System.out.println("Exception while reading the file " + ioe);
    System.out.println(ioe.getStackTrace().toString()) ;

    validationMessage = "exceptionReadingFile";
    } finally {
    try {
    if (bin != null) {
    bin.close();
    }
    } catch (IOException ioe) {
    System.out.println("Error while closing the stream :" + ioe);
    validationMessage = "exceptionClosingStream";
    }

    }
    }

    public static String returnValidationMessage() {
    return validationMessage;
    }
    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Reading file method stops on blank line - It should not

    Your new program does not display the number of bytes read.
    When I run the program it reads 464 bytes. Which is the total number of bytes in the file.
    How many bytes does your program read?
    Look at the input file and see how many bytes are in it. The read method should read up to 1024 bytes.

  8. #8
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default Re: Reading file method stops on blank line - It should not

    Based on below result I got 463

    SQL Compare Command Line V8.50.25.11
    ================================================== ============================
    Copyright Red Gate Software Ltd 1999-2010

    Serial Number: 002-002-113393-F475

    Registering data sources
    Creating mappings
    Comparing
    Applying Command Line Items
    Creating SQL
    Synchronizing databases (from DB1 to DB2)
    Error: Synchronization of 'XX.XX.XX.XX.DB1' and
    'XX.XX.XX.XX.DB2' failed: Invalid object name
    'dbo.fnC_Ext_testsp'.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Reading file method stops on blank line - It should not

    Does Your last post show that the program is reading all of the file now?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Reading file method stops on blank line - It should not

    Print out the file.length() for the file you are reading and compare that to the bytesRead value.

  11. #11
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default Re: Reading file method stops on blank line - It should not

    NO the last post 14# is the file that I am reading

    the execution result is as follow (see below) and it has 173 bytes

    strFC=SQL Compare Command Line V8.50.25.11
    ================================================== ============================
    Copyright Red Gate Software Ltd 1999-2010

    <

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Reading file method stops on blank line - It should not

    If your file has a total of 173 bytes and you read all 173 bytes of the file, can you explain what your problem is?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Reading file method stops on blank line - It should not

    Have you tried: Print out the file.length() for the file you are reading and compare that to the bytesRead value.

Similar Threads

  1. removing the last blank line from txt file
    By heartysnowy in forum New To Java
    Replies: 8
    Last Post: 12-06-2013, 01:09 PM
  2. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 07:56 AM
  3. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  4. Remove a Blank Line from Text file
    By nitinverma in forum CLDC and MIDP
    Replies: 8
    Last Post: 06-15-2010, 06:12 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •