{"id":19967091,"url":"https://github.com/iambharath-ashok/java-io","last_synced_at":"2026-03-05T11:33:17.431Z","repository":{"id":87193389,"uuid":"168288342","full_name":"iambharath-ashok/Java-IO","owner":"iambharath-ashok","description":"Java IO","archived":false,"fork":false,"pushed_at":"2023-12-15T17:50:25.000Z","size":41,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T17:35:02.603Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iambharath-ashok.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-30T06:03:22.000Z","updated_at":"2019-03-07T10:20:49.000Z","dependencies_parsed_at":"2023-12-15T18:46:41.619Z","dependency_job_id":"12d976dd-3bbe-459d-880e-091c5c410547","html_url":"https://github.com/iambharath-ashok/Java-IO","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/iambharath-ashok/Java-IO","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambharath-ashok%2FJava-IO","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambharath-ashok%2FJava-IO/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambharath-ashok%2FJava-IO/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambharath-ashok%2FJava-IO/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iambharath-ashok","download_url":"https://codeload.github.com/iambharath-ashok/Java-IO/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambharath-ashok%2FJava-IO/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30122184,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T11:11:57.947Z","status":"ssl_error","status_checked_at":"2026-03-05T11:11:29.001Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T02:39:53.954Z","updated_at":"2026-03-05T11:33:17.411Z","avatar_url":"https://github.com/iambharath-ashok.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Java-IO\n\n##\tJava create new file\n\n-\tjava.io.File class can be used to create a new File in Java\n-\tWe can call createNewFile() method to create new file in Java\n\n-\tFile createNewFile() method returns true if new file is created and false if file already exists\n-\tThis method also throws java.io.IOException when it’s not able to create the file\n-\tThe files created is empty and of zero bytes\n\n-\tWhen we create the File object by passing file name:\t\n\t\n\t-\tIt can be with absolute path\n\t-\tOr we can only provide the file name or we can provide relative path\n\n-\tFor non-absolute path, File object tries to locate files in the project root directory\n-\tIf we run the program from command line, for non-absolute path, File object tries to locate files from the current directory\n-\tWhile creating the file path, we should use System property \"file.separator\" to make our program platform independent\n\t\n\t\n\tCode Snippet:\n\t\n\t\t public static void main(String[] args) throws IOException {\n\t\t\tString fileSeparator = System.getProperty(\"file.separator\");\n\t\t\t\n\t\t\t//absolute file name with path\n\t\t\tString absoluteFilePath = fileSeparator+\"Users\"+fileSeparator+\"Iambharath\"+fileSeparator+\"file.txt\";\n\t\t\tFile file = new File(absoluteFilePath);\n\t\t\tif(file.createNewFile()){\n\t\t\t\tSystem.out.println(absoluteFilePath+\" File Created\");\n\t\t\t}else System.out.println(\"File \"+absoluteFilePath+\" already exists\");\n\t\t\t\n\t\t\t//file name only\n\t\t\tfile = new File(\"file.txt\");\n\t\t\tif(file.createNewFile()){\n\t\t\t\tSystem.out.println(\"file.txt File Created in Project root directory\");\n\t\t\t}else System.out.println(\"File file.txt already exists in project root directory\");\n\t\t\t\n\t\t\t//relative path\n\t\t\tString relativePath = \"tmp\"+fileSeparator+\"file.txt\";\n\t\t\tfile = new File(relativePath);\n\t\t\tif(file.createNewFile()){\n\t\t\t\tSystem.out.println(relativePath+\" File Created in Project root directory\");\n\t\t\t}else System.out.println(\"File \"+relativePath+\" already exists in project root directory\");\n\t\t}\n\t\t\n\t\n\tOutput:\n\t\n\t\t/Users/Iambharath/file.txt File Created\n\t\tfile.txt File Created in Project root directory\n\t\tException in thread \"main\" \n\t\tjava.io.IOException: No such file or directory\n\t\t\tat java.io.UnixFileSystem.createFileExclusively(Native Method)\n\t\t\tat java.io.File.createNewFile(File.java:947)\n\t\t\tat com.bharath.files.CreateNewFile.main(CreateNewFile.java:32)\n\t\n\t\n\t\n\t\n\t\n\t\n### FileOutputStream.write(byte[] b)\n\n-\tIf we want to create a new file and at the same time write some data into it, \n-\tWe can use FileOutputStream write method\t\n\t\n\t\n\t\n\tCode Snippet:\n\t\n\t\tString filname = \"Iambharathfile1\";\n\t\tSystem.out.println(Arrays.toString(filname.getBytes()));\n\t\t\n\t\tFileOutputStream fos = new FileOutputStream(\"name.txt\");\n\t\tfos.write(filname.getBytes());\n\t\tfos.flush();\n\t\tfos.close();\n\t\n\t\n\tOutput:\n\t\t\n\t\t[98, 104, 97, 114, 97, 116, 104, 102, 105, 108, 101, 49, 46, 116, 120, 116]\n\n\t\n### Java NIO Files.write()\t\n\t\n-\tWe can use Java NIO Files class to create a new file and write some data into it\t\n\t\n\t\n\tCode Snippet:\n\t\n\t\tString fileData = \"Bahrfath\";\n\t\tFiles.write(Paths.get(\"name.txt\"), fileData.getBytes());\n\t\n------------------------------------------------\n\n## Java Delete File, directory\t\n\t\n-\tJava File delete() method can be used to delete files or empty directory/folder in java.\n-\tJava file delete method returns true if file gets deleted and returns false if file doesn’t exist.\n-\tIf we are trying to delete a directory, it checks java File delete() method check if it’s empty or not.\n\t-\tIf directory is empty, it gets deleted \n\t-\telse delete() method doesn’t do anything and return false. \n-\tSo in this case, we have to recursively delete all the files and then the empty directory.\n-\tAnother way to delete a non-empty directory is by using Files.walkFileTree() method. \n-\tIn this method, we can process all the files one by one, and call delete method on single files.\n\n\n\tCode snippet:\n\t\n\t\tpublic class DeleteFileJava {\n\n\t\t\t/**\n\t\t\t * This class shows how to delete a File in Java\n\t\t\t * @param args\n\t\t\t **/\n\t\t\tpublic static void main(String[] args) {\n\t\t\t\t//absolute file name with path\n\t\t\t\tFile file = new File(\"/Users/Iambharath/file.txt\");\n\t\t\t\tif(file.delete()){\n\t\t\t\t\tSystem.out.println(\"/Users/Iambharath/file.txt File deleted\");\n\t\t\t\t}else System.out.println(\"File /Users/Iambharath/file.txt doesn't exists\");\n\t\t\t\t\n\t\t\t\t//file name only\n\t\t\t\tfile = new File(\"file.txt\");\n\t\t\t\tif(file.delete()){\n\t\t\t\t\tSystem.out.println(\"file.txt File deleted from Project root directory\");\n\t\t\t\t}else System.out.println(\"File file.txt doesn't exists in project root directory\");\n\t\t\t\t\n\t\t\t\t//relative path\n\t\t\t\tfile = new File(\"tmp/file.txt\");\n\t\t\t\tif(file.delete()){\n\t\t\t\t\tSystem.out.println(\"tmp/file.txt File deleted from Project root directory\");\n\t\t\t\t}else System.out.println(\"File tmp/file.txt doesn't exists in project root directory\");\n\t\t\t\t\n\t\t\t\t//delete empty directory\n\t\t\t\tfile = new File(\"tmp\");\n\t\t\t\tif(file.delete()){\n\t\t\t\t\tSystem.out.println(\"tmp directory deleted from Project root directory\");\n\t\t\t\t}else System.out.println(\"tmp directory doesn't exists or not empty in project root directory\");\n\t\t\t\t\n\t\t\t\t//try to delete directory with files\n\t\t\t\tfile = new File(\"/Users/Iambharath/project\");\n\t\t\t\tif(file.delete()){\n\t\t\t\t\tSystem.out.println(\"/Users/Iambharath/project directory deleted from Project root directory\");\n\t\t\t\t}else System.out.println(\"/Users/Iambharath/project directory doesn't exists or not empty\");\n\t\t\t}\n\n\t\t}\n\t\n### Java delete directory\n\n-\tDeleting Non-Empty directory in the Java 7\n\n\tCode Snippet:\n\t\n\t\tpublic class JavaDeleteDirectory {\n\n\t\t\tpublic static void main(String[] args) {\n\t\t\t\tFile dir = new File(\"/Users/Iambharath/log\");\n\t\t\t\t\n\t\t\t\tif(dir.isDirectory() == false) {\n\t\t\t\t\tSystem.out.println(\"Not a directory. Do nothing\");\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tFile[] listFiles = dir.listFiles();\n\t\t\t\tfor(File file : listFiles){\n\t\t\t\t\tSystem.out.println(\"Deleting \"+file.getName());\n\t\t\t\t\tfile.delete();\n\t\t\t\t}\n\t\t\t\t//now directory is empty, so we can delete it\n\t\t\t\tSystem.out.println(\"Deleting Directory. Success = \"+dir.delete());\n\t\t\t\t\n\t\t\t}\n\n\t\t}\n\t\n\t\n###\tJava delete directory recursively\n\n-\tEarlier we had to write recursion based code to delete a directory with nested directories\n-\tBut with Java 7, we can do this using Files class\n-\tAnother way to delete a non-empty directory is by using Files.walkFileTree() method. \n-\tIn this method, we can process all the files one by one, and call delete method on single files.\n\tCode Snippet:\n\t\n\t\tpublic class JavaDeleteDirectoryRecursively {\n\n\t\t\tpublic static void main(String[] args) throws IOException {\n\t\t\t\t\n\t\t\t\tPath directory = Paths.get(\"/Users/Iambharath/log\");\n\t\t\t\tFiles.walkFileTree(directory, new SimpleFileVisitor\u003cPath\u003e() {\n\t\t\t\t   @Override\n\t\t\t\t   public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {\n\t\t\t\t\t   Files.delete(file); // this will work because it's always a File\n\t\t\t\t\t   return FileVisitResult.CONTINUE;\n\t\t\t\t   }\n\n\t\t\t\t   @Override\n\t\t\t\t   public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {\n\t\t\t\t\t   Files.delete(dir); //this will work because Files in the directory are already deleted\n\t\t\t\t\t   return FileVisitResult.CONTINUE;\n\t\t\t\t   }\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\t\t\t\n---------------------------------------------\n\n## \tJava File separator, separatorChar, pathSeparator, pathSeparatorChar\n\n\t\n-\tjava.io.File class contains four static separator variables\n\n\t-\tFile.separator: Platform dependent default name-separator character as String. \n\t-\tFor windows, it’s ‘\\’ and for unix it’s ‘/’.\n\t-\tFile.separatorChar: Same as separator but it’s char.\n\t-\tFile.pathSeparator: Platform dependent variable for path-separator.\n\t-\tFor example PATH or CLASSPATH variable list of paths separated by ‘:’ in Unix systems and ‘;’ in Windows system.\n\t-\tFile.pathSeparatorChar: Same as pathSeparator but it’s char.\t\n\t\n\t\n\t\n\tCode snippet:\n\t\n\t\tpublic static void main(String[] args) {\n\t\t\tSystem.out.println(\"File.separator = \"+File.separator);\n\t\t\tSystem.out.println(\"File.separatorChar = \"+File.separatorChar);\n\t\t\tSystem.out.println(\"File.pathSeparator = \"+File.pathSeparator);\n\t\t\tSystem.out.println(\"File.pathSeparatorChar = \"+File.pathSeparatorChar);\n\t\t\t\n\t\t}\n\t\n\t\n\tOutput linux:\n\t\n\t\tFile.separator = /\n\t\tFile.separatorChar = /\n\t\tFile.pathSeparator = :\n\t\tFile.pathSeparatorChar = :\n\t\t\n\tOutput widows:\n\n\t\tFile.separator = \\\n\t\tFile.separatorChar = \\\n\t\tFile.pathSeparator = ;\n\t\tFile.pathSeparatorChar = ;\n\t\n-----------------------------------------------------------------\n\n## How to Delete a Directory/Folder in Java using Recursion\n\nCode Snippet:\n\n\t\t\n\t\tpublic class DeleteFolderRecursively {\n\n\t\t\tpublic static void main(String[] args) {\n\t\t\t\tString folder = \"/Users/Iambharath/tmp\";\n\t\t\t\t//delete folder recursively\n\t\t\t\trecursiveDelete(new File(folder));\n\t\t\t}\n\t\t\t\n\t\t\tpublic static void recursiveDelete(File file) {\n\t\t\t\t//to end the recursive loop\n\t\t\t\tif (!file.exists())\n\t\t\t\t\treturn;\n\t\t\t\t\n\t\t\t\t//if directory, go inside and call recursively\n\t\t\t\tif (file.isDirectory()) {\n\t\t\t\t\tfor (File f : file.listFiles()) {\n\t\t\t\t\t\t//call recursively\n\t\t\t\t\t\trecursiveDelete(f);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t//call delete to delete files and empty directory\n\t\t\t\tfile.delete();\n\t\t\t\tSystem.out.println(\"Deleted file/folder: \"+file.getAbsolutePath());\n\t\t\t}\n\t\t}\n-----------------------------------------------------------------\n## Java Rename File – Jave Move File\n\t\n\t\n-\tWe can use File.renameTo(File dest) method for java rename file and java move file operations.\n-\tFile renameTo method returns true if file rename is successful, else it returns false.\n-\tSome of the rename operation are platform dependent. \n\t-\tFor example it might fail if we move a file from one filesystem to another or if a file already exists with the same name at destination directory\n-\tIn Mac OS, if destination file already exists renameTo override the existing file with source file\n-\tWe should always check the renameTo return value to make sure rename file is successful \n-\tBecause it’s platform dependent and it doesn’t throw IO exception if rename fails\n\n\n\tpublic static void main(String[] args) {\n        //absolute path rename file\n        File file = new File(\"/Users/Iambharath/java.txt\");\n        File newFile = new File(\"/Users/Iambharath/java1.txt\");\n        if(file.renameTo(newFile)){\n            System.out.println(\"File rename success\");;\n        }else{\n            System.out.println(\"File rename failed\");\n        }\n\t\t\n\t\t//java move file from one directory to another\n        file = new File(\"/Users/Iambharath/DB.properties\");\n        newFile = new File(\"DB_Move.properties\");\n        if(file.renameTo(newFile)){\n            System.out.println(\"File move success\");;\n        }else{\n            System.out.println(\"File move failed\");\n        }\n\t}\n\t\n\t\n-----------------------------------------------------------------\t\n## Java get file size\n\t\n### Java get file size using File class\n\n\n-\tJava File length() method returns the file size in bytes.\n-\tWill only work for file and not for directory. \n-\tSo before calling this method to get file size in java\n-\tMake sure file exists and it’s not a directory.\n\n\n\tCode Snippet:\n\t\n\t\tpublic class JavaGetFileSize {\n\t\t\tstatic final String FILE_NAME = \"/Users/Iambharath/Downloads/file.pdf\";\n\t\t\tpublic static void main(String[] args) {\n\t\t\t\tFile file = new File(FILE_NAME);\n\t\t\t\tif (!file.exists() || !file.isFile()) return;\n\n\t\t\t\tSystem.out.println(getFileSizeBytes(file));\n\t\t\t\tSystem.out.println(getFileSizeKiloBytes(file));\n\t\t\t\tSystem.out.println(getFileSizeMegaBytes(file));\n\t\t\t}\n\n\t\t\tprivate static String getFileSizeMegaBytes(File file) {\n\t\t\t\treturn (double) file.length() / (1024 (Mul) 1024) + \" mb\";\n\t\t\t}\n\t\t\t\n\t\t\tprivate static String getFileSizeKiloBytes(File file) {\n\t\t\t\treturn (double) file.length() / 1024 + \"  kb\";\n\t\t\t}\n\n\t\t\tprivate static String getFileSizeBytes(File file) {\n\t\t\t\treturn file.length() + \" bytes\";\n\t\t\t}\n\t\t}\n\t\t\n\tOutput;\n\t\n\t\t34353544343 bytes\n\t\t343.345323 kb\n\t\t3.35343 mb\n---------------------------------------------\t\t\n\t\t\n## How to Get File Extension in Java\t\t\n\n\tCode Snippet:\n\t\t\n\t\tprivate static String getFileExtension(File file) {\n\t\t\tString fileName = file.getName();\n\t\t\tif(fileName.lastIndexOf(\".\") != -1 \u0026\u0026 fileName.lastIndexOf(\".\") != 0)\n\t\t\treturn fileName.substring(fileName.lastIndexOf(\".\")+1);\n\t\t\telse return \"\";\n\t\t}\n\t\t\n---------------------------------------------\t\t\n## How to check if File exists in Java\n\n\n\tCode Snippet:\n\t\t\n\t\tpublic class FileExists {\n\n\t\t\tpublic static void main(String[] args) {\n\t\t\t\tFile file = new File(\"/Users/Iambharath/source.txt\");\n\t\t\t\tFile notExist = new File(\"xyz.txt\");\n\t\t\t\t\n\t\t\t\ttry {\n\t\t\t\t\tSystem.out.println(file.getCanonicalPath() + \" exists? \"+file.exists());\n\t\t\t\t\tSystem.out.println(notExist.getCanonicalPath() + \" exists? \"+notExist.exists());\n\t\t\t\t} catch (IOException e) {\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\n\t\t}\n\t\t\n\tOutput:\n\t\t\n\t\t/Users/Iambharath/source.txt exists? true\n\t\t/Users/Iambharath/JavaPrograms/xyz.txt exists? false\n---------------------------------------------\t\t\n## How to check File is Directory or File in java\n\n-\tjava.io.File class contains two methods using which we can find out if the file is a directory or a regular file in java.\n-\tisFile(): This method returns true if file exists and is a regular file\n\t-\tNote that if file doesn’t exist then it returns false.\n\n-\tisDirectory(): This method returns true if file is actually a directory\n\t-\tIf path doesn’t exist then it returns false.\n\t\n-\tWhile checking if a file is directory or regular file, we should first check if file exists or not. \n-\tIf it exists then only we should check if it’s a directory or file.\n\t\n\tCode Snippet:\n\t\n\t\tpublic class CheckDirectoryOrFile {\n\n\t\t\tpublic static void main(String[] args) {\n\t\t\t\tFile file = new File(\"/Users/Iambharath/source.txt\");\n\t\t\t\tFile dir = new File(\"/Users/Iambharath\");\n\t\t\t\tFile notExists = new File(\"/Users/Iambharath/notafile\");\n\t\t\t\t\n\t\t\t\tSystem.out.println(\"/Users/Iambharath/source.txt is file?\"+file.isFile());\n\t\t\t\tSystem.out.println(\"/Users/Iambharath/source.txt is directory?\"+file.isDirectory());\n\t\t\t\t\n\t\t\t\tSystem.out.println(\"/Users/Iambharath is file?\"+dir.isFile());\n\t\t\t\tSystem.out.println(\"/Users/Iambharath is directory?\"+dir.isDirectory());\n\t\t\t\t\n\t\t\t\tSystem.out.println(\"/Users/Iambharath/notafile is file?\"+notExists.isFile());\n\t\t\t\tSystem.out.println(\"/Users/Iambharath/notafile is directory?\"+notExists.isDirectory());\n\t\t\t}\n\n\t\t}\n\t\n\t\n\tOutput:\n\t\t\n\t\t/Users/Iambharath/source.txt is file?true\n\t\t/Users/Iambharath/source.txt is directory?false\n\t\t/Users/Iambharath is file?false\n\t\t/Users/Iambharath is directory?true\n\t\t/Users/Iambharath/notafile is file?false\n\t\t/Users/Iambharath/notafile is directory?false\n\t\t\t\n---------------------------------------------\t\t\t\n## How to get File last modified date in Java\n\n-\tjava.io.File class lastModified() returns last modified date in long\n-\twe can construct date object in human readable format with this time.\n-\tIf file doesn’t exists, lastModified() returns 0L, \n\n\n\tCode Snippet:\n\t\n\t\tpublic class FileDate {\n\n\t\t\tpublic static void main(String[] args) {\n\t\t\t\tFile file = new File(\"employee.xml\");\n\t\t\t\t\n\t\t\t\tlong timestamp = file.lastModified();\n\t\t\t\tSystem.out.println(\"employee.xml last modified date = \"+new Date(timestamp));\n\t\t\t}\n\n\t\t}\n\t\t\n\tOutput:\n\n\t\temployee.xml last modified date = Fri Dec 07 14:19:10 PST 2012\n\t\t\n-\tAfter deleting employee.xml then the output is:\t\t\n\t\n\t\n\tOutput:\n\t\t\n\t\temployee.xml last modified date = Wed Dec 31 16:00:00 PST 1969\n---------------------------------------------\t\n## Java File Path, Absolute Path and Canonical Path\n\n### Getting file paths in java \n\n-\tgetPath()\n-\tgetAbsolutePath()\n-\tgetCanonicalPath()\n\n-\tThese methods returns String value of file Path based on how its created\n-\tThey just work on the pathname of the file used while creating the File object\n\n\tCode Snippet:\n\t\n\t\tpublic static void main(String[] args) throws IOException, URISyntaxException {\n\t\t\tFile file = new File(\"/Users/Iambharath/test.txt\");\n\t\t\tprintPaths(file);\n\t\t\t// relative path\n\t\t\tfile = new File(\"test.xsd\");\n\t\t\tprintPaths(file);\n\t\t\t// complex relative paths\n\t\t\tfile = new File(\"/Users/Iambharath/../Iambharath/test.txt\");\n\t\t\tprintPaths(file);\n\t\t\t// URI paths\n\t\t\tfile = new File(new URI(\"file:///Users/Iambharath/test.txt\"));\n\t\t\tprintPaths(file);\n\t\t}\n\t\t\n\t\tprivate static void printPaths(File file) throws IOException {\n\t\t\tSystem.out.println(\"Absolute Path: \" + file.getAbsolutePath());\n\t\t\tSystem.out.println(\"Canonical Path: \" + file.getCanonicalPath());\n\t\t\tSystem.out.println(\"Path: \" + file.getPath());\n\t\t}\n\t\n-\tUsing canonical path is best suitable to avoid any issues because of relative paths\n-\tJava file path methods doesn’t check if file exists or not. \n-\tThey just work on the pathname of the file used while creating the File object\n\t\n---------------------------------------------\t\n## Java File Permissions\n\n-\tJava File class contains methods to check file permissions for application user\n-\tThey also have some methods to set file permissions for the user and everybody else\n-\tThese File set permission methods return false if they are not able to set the file permissions\n-\tThis can happen due to user privilege\n\t\n\t-\tFor example, if I change the owner of my sample file to root\n\t-\tThen all the set file permission method calls return false\n\n\t\n\tCode Snippet:\n\t\n\t\tpublic class JavaFilePermissions {\n\n\t\t\tpublic static void main(String[] args) {\n\t\t\t\tFile file = new File(\"/Users/Iambharath/run.sh\");\n\t\t\t\t//check file permissions for application user\n\t\t\t\tSystem.out.println(\"File is readable? \"+file.canRead());\n\t\t\t\tSystem.out.println(\"File is writable? \"+file.canWrite());\n\t\t\t\tSystem.out.println(\"File is executable? \"+file.canExecute());\n\t\t\t\t//change file permissions for application user only\n\t\t\t\tfile.setReadable(false);\n\t\t\t\tfile.setWritable(false);\n\t\t\t\tfile.setExecutable(false);\n\t\t\t\t//change file permissions for other users\n\t\t\t\tfile.setReadable(true, false);\n\t\t\t\tfile.setWritable(true, false);\n\t\t\t\tfile.setExecutable(true, true);\n\t\t\t}\n\n\t\t}\n\t\n--------------------------------------------------\n\n## How to set File Permissions in Java Easily using Java 7 PosixFilePermission\n\t\n-\tJava File class has the ability to set the file permissions but it’s not versatile\n-\tThe biggest drawback is that:\n\t-\twe can divide file permissions into two set of users – owner and everybody else only\n- \tWe can’t set different file permissions for group and other users\n\n-\tJava 7 has introduced PosixFilePermission Enum and java.nio.file.Files \n-\tjava.nio.file.Files  includes a method setPosixFilePermissions(Path path, Set\u003cPosixFilePermission\u003e perms) that can be used to set file permissions easily\n\n\n\tCode Snippet:\n\t\n\t\tpublic class FilePermissions {\n\n\t\t\t/**\n\t\t\t * File Permissions Java Example using File and PosixFilePermission\n\t\t\t * @param args\n\t\t\t * @throws IOException \n\t\t\t ***/\n\t\t\tpublic static void main(String[] args) throws IOException {\n\t\t\t\tFile file = new File(\"/Users/Iambharath/temp.txt\");\n\t\t\t\t\n\t\t\t\t//set application user permissions to 455\n\t\t\t\tfile.setExecutable(false);\n\t\t\t\tfile.setReadable(false);\n\t\t\t\tfile.setWritable(true);\n\t\t\t\t\n\t\t\t\t//change permission to 777 for all the users\n\t\t\t\t//no option for group and others\n\t\t\t\tfile.setExecutable(true, false);\n\t\t\t\tfile.setReadable(true, false);\n\t\t\t\tfile.setWritable(true, false);\n\t\t\t\t\n\t\t\t\t//using PosixFilePermission to set file permissions 777\n\t\t\t\tSet\u003cPosixFilePermission\u003e perms = new HashSet\u003cPosixFilePermission\u003e();\n\t\t\t\t//add owners permission\n\t\t\t\tperms.add(PosixFilePermission.OWNER_READ);\n\t\t\t\tperms.add(PosixFilePermission.OWNER_WRITE);\n\t\t\t\tperms.add(PosixFilePermission.OWNER_EXECUTE);\n\t\t\t\t//add group permissions\n\t\t\t\tperms.add(PosixFilePermission.GROUP_READ);\n\t\t\t\tperms.add(PosixFilePermission.GROUP_WRITE);\n\t\t\t\tperms.add(PosixFilePermission.GROUP_EXECUTE);\n\t\t\t\t//add others permissions\n\t\t\t\tperms.add(PosixFilePermission.OTHERS_READ);\n\t\t\t\tperms.add(PosixFilePermission.OTHERS_WRITE);\n\t\t\t\tperms.add(PosixFilePermission.OTHERS_EXECUTE);\n\t\t\t\t\n\t\t\t\tFiles.setPosixFilePermissions(Paths.get(\"/Users/Iambharath/run.sh\"), perms);\n\t\t\t}\n\t}\n--------------------------------------------------\n\n## Java File  Copy \n\n- \tNo Direct Methods on java.io.File class\n-\t4 Other ways are \n\n\t-\tUsing Input Stream and Output Stream\n\t-\tJava 4 NIO FileChannels\n\t-\tApache Common FileUtils\n\t-\tJava 7 Files.copy\n\t\n\t\n### Using Input and Output Stream\n\n\tCode Snippet:\n\t\n\t\tprivate static void copyFileUsingStream(File source, File dest) throws IOException {\n\t\t\tInputStream is = null;\n\t\t\tOutputStream os = null;\n\t\t\ttry {\n\t\t\t\tis = new FileInputStream(source);\n\t\t\t\tos = new FileOutputStream(dest);\n\t\t\t\tbyte[] buffer = new byte[1024];\n\t\t\t\tint length;\n\t\t\t\twhile ((length = is.read(buffer)) \u003e 0) {\n\t\t\t\t\tos.write(buffer, 0, length);\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tis.close();\n\t\t\t\tos.close();\n\t\t\t}\n\t\t}\n### Using NIO FileChannels\n\n-\tJava NIO classes were introduced in Java 1.4 and FileChannel can be used to copy file in java\n-\tAccording to transferFrom() method javadoc, FileChannel is supposed to be faster than using Streams for java copy files\n\n\t\n\tCode snippet:\n\t\n\t\tprivate static void copyFileUsingChannel(File source, File dest) throws IOException {\n\t\t\tFileChannel sourceChannel = null;\n\t\t\tFileChannel destChannel = null;\n\t\t\ttry {\n\t\t\t\tsourceChannel = new FileInputStream(source).getChannel();\n\t\t\t\tdestChannel = new FileOutputStream(dest).getChannel();\n\t\t\t\tdestChannel.transferFrom(sourceChannel, 0, sourceChannel.size());\n\t\t\t   }finally{\n\t\t\t\t   sourceChannel.close();\n\t\t\t\t   destChannel.close();\n\t\t   }\n\t\t}\n\n### Apache Common FileUtils\n\n\t\n\tCode Snippet:\n\t\n\t\tprivate static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {\n\t\t\tFileUtils.copyFile(source, dest);\n\t\t}\n\t\n### Java 7 Files.copy()\n\n\t\t\n\tCode Snippet:\n\t\n\t\tprivate static void copyFileUsingJava7Files(File source, File dest) throws IOException {\n\t\t\tFiles.copy(source.toPath(), dest.toPath());\n\t\t}\n-------------------------------------\t\n\ncreateNewFile()\ndelete()\nisDirectory()\nlists()\nexists()\nrenameTo() - move and rename both\nisFile()\nlength()\nmkdirs()\n\ngetCanonicalPath()\ngetPath()\ngetAbsolutePath()\n\n// Permission\ncanRead()\ncanExecute()\ncanWrite()\n\nsetReadable(true)\nsetWriteable(false)\nsetExecutable(false)\n\n\nFiles.setPosixFilePermissions(Path.get(), Set\u003cPosixFilePermission\u003e)\n\ninputStream to OutStream\ndestChannel.transferFrom(srcChannel,0, srcChannel.size())\nFileUtils.copyFile(File, File)\nFiles.copy(File, File)\n\nreadAllByets(Path)\nreadAllLines(Path)\nlines(Path, StCharSets.UTF_16)\n\nFileReader\n\t\n\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiambharath-ashok%2Fjava-io","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiambharath-ashok%2Fjava-io","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiambharath-ashok%2Fjava-io/lists"}