<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ankit Virparia &#187; File</title>
	<atom:link href="http://ankit.co/tag/file/feed" rel="self" type="application/rss+xml" />
	<link>http://ankit.co</link>
	<description>A Programmer, Designer and Trainer</description>
	<lastBuildDate>Sun, 11 May 2014 04:15:47 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Various ways to read a file</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-a-file?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=various-ways-to-read-a-file</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-a-file#comments</comments>
		<pubDate>Thu, 17 Jan 2013 07:22:16 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=566</guid>
		<description><![CDATA[<p>Persistence vs Permanent storage In programming, persistence storage refers to a storage area which may forget the data/information once it looses the electronic energy supply. such as RAM &#8211; Random Access Memory. When we run an application having some variables, methods, objects, etc. That data is stored in Computer RAM. Problem with this system is [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-a-file">Various ways to read a file</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h2>Persistence vs Permanent storage</h2>
<p>In programming, persistence storage refers to a storage area which may forget the data/information once it looses the electronic energy supply. such as RAM &#8211; Random Access Memory. When we run an application having some variables, methods, objects, etc. That data is stored in Computer RAM. Problem with this system is when we want to share some information between two execution cycle that won&#8217;t be possible. For Example: We have developed a &#8220;Student Management System&#8221; for a college and one operator starts entering student records in our software. If we as a developer has written code which stores student information in an Array, in that case a power failure/system crash/software termination/software closing may lead to information lost and operator need to start entering details from the scratch! Here the conclusion is: We cannot not store the important or permanent data in RAM. We must choose either 1) File 2) Database. In this tutorial we gonna learn about reading a file. so that the previously written data can be fetched and processed. By this way our application will also be in a consistent state. Following are the various ways to read a file in any storage device like hard disk, floppy, pen drive, etc. The basic need to use a storage device is to mount the same and you are ready to go!</p>
<h2>FileInputStream</h2>
<p>FileInputStream is derived from InputStream class.</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class FileInputStreamDemo
{
	public static void main(String[] s)throws IOException
	{
		byte[] b = new byte[10000];
		File f = new File(&quot;data.txt&quot;);  // this wil points to the data.txt of the same directory where your program is stored.
		FileInputStream fis = new FileInputStream(f);
		int length = fis.read(b);
		String str = new String(b,0,length);
		
		System.out.println(&quot;Data: &quot;+str);
		
		fis.close();
			
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileInputStreamDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="File Input Stream example" src="../../../images/FileInputStreamDemo_400.jpg" /></a></p>
<hr/>
<h2>FileReader</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class FileReaderDemo
{
	public static void main(String[] s)throws IOException
	{
		String str = &quot;&quot;;
		char[] c = new char[100];
		File f = new File(&quot;data.txt&quot;); 
		FileReader fr = new FileReader(f);
		
		int len = 0;
		len = fr.read(c);
		str = new String(c,0,len);
		System.out.println(&quot;Data: &quot;+str);
		
		fr.close();
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileReaderDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="File Reader example" src="../../../images/FileReaderDemo_400.jpg" /></a></p>
<hr/>
<h2>BufferedWriter + FileWriter</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class FileBufferedReaderDemo
{
	public static void main(String[] arg)throws Exception
	{
		BufferedReader br = new BufferedReader(new FileReader(new File(&quot;data.txt&quot;)));
		System.out.print(&quot;Data: &quot;);
		String str = br.readLine();
		System.out.println(str);	
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileBufferedReaderDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Buffered Reader file example" src="../../../images/FileBufferedReaderDemo_400.jpg" /></a></p>
<hr/>
<h2>FilterReader</h2>
<p>FilterReader is an abstract class which does not have any abstract method. We can override the methods of it and perform our code instead. For more details visit: <a href="tutorials/java-tutorials/input-output/filterreader" title="FilterReader">FilterReader</a></p>
<h3>Aim</h3>
<p>Write a program using FilterReader which removes tags from the file content. e.g. &lt;tag&gt; should be removed.</p>
<pre class="brush: java; title: ; notranslate">

import java.io.*;
class FilterReaderDemo
{
  	public static void main(String[] args) 
	{
		try 
		{
          FilterReader fr = new FilterReader(new FileReader(&quot;data.txt&quot;)){
			  		  public int read(char[] buf, int from, int len) throws IOException 
					  {
						boolean intag = false;
						int numchars = 0; 
						
						while (numchars == 0) 
						{
						  numchars = in.read(buf, from, len); 
						  if (numchars == -1)
						  {
							return -1;
						  }
						  int last = from; 
						  for (int i = from; i &lt; from + numchars; i++) 
						  {
							if (!intag) 
							{
							  if (buf[i] == '&lt;')
								intag = true; 
							  else
								buf[last++] = buf[i]; 
							} 
							else if (buf[i] == '&gt;')
							{
							  intag = false;
							}
						  }
						  numchars = last - from; 
						}
						return numchars;
					  }
					
					  public int read() throws IOException 
					  {
						char[] buf = new char[1];
						int result = read(buf, 0, 1);
						if (result == -1)
						  return -1;
						else
						  return (int) buf[0];
					  }
			  };
		  BufferedReader in = new BufferedReader(fr);
		  
		  String line;
		  while ((line = in.readLine()) != null)
		  {
			System.out.println(line);
		  }
		  in.close(); // Close the stream.
		} 
		catch (Exception e) 
		{
		  System.err.println(e);
		}
  }
}

</pre>
<p><strong>Output:</strong><br />
<strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FilterDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Filter Demo" src="../../../images/FilterDemo_400.jpg" /></a><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FilterDemoOutput.jpg" rel="attachment"><img class="size-full aligncenter" alt="BufferedWriter demo" src="../../../images/FilterDemoOutput_400.jpg" /></a></p>
<hr/>
<h2>DataInputStream</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class DataInputStreamDemo
{
	public static void main(String[] s)throws Exception
	{
		FileInputStream fis = new FileInputStream(new File(&quot;db.txt&quot;));
		DataInputStream dis = new DataInputStream(fis);
		int avail =  dis.available();
		byte[] b = new byte[avail];
		dis.readFully(b);
		System.out.println(new String(b)); 
		dis.close();		
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/DataInputStreamDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Data Input StreamDemo" src="../../../images/DataInputStreamDemo_400.jpg" /></a></p>
<hr/>
<h2>ObjectInputStream</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
import java.util.*;
class ObjectInputStreamDemo
{
	public static void main(String[] s)throws Exception
	{
		FileInputStream fis = new FileInputStream(&quot;test.txt&quot;);
        ObjectInputStream ois = new ObjectInputStream(fis);

        System.out.println(ois.readInt());
        System.out.println(ois.readObject());
        System.out.println(ois.readObject());
		ois.close();
		System.out.println(&quot;All Objects read!&quot;);
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/ObjectInputStreamDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Object Input Demo" src="../../../images/ObjectInputStreamDemo_400.jpg" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-a-file">Various ways to read a file</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-a-file/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>FilterReader</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/filterreader?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=filterreader</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/filterreader#comments</comments>
		<pubDate>Thu, 17 Jan 2013 06:07:58 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=567</guid>
		<description><![CDATA[<p>What is Filtering? A normal reader/writer class can read/write what we demand for. But lets say we have a special requirement of writing/reading. For Example: We want to read only those lines from the file which has &#8220;.&#8221; at the end. Rest we want to skip. For this kind of scenario where by some very [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/filterreader">FilterReader</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h2>What is Filtering?</h2>
<p>A normal reader/writer class can read/write what we demand for. But lets say we have a special requirement of writing/reading. For Example: We want to read only those lines from the file which has &#8220;.&#8221; at the end. Rest we want to skip. For this kind of scenario where by some very frequent and static processing is to be done each time then we can create our own FilterReader which will do the job for us. So that manually we will not be doing it again and again. FilterReader is an abstract class which does not have any abstract method. We can override the methods of it and perform our code instead. We are going to discuss another case here.</p>
<h3>Aim</h3>
<p>Write a program using FilterReader which removes tags from the file content. e.g. &lt;tag&gt; should be removed.</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class NoTagReader extends FilterReader 
{
  boolean intag = false;
  public NoTagReader(Reader r) 
  {
    super(r);
  }

  public int read(char[] buf, int from, int len) throws IOException 
  {
    int numchars = 0; 
	
    while (numchars == 0) 
	{
      numchars = in.read(buf, from, len); 
      if (numchars == -1)
	  {
        return -1;
	  }
      int last = from; 
      for (int i = from; i &lt; from + numchars; i++) 
	  {
        if (!intag) 
		{
          if (buf[i] == '&lt;')
            intag = true; 
          else
            buf[last++] = buf[i]; 
        } 
		else if (buf[i] == '&gt;')
		{
          intag = false;
		}
      }
      numchars = last - from; 
    }
    return numchars;
  }

  public int read() throws IOException 
  {
    char[] buf = new char[1];
    int result = read(buf, 0, 1);
    if (result == -1)
      return -1;
    else
      return (int) buf[0];
  }

}
class FilterReaderDemo
{
  	public static void main(String[] args) 
	{
		try 
		{
          NoTagReader ntr = new NoTagReader(new FileReader(&quot;data.txt&quot;))
		  BufferedReader in = new BufferedReader(ntr);
		  
		  String line;
		  while ((line = in.readLine()) != null)
		  {
			System.out.println(line);
		  }
		  in.close(); // Close the stream.
		} 
		catch (Exception e) 
		{
		  System.err.println(e);
		}
  }
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FilterReaderDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Filter Reader Demo" src="../../../images/FilterReaderDemo_400.jpg" /></a><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FilterRaderDemo_output.jpg" rel="attachment"><img class="size-full aligncenter" alt="Filter Reader demo" src="../../../images/FilterRaderDemo_output_400.jpg" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/filterreader">FilterReader</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/filterreader/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FilterWriter</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/filterwriter?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=filterwriter</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/filterwriter#comments</comments>
		<pubDate>Wed, 16 Jan 2013 08:27:51 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Filter]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=556</guid>
		<description><![CDATA[<p>What is Filtering? A normal reader/writer class can read/write what we demand for. But lets say we have a special requirement of writing/reading. For Example: At the end of the file which we write, we want to print(write) Date. Which is a special requirement and this will be needed for each time we write a [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/filterwriter">FilterWriter</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h2>What is Filtering?</h2>
<p>A normal reader/writer class can read/write what we demand for. But lets say we have a special requirement of writing/reading. For Example: At the end of the file which we write, we want to print(write) Date. Which is a special requirement and this will be needed for each time we write a file. For this kind of scenario where by some very frequent and static processing is to be done each time then we can create our own FilterWriter which will do the job for us. So that manually we will not be doing it again and again. FilterWriter is an abstract class which does not have any abstract method. We can override the methods of it and perform our code instead. We are going to discuss another case here.</p>
<h3>Aim</h3>
<p>Write a program using FilterWriter which converts larges statements(having more than 15 chars) to  a shorter statement and write a file.<br />
e.g. If we tries to write &#8220;This is demo by Ankit&#8221;. It appends first 5 chars and last 5 chars of string with &#8220;&#8230;&#8221; and generate &#8220;This &#8230; Ankit&#8221; as a output.</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

public class FilterDemo 
{

    public static void main(String[] args)throws Exception
	{
        File f = new File(&quot;data.txt&quot;);  
		FileWriter fw = new FileWriter(f);
		myFilterWriter mfw = new myFilterWriter(fw);
		mfw.write(&quot;This is demo by Ankit&quot;);
		mfw.flush();
		mfw.close();
    }
}

class myFilterWriter extends FilterWriter
{
    myFilterWriter(Writer w)
    {
       super(w); 
    }

    @Override
    public void write(String str) throws IOException 
    {
        int len = str.length();
        String output = &quot;&quot;;
        if(len&gt;15)
        {
            output = str.substring(0,5);
            output += &quot;... &quot;;
            output += str.substring(len-5,len);
        }
        super.write(output); 
    }
    
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FilterDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Filter Demo" src="../../../images/FilterDemo_400.jpg" /></a><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FilterDemoOutput.jpg" rel="attachment"><img class="size-full aligncenter" alt="BufferedWriter demo" src="../../../images/FilterDemoOutput_400.jpg" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/filterwriter">FilterWriter</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/filterwriter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Various ways to write a file</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-a-file?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=various-ways-to-write-a-file</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-a-file#comments</comments>
		<pubDate>Wed, 16 Jan 2013 05:14:54 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=550</guid>
		<description><![CDATA[<p>Persistence vs Permanent storage In programming, persistence storage refers to a storage area which may forget the data/information once it looses the electronic energy supply. such as RAM &#8211; Random Access Memory. When we run an application having some variables, methods, objects, etc. That data is stored in Computer RAM. Problem with this system is [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-a-file">Various ways to write a file</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h2>Persistence vs Permanent storage</h2>
<p>In programming, persistence storage refers to a storage area which may forget the data/information once it looses the electronic energy supply. such as RAM &#8211; Random Access Memory. When we run an application having some variables, methods, objects, etc. That data is stored in Computer RAM. Problem with this system is when we want to share some information between two execution cycle that won&#8217;t be possible. For Example: We have developed a &#8220;Student Management System&#8221; for a college and one operator starts entering student records in our software. If we as a developer has written code which stores student information in an Array, in that case a power failure/system crash/software termination/software closing may lead to information lost and operator need to start entering details from the scratch! Here the conclusion is: We cannot not store the important or permanent data in RAM. We must choose either 1) File 2) Database. In this tutorial we gonna learn about writing a file. so that the same/other application can read the data which we have written in future. By this way our application will also be in a consistent state. Following are the various ways to write a file in any storage device like hard disk, floppy, pen drive, etc. The basic need to use a storage device is to mount the same and you are ready to go!</p>
<h2>FileOutputStream</h2>
<p>FileOutputStream is derived from OutputStream class.</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class FileOutputStreamDemo
{
	public static void main(String[] s)throws IOException
	{
		byte[] b;
		String str = &quot;I want to write some text to file.&quot;;
		b = str.getBytes();
		
		
		File f = new File(&quot;data.txt&quot;);  // this will points to the data.txt of the same directory where your program is stored.
		FileOutputStream fos = new FileOutputStream(f);
		fos.write(b);
		fos.flush();
		
		System.out.println(&quot;File Created!&quot;);
		
		fos.close();
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileOutputStreamDemoOutput.jpg" rel="attachment"><img class="size-full aligncenter" alt="File Output Stream example" src="../../../images/FileOutputStreamDemoOutput_400.jpg" /></a><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileOutputStreamDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="File Output Stream demo" src="../../../images/FileOutputStreamDemo_400.jpg" /></a></p>
<hr/>
<h2>FileWriter</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class FileWriterDemo
{
	public static void main(String[] s)throws IOException
	{
		String str = &quot;I want to write some text to file.&quot;;
		
		File f = new File(&quot;data.txt&quot;);  // this wil points to the data.txt of the same directory where your program is stored.
		FileWriter fw = new FileWriter(f);
		
		fw.write(str);
		fw.flush();
		
		System.out.println(&quot;File Created!&quot;);
		
		fw.close();
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileOutputStreamDemoOutput.jpg" rel="attachment"><img class="size-full aligncenter" alt="File Writer example" src="../../../images/FileOutputStreamDemoOutput_400.jpg" /></a><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileWriter.jpg" rel="attachment"><img class="size-full aligncenter" alt="File Writer demo" src="../../../images/FileWriter_400.jpg" /></a></p>
<hr/>
<h2>BufferedWriter + FileWriter</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class FileBufferedWriterDemo
{
	public static void main(String[] s)throws IOException
	{
		String str = &quot;I want to write some text to file.&quot;;
		
		
		File f = new File(&quot;data.txt&quot;);  // this wil points to the data.txt of the same directory where your program is stored.
		FileWriter fw = new FileWriter(f);
		BufferedWriter bw = new BufferedWriter(fw);
		
		bw.write(str);
		bw.flush();
		
		System.out.println(&quot;File Created!&quot;);
		
		bw.close();
			
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileOutputStreamDemoOutput.jpg" rel="attachment"><img class="size-full aligncenter" alt="Buffered Writer file example" src="../../../images/FileOutputStreamDemoOutput_400.jpg" /></a><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileBufferedWriterDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="BufferedWriter demo" src="../../../images/FileBufferedWriterDemo_400.jpg" /></a></p>
<hr/>
<h2>FilterWriter</h2>
<p>FilterWriter is an abstract class which does not have any abstract method. We can override the methods of it and perform our code instead. For more details visit: <a href="tutorials/java-tutorials/input-output/filterwriter" title="FilterWriter">FilterWriter</a></p>
<h3>Aim</h3>
<p>Write a program which defines a FilterWriter which converts larges statements(having more than 15 chars) to  a shorter statement and write a file.<br />
e.g. If we tries to write &#8220;This is demo by Ankit&#8221;. It appends first 5 chars and last 5 chars of string with &#8220;&#8230;&#8221; and generate &#8220;This &#8230; Ankit&#8221; as a output.</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
public class FilterDemo
{
    public static void main(String[] args) throws IOException 
    {
        // TODO code application logic here
        File f = new File(&quot;data.txt&quot;);  
        FileWriter fw = new FileWriter(f);
        FilterWriter mfw = new FilterWriter(fw){
            @Override
            public void write(String str) throws IOException 
            {
                int len = str.length();
                String output = &quot;&quot;;
                if(len&gt;15)
                {
                    output = str.substring(0,5);
                    output += &quot;... &quot;;
                    output += str.substring(len-5,len);
                }
                super.write(output); 
             }};
        mfw.write(&quot;This is demo by Ankit&quot;);
        mfw.flush();
        mfw.close();
        
    }
}

</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FilterDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Filter Demo" src="../../../images/FilterDemo_400.jpg" /></a><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FilterDemoOutput.jpg" rel="attachment"><img class="size-full aligncenter" alt="BufferedWriter demo" src="../../../images/FilterDemoOutput_400.jpg" /></a></p>
<hr/>
<h2>DataOputputStream</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class DataOutputStreamDemo
{
	public static void main(String[] s)throws Exception
	{
		FileOutputStream fos = new FileOutputStream(new File(&quot;db.txt&quot;));
		DataOutputStream dos = new DataOutputStream(fos);
		dos.writeBytes(&quot;This is Test\n\n&quot;); 
		dos.writeDouble(1232.23);
		dos.writeInt(123);
		
		dos.flush();
		dos.close();	
		System.out.println(&quot;File Written&quot;);	
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/DataOutputStreamDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Filter Demo" src="../../../images/DataOutputStreamDemo_400.jpg" /></a></p>
<hr/>
<h2>ObjectOputputStream</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
import java.util.*;
class ObjectOutputStreamDemo
{
	public static void main(String[] s)throws Exception
	{
		FileOutputStream fos = new FileOutputStream(&quot;test.txt&quot;);
                ObjectOutputStream oos = new ObjectOutputStream(fos);

                oos.writeInt(12345);
                oos.writeObject(&quot;Today&quot;);
                oos.writeObject(new Date());
		oos.flush();
                oos.close();
		System.out.println(&quot;Objects written!&quot;);
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/OOS.jpg" rel="attachment"><img class="size-full aligncenter" alt="Object Output Demo" src="../../../images/OOS_400.jpg" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-a-file">Various ways to write a file</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-a-file/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating directories using Java</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/creating-directories-using-java?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-directories-using-java</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/creating-directories-using-java#comments</comments>
		<pubDate>Mon, 14 Jan 2013 08:11:34 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[Directory]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=509</guid>
		<description><![CDATA[<p>Program to create a new Directory using File class: Output: Program to create multiple new Directories using File class: In case if we would like to create multiple nested directories, A different function is to be invoked. which is: mkdirs() Output:</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/creating-directories-using-java">Creating directories using Java</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h3>Program to create a new Directory using File class:</h3>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class mkdirDemo
{
	public static void main(String[] s)
	{
		File f = new File(&quot;newDir&quot;);
		f.mkdir();
		System.out.println(&quot;Directory created.&quot;);	
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/newDirectory.jpg" rel="attachment"><img class="size-full aligncenter" alt="create 1 directory" src="../../../images/newDirectory_400.jpg" /></a></p>
</hr>
<h3>Program to create multiple new Directories using File class:</h3>
<p>In case if we would like to create multiple nested directories, A different function is to be invoked. which is: <strong>mkdirs()</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class mkdirsDemo
{
	public static void main(String[] s)
	{
		File f = new File(&quot;a/b/newDir/testDir/c&quot;);
		f.mkdirs();
		System.out.println(&quot;Directories created.&quot;);	
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/mkdirsDemoOutput.jpg" rel="attachment"><img class="size-full aligncenter" alt="create multiple directories" src="../../../images/mkdirsDemoOutput_400.jpg" /></a><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/mkdirsDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="create multiple directories" src="../../../images/mkdirsDemo_400.jpg" /></a></p>
</hr>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/creating-directories-using-java">Creating directories using Java</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/creating-directories-using-java/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Listing content of given Directory</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/listing-content-of-given-directory?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=listing-content-of-given-directory</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/listing-content-of-given-directory#comments</comments>
		<pubDate>Mon, 14 Jan 2013 07:27:45 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=506</guid>
		<description><![CDATA[<p>Our goal is to read the content of given directory and list all the files and folders available inside it. To retrieve list of files/folders inside the given folder, we can call list() method of File class as shown in the example. Output:</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/listing-content-of-given-directory">Listing content of given Directory</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Our goal is to read the content of given directory and list all the files and folders available inside it. To retrieve list of files/folders inside the given folder, we can call list() method of File class as shown in the example.</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
import java.util.*;
class ListDirContent 
{
	public static void main(String args[]) 
	{
		String dirname = &quot;&quot;;
		System.out.print(&quot;Enter a path:&quot;);
		Scanner sc = new Scanner(System.in);
		dirname = sc.nextLine();
		
		File f1 = new File(dirname);
		if (f1.isDirectory()) 
		{
			System.out.println(&quot;Directory of &quot; + dirname);
			String s[] = f1.list();
			for (int i=0; i &lt; s.length; i++) 
			{
				File f = new File(dirname + &quot;/&quot; + s[i]);
				if (f.isDirectory()) 
				{
					System.out.println(s[i] + &quot; is a directory&quot;);
				} 
				else 
				{
					System.out.println(s[i] + &quot; is a file&quot;);
				}
			}
		} 
		else 
		{
			System.out.println(dirname + &quot; is not a directory&quot;);
		}
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/ListDirContent.jpg" rel="attachment"><img class="size-full aligncenter" alt="List directory content" src="../../../images/ListDirContent_400.jpg" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/listing-content-of-given-directory">Listing content of given Directory</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/listing-content-of-given-directory/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to File</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/introduction-to-file?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=introduction-to-file</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/introduction-to-file#comments</comments>
		<pubDate>Sun, 13 Jan 2013 18:17:05 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=495</guid>
		<description><![CDATA[<p>File is a class which belongs to java.io package. As the name suggests File class can be used to point a File as well as a folder stored in any storage device. How to use File class To check the availability of folder or file. To check the permission of folder or file. To create [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/introduction-to-file">Introduction to File</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>File is a class which belongs to java.io package. As the name suggests File class can be used to point a File as well as a folder stored in any storage device. </p>
<h3>How to use File class</h2>
<ul>
<li>To check the availability of folder or file.</li>
<li>To check the permission of folder or file.</li>
<li>To create a blank file.</li>
<li>To create a stream which points to specific named file which can be used to read/write some data</li>
</ul>
<hr/>
<h2>Program to create a new blank file.</h2>
<p><strong>File Name:</strong> CreateBlankFile.java</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class CreateBlankFile 
{
	public static void main(String[] s) throws IOException
	{
		File f = new File(&quot;myFile.txt&quot;);
		f.createNewFile();
		System.out.println(&quot;File created!&quot;);	
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/fileCreateDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="File Create" src="../../../images/fileCreateDemo_400.jpg" /></a></p>
<hr/>
<h2>Retrieve file information</h2>
<p><strong>File Name:</strong> FileMethodDemo.java</p>
<pre class="brush: java; title: ; notranslate">
import java.io.File;
class FileMethodDemo 
{
	public static void main(String args[]) 
	{
		File f1 = new File(&quot;myFile.txt&quot;);
		System.out.println(&quot;File Name: &quot; + f1.getName());
		System.out.println(&quot;Path: &quot; + f1.getPath());
		System.out.println(&quot;Abs Path: &quot; + f1.getAbsolutePath());
		System.out.println(&quot;Parent: &quot; + f1.getParent());
		System.out.println(f1.exists() ? &quot;exists&quot; : &quot;does not exist&quot;);
		System.out.println(f1.canWrite() ? &quot;is writeable&quot; : &quot;is not writeable&quot;);
		System.out.println(f1.canRead() ? &quot;is readable&quot; : &quot;is not readable&quot;);
		System.out.println(&quot;is &quot; + (f1.isDirectory() ? &quot;&quot; : &quot;not&quot; + &quot; a directory&quot;));
		System.out.println(f1.isFile() ? &quot;is normal file&quot; : &quot;might be a named pipe&quot;);
		System.out.println(f1.isAbsolute() ? &quot;is absolute&quot; : &quot;is not absolute&quot;);
		System.out.println(&quot;File last modified: &quot; + f1.lastModified());
		System.out.println(&quot;File size: &quot; + f1.length() + &quot; Bytes&quot;);
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/FileInfoDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="File Information" src="../../../images/FileInfoDemo_400.jpg" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/introduction-to-file">Introduction to File</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/introduction-to-file/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
