Friday, March 13, 2015

Set file permissions and Buffered Reader and array to input stream in Java



Set file permissions in java

package com.java2novice.files;
import java.io.File;
public class MyFilePermissions
{
          public static void main(String a[])
            {
        File scriptFile = new File("/home/java2novice/test.sh");
        System.out.println("Current file permissions:");
        System.out.println("Can Execute? "+scriptFile.canExecute());
        System.out.println("Can Read? "+scriptFile.canRead());
        System.out.println("Can Write? "+scriptFile.canWrite());
        scriptFile.setExecutable(true);
        scriptFile.setReadable(true);
        scriptFile.setWritable(true);
        System.out.println("Now file permissions:");
        System.out.println("Can Execute? "+scriptFile.canExecute());
        System.out.println("Can Read? "+scriptFile.canRead());
        System.out.println("Can Write? "+scriptFile.canWrite());
    }
}

Convert byte array to reader or Buffered Reader

package com.java2novice.files;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MyByteArrayToReader
{
     public static void main(String a[])
     {
            String str = "converting to input stream"+"\n and this is second line";
                   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 = bfReader.readLine()) != null)
              {
                System.out.println(temp);
            }
        }
         catch (IOException e)
          {
            e.printStackTrace();
        }
           finally
           {
            try
            {
                if(is != null) is.close();
            }
            catch (Exception ex){}
        }
    }
}

Convert input stream to reader or Buffered Reader

package com.java2novice.files;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 public class MyIstoBr
{
 public static void main(String a[])
{  
        InputStream is = null;
        BufferedReader bfReader = null;
        try
           {
            is = new FileInputStream("C:/sample.txt");
            bfReader = new BufferedReader(new InputStreamReader(is));
            String temp = null;
            while((temp = bfReader.readLine()) != null)
             {
                System.out.println(temp);
            }
        }
         catch (FileNotFoundException e)
           {
                   e.printStackTrace();
        }
        catch (IOException e)
          {
            e.printStackTrace();
        }
    }
}

Convert byte array to input stream

package com.java2novice.files;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ByteArrToInputStream
{
public static void main(String a[])
{
        String str = "converting to input stream";
        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("Data Recovered: "+new String(b));
        }
         catch (IOException e)
       {
            e.printStackTrace();
        }
         finally
          {
            try
              {
                if(is != null) is.close();
            }
         catch (Exception ex){}
        }
     }
}

No comments:

Post a Comment