Contact Us
  
 
 
    SearchSearch   RegisterRegister   Log inLog in 

Read and write file in jsp

Read and write file in jsp

 
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans
View previous topic :: View next topic  
Author Message
admin
Site Admin


Joined: 24 Aug 2006
Posts: 30

PostPosted: Thu Jul 17, 2008 9:02 am    Post subject: Read and write file in jsp Reply with quote

How to read and write in file through JSP

This is example of reading and writing doc or any other file in java through JSP. We have to use input output class to achieve this reading file.

Code:


<%@ page import="java.io.*"  %>
<html>
<head>
<title>Read write file JSP</title>
</head>

<body>
<%
String fileName=getServletContext().getRealPath("jsp.txt");

File f=new File(fileName);

InputStream in = new FileInputStream(f);

BufferedInputStream bin = new BufferedInputStream(in);

DataInputStream din = new DataInputStream(bin);
StringBuffer sb=new StringBuffer();
while(din.available()>0)
    {
    sb.append(din.readLine());
    }

try {   
    PrintWriter pw = new PrintWriter(new FileOutputStream("c:/file.txt"));// save file
    pw.println(sb.toString());
    pw.close();
} catch(IOException e) {
   e.getMessage();
}
         
in.close();
bin.close();
din.close();
%>
Successfully write file
</body>
</html>



This is read first jsp.txt file from c drive and after reading this file it will save in file.txt
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans All times are GMT
Page 1 of 1

 
Jump to:  
  Quick Post a Reply Topic
     
  Name (required)  
     
  Email Address (will not be published) (required)  
     
  Subject of topic (required)  
     
   
     
   
     
  Advertisement  
     
 
     


  More Links  
 
JSP Introduction
JSP Setup
Simple JSP Example
JSP Variable
Array Declaration in JSP
JSP Forms and User Input
Form Validation
JSP Cookies
JSP Session Object
Loop, content collection, iterator, conditional check in JSP
JSP Application Object
JSP Include
JSP Response Object
JSP Request Object
JSP Error Handling
JSP File Handling Object
JSP Action Tag
JSP Implicit Objects
JSP Declaration
JSP Directive
JDBC Introduction
JDBC Driver
JDBC Connection
JDBC Prepared/Statement
JDBC ResultSet
JDBC Close Connection object
JDBC Insert
JDBC Display Records
JDBC Update
JDBC Delete
JDBC Sorting
JDBC Query
JDBC Exception
JDBC Add Batch
JDBC Transaction/RollBack