Sitemap    
  Home Food Photographs IT Parenting Resources Learning  
PERL
How to Read and write a file
Regular expression
How to Read and write a file

readwritefile.pl
#need data1.txt and data1.JPG

#data1.txt
#line1
#line2

print "----READ WRITE TEXT FILE----\n";
print "Use '+<' file to open in read only mode\n";
open FILE, "+<", "data1.txt" or die "Cannot open file" ;
print "file data - start\n";
while (<FILE>)
{
  print $_;
}
print "\nfile data - end\n\n";

print "Use '+>>' file to open in write and append mode\n";
print "No new file shall be created\n";
open FILE, "+>>", "data1.txt" or die "Cannot open file" ;
print "Adding lines to the file\n";
print FILE "\nline5\n";
print FILE "line6\n";
open FILE, "+<", "data1.txt" or die "Cannot open file" ;
print "file data - start\n";
while (<FILE>)
{
  print $_;
}
print "file data - end\n";
print "Note: New data is appended to old data\n\n";

print "Use '+>' file to open in write and truncate mode\n";
print "New file shall be created if it already exists\n";
open FILE, "+>", "data1.txt" or die "Cannot open file" ;
print "Adding lines to the file\n";
print FILE "line7\n";
print FILE "line8";
open FILE, "+<", "data1.txt" or die "Cannot open file" ;
print "file data - start\n";
while (<FILE>)
{
  print $_;
}
print "\nfile data - end\n";
print "Note: Old data is deleted\n";

close FILE;

print "\n----READ WRITE JPG/BINARY FILE----\n";
my $buffer;
open INPUTFILE, "+<", "data1.JPG" or  die "Cannot open file" ;
open OUTPUTFILE, ">", "dataOut.JPG" or  die "Cannot open file" ;
binmode INPUTFILE;
binmode OUTPUTFILE;
while (read (INPUTFILE, $buffer, 65536))
{
  print OUTPUTFILE $buffer;
}
close(INPUTFILE);
close(OUTPUTFILE);
print "Note: Now try to open written file to verify it got written correctly\n\n";

print "\n----Now use FILEHANDLE module to read and write----\n";
print "\n----Now use FILEHANDLE module to read----\n";
use FileHandle;
$fh = FileHandle->new;
if ($fh->open("< data1.txt"))
{
  print <$fh>;
  $fh->close;
}

print "\n----Now use FILEHANDLE module to write----\n";
$fh = FileHandle->new("> data1.txt");
print $fh "line9\n";
$fh->close;
$fh = FileHandle->new("< data1.txt");
print <$fh>;
$fh->close;

print "\n----Now use FILEHANDLE module to READ WRITE JPG/BINARY FILE----\n";
$fhIn =  FileHandle->new("< data1.JPG");
$fhOut = FileHandle->new("> dataOut1.JPG");
my $buffer;
binmode $fhIn;
binmode $fhOut;
while (read ($fhIn, $buffer, 65536))
{
  print $fhOut $buffer;
}
$fhIn->close;
$fhOut->close;

Output
$ perl readwritefile.pl
----READ WRITE TEXT FILE----
Use '+<' file to open in read only mode
file data - start
line9

file data - end

Use '+>>' file to open in write and append mode
No new file shall be created
Adding lines to the file
file data - start
line9

line5
line6
file data - end
Note: New data is appended to old data

Use '+>' file to open in write and truncate mode
New file shall be created if it already exists
Adding lines to the file
file data - start
line7
line8
file data - end
Note: Old data is deleted

----READ WRITE JPG/BINARY FILE----
Note: Now try to open written file to verify it got written correctly


----Now use FILEHANDLE module to read and write----

----Now use FILEHANDLE module to read----
line7
line8
----Now use FILEHANDLE module to write----
line9

----Now use FILEHANDLE module to READ WRITE JPG/BINARY FILE----

ADD TO DEL.ICIO.US ADD TO DIGG ADD TO FURL ADD TO REDDIT ADD TO STUMBLEUPON ADD TO TECHNORATI FAVORITES ADD TO SQUIDOO ADD TO YAHOO MYWEB ADD TO ASK ADD TO GOOGLE
Post your comments:
Your Name: 
Security check:
Your Comment: 1500 chars
Latest
How to loop arraylist JAVA6 Syntax
How to iterrate through ArrayList and display multiple rows using for loop JAVA6 syntax..
Java JSP  2010-Oct-12
How to loop arraylist
How to iterrate through ArrayList and display multiple rows using for loop, JSTL tags..
Java JSP  2010-Oct-12
Consumer using JAX WS Dispatch API and DOM parser 1
Access/Consume Webservice using Servlet, JAX-WS Dispatch API dynamic client and parsing SOAP response using DOM parser...
Java Webservice  2010-Sep-20
Simple Java first CXF Webservice 4
Building Simple Java first CXF webservice using CXFServlet, Spring, jaxws:endpoint, @WebService, @SOAPBinding, @WebResult
Java Webservice  2010-Sep-19
Simple Java first CXF Webservice 3
Building Simple Java first CXF webservice using CXFServlet, Spring, jaxws:endpoint, @WebService, @SOAPBinding, @WebResult
Java Webservice  2010-Sep-18
Simple Java first CXF Webservice 1
Building Simple Java first CXF webservice using CXFServlet, Spring, jaxws:endpoint, @WebService, @SOAPBinding, @WebResult
Java Webservice  2010-Sep-18
Simple Java first CXF Webservice 2
Building Simple Java first CXF webservice using CXFServlet, Spring, jaxws:endpoint, @WebService, @SOAPBinding, @WebResult
Java Webservice  2010-Sep-18
Encoding special characters in userinput or on server
Encoding can be done either in Javascript or JAVA encodeURIComponent, escape, java.net.URLEncoder..
Java JSP  2010-Aug-09
Jstl fmt tag i18n formatdate formatcurrency
fmt:setBundle, native2ascii.exe, fmt:message, fmt:setLocale, fmt:formatDate, fmt:formatNumber type=currency..
Java JSP  2010-Aug-05
How to read and write a file
How to read and write a file java.io.BufferedReader,java.io.FileReader, InputStreamReader ...
Java J2SE  2010-Aug-04
jQuery validate form using ajax 2
How to validate/submit form using ajax and jQuery input#, ($.ajax)..
Javascript  2010-Jul-23
jQuery validate form using ajax 1
How to validate/submit form using ajax and jQuery ($.ajax)..
Javascript  2010-Jul-23
How to create datasource in RAD Websphere
Create JDBC provider, datasource and JAAS security setup, JDBC connection URLs
Servers Websphere  2010-Jul-08
Get Started 4
Struts2 framework structure, how various components fit together
Java Struts2  2010-Jul-07
How to populate a form when JSP is called first time
This is achieved throught setting bean in request, using frameworks like Struts2, Spring3..
Java JSP  2010-Jul-07
Spring3 And Hibernate 4
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07