Monday, January 12, 2015

Delete temporary and Write String and Write Byte content to a file in java Pro



How to delete temporary file in java

package com.java2novice.files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class MyTempDelete
{
public static void main(String a[])
{
        File tempFile = null;
        BufferedWriter writer = null;
try
{
            tempFile = File.createTempFile("MyTempFile", ".tmp");
            writer = new BufferedWriter(new FileWriter(tempFile));
            writer.write("Writing data into temp file!!!");
  }
  catch (IOException e)
       {
            e.printStackTrace();
        }
         finally
        {
            try
           {
                if(writer != null) writer.close();
            }
         catch(Exception ex){}
        }
        System.out.println("Stored data in temporary file.");
        tempFile.deleteOnExit();
        tempFile.delete();
    }
}

How to write string content to a file in java

package com.java2novice.files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriteToFile
{
public static void main(String[] args)
{
  BufferedWriter bufferedWriter = null;
  try
  {
       String strContent = "This example shows how to write string content to a file";
            File myFile = new File("C:/MyTestFile.txt");
            if (!myFile.exists())
           {
                myFile.createNewFile();
            }
            Writer writer = new FileWriter(myFile);
            bufferedWriter = new BufferedWriter(writer);
            bufferedWriter.write(strContent);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
           {
                if(bufferedWriter != null) bufferedWriter.close();
            }
           catch(Exception ex){}
        }
    }
}

How to write byte content to a file in java

package com.java2novice.files; 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ByteWriteToFile
{
public static void main(String[] args)
{
       OutputStream opStream = null;
        try
      {
         String strContent = "This example shows how to write byte content to a file";
            byte[] byteContent = strContent.getBytes();
            File myFile = new File("C:/MyTestFile.txt");
            if (!myFile.exists())
           {
                myFile.createNewFile();
            }
            opStream = new FileOutputStream(myFile);
            opStream.write(byteContent);
            opStream.flush();
        }
      catch (IOException e)
      {
            e.printStackTrace();
        }
        finally
        {
            try
           {
                if(opStream != null) opStream.close();
            }
            catch(Exception ex){}
        }
    }
}





No comments:

Post a Comment