Showing posts with label IO. Show all posts
Showing posts with label IO. Show all posts

How to read file contents and dispay into String in Java

How to read file contents and dispay into String in Java
In this post we are going to learn about how to read file contents and display the contents into string.
The following example demonstrates to read file content from source.txt file .
File Name : source.txt
It is a popular ORM (Object Relational Mapping) framework.
ORM stands for Object Relational mapping, It fulfills gap between RDBM’s and OOP’s.
File Name : ReadFileInString.java

OUTPUT :
File Content : 
It is a popular ORM (Object Relational Mapping) framework.
 ORM stands for Object Relational mapping, It fulfills gap between RDBM’s and OOP’s.

Example 2 
File Content : 
It is a popular ORM (Object Relational Mapping) framework.
File Content : 
 ORM stands for Object Relational mapping, It fulfills gap between RDBM’s and OOP’s.

How to copy file content using Java

How to copy file content using Java

In this article we are going to discuss about how to copy file content to another file

Filename:sourceFile.txt
site1 = www.google.com
site2 = www.becbe.com
site3 = www.linux.com
site4 = www.motorola.com
site5 = www.apple.com
Filename: targetFile.txt
sample file

Filename: FileCopyTest .java

package com.becbe.java.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
 * This class shows demo to copy file content
 * @author bebce
 *
 */
public class FileCopyTest {

 public static String sourceFilePath="d:\\sourceFile.txt";
 public static String targetPath="d:\\targetFile.txt";
 /**
  * This method is used to copy content from one file other
  * @param sourceFile
  * @param targetFile
  * @return
  */
 public static boolean copy(File sourceFile, File targetFile)
 {
  boolean success = false;
  //File availability verification
     if(!sourceFile.exists() || !targetFile.exists())
     {
      return false;
     }
  try {
   //Reading file content from source File
   FileInputStream sourceFileStream = new FileInputStream(sourceFile);
   FileChannel sourceFileChannel = sourceFileStream.getChannel();
   //writing file content 
   FileOutputStream targetFileStream = new FileOutputStream(targetFile);
   FileChannel targetFileChannel=targetFileStream.getChannel();
   //copy file content
   sourceFileChannel.transferTo(0, sourceFile.length(), targetFileChannel);
   success = true;
  }
  catch (Exception e) {
   // TODO: handle exception
   
  }
    
  return success;
 }
 
 /**
  * @param args
  * @throws IOException 
  */
 public static void main(String[] args) {
  FileCopyTest cpTest=new FileCopyTest(); 
     File sourceFile = new File(cpTest.sourceFilePath);
     File targetFile = new File(cpTest.targetPath);
      if(cpTest.copy(sourceFile, targetFile))
     {
      System.out.println("File copied successfully ");
     }else{
      System.out.println("File copy operation failed ");
     }
     
  
 }

}


Output:
File copied successfully 

How to read data from properties file and store in to Map

How to read data from properties file and store in to Map

In this article we are going to discuss about how to read data from properties and store in to map.

Filename: sample.properties
## Web Site information
site1 = www.google.com
site2 = www.becbe.com
site3 = www.linux.com
site4 = www.motorola.com
site5 = www.apple.com

Filename: ReadPropertiesFileToMap.java
package com.becbe.sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * 
 */

/**
 * @author tponnam
 *
 */
public class ReadPropertiesFileToMap {
 private String filePath="d:\\sample.properties";
 
 /**
  * This method validate the file existence 
  * @return
  */
 private File getPropertiesFile()
 {
  File file = new File(filePath);
  if( !file .exists()){
   try {
    file.createNewFile();
    
   } catch (IOException e) {
    
   }
  }
  return file;
 }
 
 /**
  * This Method reads data from properties file and store into Map
  * @return
  */
 public HashMap getSBX5kNodeToProxyPortMap(){
     HashMap map = new HashMap();
  Properties properties = new Properties();
  try {
   properties.load(new FileInputStream(getPropertiesFile()));
     for (String key : properties.stringPropertyNames()) {
         String value = properties.getProperty(key);
         map.put(key, value);
       }

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return map;
  
 }
 public static void main(String[] args) {
  
  ReadPropertiesFileToMap fileToMap = new ReadPropertiesFileToMap();
  HashMap map = fileToMap.getSBX5kNodeToProxyPortMap();
  Iterator iter = map.entrySet().iterator();
   
  while (iter.hasNext()) {
   Map.Entry mEntry = (Map.Entry) iter.next();
   System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
  }
 }
}
Output:
site5 : www.apple.com
site4 : www.motorola.com
site3 : www.linux.com
site2 : www.becbe.com
site1 : www.google.com