{"id":873,"date":"2015-06-22T20:05:55","date_gmt":"2015-06-22T20:05:55","guid":{"rendered":"http:\/\/www.van-maanen.com\/?p=873"},"modified":"2015-06-22T20:05:55","modified_gmt":"2015-06-22T20:05:55","slug":"reading-and-writing-in-java","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=873","title":{"rendered":"Reading and writing in Java"},"content":{"rendered":"<p>Reading and writing from and to files is not easy in Java. This can already be seen if one simply googles on &#8220;Java Filereader problem&#8221;. This generated 477000 hits. Apparently, reading (and writing) is not trivial. I wrote a small program that is able to read a file and copy its contents to another file. It is tested on both Windows and Linux and it seems to work.<\/p>\n<p>A minor note on using the programme on Windows. To circumvent problems with the backslash, it is preceded by another backslash. Hence the filename is written as &#8220;D:\\\\bla bla&#8221;, where one might expect a &#8220;D:\\bla bla&#8221;. The cause is that a backslash is interpreted as an exception character. (and not as a directory seperator. Hence the necessity to use the double backslash &#8220;\\\\&#8221; instead of a single one. Then the EOF character. In Windows, this EOF character can be enforced by a &#8220;\\r\\n&#8221;, whereas such EOF character is &#8220;\\n&#8221; when it comes down to Linux.<\/p>\n<p>I use a buffered read to hadle buffers. This enables this programme to be used with large input files. <\/p>\n<pre>\nimport java.io.*;\n \npublic class CopyCharacters {\n    public static void main(String[] args) throws IOException {\n \n    \tBufferedReader bufferedReader = null;\n        FileWriter outputStream = null;\n        String filename = \"D:\\\\Users\\\\tmaanen\\\\prive\\\\java\\\\invoer.txt\";\n \n        try {\n            bufferedReader = new BufferedReader(new FileReader(filename));\n            File file = new File(\"D:\\\\Users\\\\tmaanen\\\\prive\\\\java\\\\characteroutput.txt\");\n            file.createNewFile();\n            outputStream = new FileWriter(file, true);    \n            int c;\n            while ((c = bufferedReader.read()) != -1) \n                 {\n                    outputStream.write(c);\n                  }\n            }\n            catch (Exception e)\n            {\n              System.err.format(\"Exception occurred trying to read '%s'.\", filename);\n              e.printStackTrace();\n            }\n         finally {\n            if (bufferedReader != null) \n            {\n            \tbufferedReader.close();\n            }\n            if (outputStream != null) \n            {\n            \toutputStream.write(\"\\r\\n\");\n            \toutputStream.close();\n            }\n                }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reading and writing from and to files is not easy in Java. This can already be seen if one simply googles on &#8220;Java Filereader problem&#8221;. This generated 477000 hits. Apparently, reading (and writing) is not trivial. I wrote a small program that is able to read a file and copy its contents to another file. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":875,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nice-to-know"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=873"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/873\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=873"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}