Posts

Showing posts with the label IO Package

Serialization In Java

Serialization In Java -  In java, serialization is the process for writing the state of object in sequence  of bytes stream. Serialization process for an object also called marshalling.  How to serialize an object?      Serializble is a marker interface, Through the implimantaion of Serializble interface  any object can be serialize.  There is no methods in Serializble interface, but Serializble interface tell to JVM  that Object can be written in byte stream.  If Serialized object have any transient variable . It means transient variable will not  written in to byte stream.   Example of Serialization -  class A implements Serializable{         A(String personName, Integer age){         this.personName=personName;         this.age=age;     }     private String personName = nul...

Directory Size in java code example

How to get Directory Size in java code example? public static long getDirectorySize(String directoryPath)     {         dirSize = 0L;         dirSize = calDirectorySize(directoryPath);         return dirSize;     }  private static long calDirectorySize(String directoryPath)     {         File fd = new File(directoryPath);         if(!fd.isFile())         {             String list[] = fd.list();             for(int i = 0; i < list.length; i++)             {                 Stri...

How to calculate number of files in a directory in java code examples

 private static int calNoOfFiles(String directoryPath)     {         String fileDelimiter= File.separator;         File fd = new File(directoryPath);         if(!fd.isFile())         {             String list[] = fd.list();             for(int i = 0; i < list.length; i++)             {                 String tempDirPath = directoryPath + fileDelimiter + list[i];                 debug(tempDirPath);                 File ftd = new File(...

How to get file extension in java code example

Get file extension in java code example public static String getFileExtension(String filePath)     {         File f = new File(filePath);         String name = f.getName();         int k = name.lastIndexOf(".");         String ext = null;         if(k != -1)             ext = name.substring(k + 1, name.length());         return ext;     }

How to copy file using FileReader in java

public static void copyFile(String sourcePath, String destinationPath){                                   File inputFile = new File(sourcePath);              File outputFile = new File(destinationPath);               try {                 FileReader in = new FileReader(inputFile);                 FileWriter out = new FileWriter(outputFile);                   int c;                   while ((c = in.read()) != -1)                   out...

How to copy file using FromFile in Struts java

public String copyFile(FormFile formFile, String destinationFolder,             String outputFileName) throws IOException {         byte[] data = new byte[1000];                 InputStream input = formFile.getInputStream();         String filename = formFile.getFileName();         new File(destinationFolder).mkdirs();         File file = new File(destinationFolder, outputFileName);         FileOutputStream output = new FileOutputStream(file);         try {             try {                 while (true) {                  ...

How to read data byte by byte in java code examples

public  void readDataByteByByte( byte[] data,int contentLength,InputStream raw){         InputStream in = new BufferedInputStream(raw);         int offset = 0;         try{         int bytesRead = 0;         while (offset < contentLength) {           bytesRead = in.read(data, offset, data.length - offset);           if (bytesRead == -1)             break;           offset += bytesRead;         }         }catch (Exception e) {             e.printStackTrace();         }         finally{ ...

How to calculate no of files in a directory in java code examples

private static int calNoOfFiles(String directoryPath)     {         File fd = new File(directoryPath);         if(!fd.isFile())         {             String list[] = fd.list();             for(int i = 0; i < list.length; i++)             {                 String tempDirPath = directoryPath + fileDelimiter + list[i];                 debug(tempDirPath);                 File ftd = new File(tempDirPath);             ...

How to get file exist or not in java code example

This method is used to check that file is exist or not on your given path.  public static boolean getFileExist(String filePath)     {         File f = new File(filePath);         return f.exists();     }

How to get file size in java code, example?

 How to get file size in java code, example Now we are writing a very simple code for find the file size in java. Copy and paste this code in your java class. public static long getFileSize(String filePath)     {         File f = new File(filePath);         return f.length();     }

How to delete file in java code example?

 Delete file in java code example Here we are describing how to delete file in java code example. Very simple code method is there. public boolean setDeleteFile(String filePath)     {         File f = new File(filePath);         return f.delete();     }

How to read a file in java - code example

Java code for read a text file in java   File file =  new  File ( "C:\\filenamehere.txt" ) ;      FileInputStream fileInputStream =  null ;      BufferedInputStream bufferedInputStream =  null ;      DataInputStream dataInputStream =  null ;      try  {       fileInputStream =  new  FileInputStream ( file ) ;             bufferedInputStream =  new  BufferedInputStream ( fileInputStream ) ;       dataInputStream =  new  DataInputStream ( bufferedInputStream ) ;              while  ( dataInputStream .available ()  !=  0 ) {               System.out.println ( dataInputStream .readLine ...

how to converts a byte size in a kbytes or Mbytes size ?

Convert byte to kbyte or Mbytes static String convertFileSize(long size) {         int divisor = 1;         String unit = "bytes";         if (size >= 1024 * 1024) {             divisor = 1024 * 1024;             unit = "MB";         }         else if (size >= 1024) {             divisor = 1024;             unit = "KB";         }         if (divisor == 1) return size / divisor + " " + unit;         String aftercomma = "" + 100 * (size % divisor) / divisor;         if (aftercomma.length() == 1) aftercomma = "0" + aftercomma; ...

Recent Post

Recent Posts Widget