create temporary file java program

Example: package com.w3schools;   import java.io.File;   public class TempFileTest { public static void main(String args[]){ File tempFile = null; try { tempFile = File.createTempFile("TempFile", ".tmp"); System.out.println("Created Temp File Location : " + tempFile.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } }package com.w3schools; import java.io.File; public class TempFileTest { public static void main(String args[]){ … Read more

Categories IO

set file permissions java program

Example: package com.w3schools;   import java.io.File;   public class FilePermissions { public static void main(String args[]){ File file = new File("D:/Test files/file 3.txt"); System.out.println("Current file permissions:"); System.out.println("Can Execute? "+file.canExecute()); System.out.println("Can Read? "+file.canRead()); System.out.println("Can Write? "+file.canWrite()); file.setExecutable(false); file.setReadable(false); file.setWritable(false); System.out.println("Now file permissions:"); System.out.println("Can Execute? "+file.canExecute()); System.out.println("Can Read? "+file.canRead()); System.out.println("Can Write? "+file.canWrite()); } }package com.w3schools; import java.io.File; … Read more

Categories IO

convert byte array to bufferedreader java

Example: package com.w3schools;   import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.InputStreamReader;   public class ByteArrayToBufferedReader { public static void main(String args[]){ String str = "Test Content"; byte[] content = str.getBytes(); InputStream is = null; BufferedReader bfReader = null; try { is = new ByteArrayInputStream(content); bfReader = new BufferedReader(new InputStreamReader(is)); String temp = null; while((temp … Read more

Categories IO

convert inputstream to bufferedreader java program

Example: package com.w3schools;   import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader;   public class InputStreamToBufferedReader { public static void main(String args[]){ InputStream is = null; BufferedReader bfReader = null; try { is = new FileInputStream("D:/Test files/file 3.txt"); bfReader = new BufferedReader(new InputStreamReader(is)); String temp = null; while((temp = bfReader.readLine()) != null){ System.out.println(temp); } } … Read more

Categories IO

convert byte array to inputstream java program

Example: package com.w3schools;   import java.io.ByteArrayInputStream; import java.io.InputStream;   public class ByteArrayToInputStream { public static void main(String args[]){ String str = "Test Content"; byte[] content = str.getBytes(); int size = content.length; InputStream is = null; byte[] b = new byte[size]; try { is = new ByteArrayInputStream(content); is.read(b); System.out.println("String content: "+new String(b)); } catch (Exception e) … Read more

Categories IO

get file last modified time in java program

The lastModified() method of File class is used to get file last modified time in java. Example: package com.w3schools;   import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date;   public class FileLastModifiedTest { public static void main(String args[]){ File file = new File("TestProp.properties"); Date date = new Date(file.lastModified()); DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); String dateFormatted … Read more

Categories IO

store property file as xml file java

Example: package com.w3schools;   import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Properties;   public class XMLPropertyFileTest { public static void main(String args[]){ OutputStream os = null; Properties prop = new Properties(); prop.setProperty("name", "tutorialspointexamples"); prop.setProperty("domain", "www.tutorialspointexamples.com"); prop.setProperty("email", "[email protected]"); try { os = new FileOutputStream("TestProp.properties"); prop.storeToXML(os, "Dynamic Property File"); System.out.println("XML Property file created successfully."); } catch (Exception e) { … Read more

Categories IO

create and store property file dynamically java

Example: package com.w3schools;   import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Properties;   public class PropertyFileTest { public static void main(String args[]){ OutputStream os = null; Properties prop = new Properties(); prop.setProperty("name", "w3schools"); prop.setProperty("domain", "www.w3schools.com"); prop.setProperty("email", "[email protected]"); try { os = new FileOutputStream("TestProp.properties"); prop.store(os, "Dynamic Property File"); System.out.println("Property file created successfully."); } catch (Exception e) { e.printStackTrace(); … Read more

Categories IO

store and read objects from a file java program

Example: Employee.java package com.w3schools;   import java.io.Serializable;   public class Employee implements Serializable { private static final long serialVersionUID = 1L; private String empName; private int empId; private String salary;   public Employee(String empName, int empId, String salary){ this.empName = empName; this.empId = empId; this.salary = salary; }   public String toString(){ return "Emp Name: … Read more

Categories IO

get file URI reference in java

The toURI() method of File class is used to get the URI reference. Example: package com.w3schools;   import java.io.File;   public class FileURI { public static void main(String args[]){ File file = new File("D:/Test files/file 3.txt"); System.out.println(file.toURI()); } }package com.w3schools; import java.io.File; public class FileURI { public static void main(String args[]){ File file = new … Read more

Categories IO