{"id":1470,"date":"2022-08-30T15:16:48","date_gmt":"2022-08-30T15:16:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/19\/java-buffered-readed-readline-gives-null-as-end-of-file-but-no-way-to-use-that-null-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:48","modified_gmt":"2022-08-30T15:16:48","slug":"java-buffered-readed-readline-gives-null-as-end-of-file-but-no-way-to-use-that-null-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/java-buffered-readed-readline-gives-null-as-end-of-file-but-no-way-to-use-that-null-collection-of-common-programming-errors\/","title":{"rendered":"java &#8211; buffered readed readline() gives null as end of file but no way to use that null-Collection of common programming errors"},"content":{"rendered":"<p>Is there a way to check whether a file was correctly written, I mean if there is an EOF at the end? I&#8217;m asking that because I have a program that takes some file, merge them in a very big file and then use it to get statistics from it. The point is that the second part never ends because it doesn&#8217;t recognize the end of file.<\/p>\n<p>The relevant parts of the code are the following: (please do not ask for the whole code as I cannot post for important reasons)<\/p>\n<pre><code>    FileWriter file=null;\n    PrintWriter pw = null;\n    String pathToRead=null;\n    InputStreamReader isr = null;\n    BufferedReader br = null ;\n    FileInputStream fis = null ;\n    TestJFileChooser d=new TestJFileChooser();\n    int c=1;\n            String line=null;\n\n    ....\n\n    \/\/here i select the files\n\n    selectedFile=new File(pathToRead);\n\n    \/\/here I get one buffer reader for each file got with listFiles()\n\n    for(File file_sel:app){\n        if (file_sel.getName().startsWith(\"gtou\")){\n            System.out.println(file_sel.getName());\n            fis = null;\n            try {\n                fis = new FileInputStream(file_sel);\n            } catch (FileNotFoundException e) {\n                e.printStackTrace();\n            }\n            isr=new InputStreamReader(fis);\n            br=new BufferedReader(isr);\n            map.put(i, br);\n            num_file++;\n            i++; \n        }\n    }\n\n    \/\/then I select the output file and open a print writer for it\n\n    fileToWrite=new File(pathToRead);\n        try {\n            file = new FileWriter(fileToWrite);\n            pw= new PrintWriter(file);\n        } catch (IOException e1) {\n            e1.printStackTrace();\n        }\n        \/\/merging part\n        ....\n        line=br.readLine();\n        while(line!=null){\n        System.out.println(\"line is:\"+line);\n        ....\n        line=br.readLine();\n        }   \n\n\n       \/\/end of merging ....\n\n        pw.flush();\n        pw.close();\n\n        try {\n            if (file!=null) file.close();\n            fis.close();\n            isr.close();\n            br.close();\n            for(int fi=0;fi 0) {\n                    os.write(buffer, 0, readBytes);\n                }\n                fos.flush();\n                is.close();\n            } catch (FileNotFoundException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>If you read files with different encodings, you will have to modify at least the reading of course.<\/p>\n<p>If it doesn&#8217;t work, I&#8217;d suggest you build a &#8220;summarized&#8221; and runable sample program.<\/p>\n<li>\n<p>The core of your question is this code:<\/p>\n<pre><code>    BufferedReader br = ...\n    String line = br.readLine();\n    while (line != null) {\n        System.out.println(\"line is:\" + line);\n        ...\n        line = br.readLine();\n    }\n<\/code><\/pre>\n<p>You say that this repeatedly outputs this:<\/p>\n<pre><code>line is: null ;\nline is: null ;\n<\/code><\/pre>\n<p>(Notice the &#8221; ;&#8221; on the end!!!)<\/p>\n<p>The <em>only way that can happen<\/em> is if the file you are reading contains at least one line that look like this:<\/p>\n<pre><code>null ;\n<\/code><\/pre>\n<p>Indeed, unless the &#8220;&#8230;&#8221; code includes a <code>continue<\/code> statement, there must must be lots of those lines in the input file.<\/p>\n<blockquote>\n<p>Is there a way to check whether a file was correctly written?<\/p>\n<\/blockquote>\n<p>Yea. Look at it using a text editor and\/or check its file size.<\/p>\n<blockquote>\n<p>I mean if there is an EOF at the end?<\/p>\n<\/blockquote>\n<p>In modern file systems, EOF is a position not a marker. Specifically it is the position after the last byte of the file. So it is logically impossible for a file to not have an EOF. (You&#8217;d have to have a file that is <em>infinite<\/em> in length for there to be no EOF.)<\/p>\n<\/li>\n<p id=\"rop\"><small>Originally posted 2013-11-19 13:18:57. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Is there a way to check whether a file was correctly written, I mean if there is an EOF at the end? I&#8217;m asking that because I have a program that takes some file, merge them in a very big file and then use it to get statistics from it. The point is that the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1470","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1470"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1470\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}