creating new file with File class



Share on Google+Share on Google+

creating new file with File class
1 Answer(s)      8 years ago
Posted in : JSP-Interview Questions
View Answers

January 7, 2009 at 5:28 AM

Hi Jagadeesh,

Please implement following code:

<%@ page import="*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
int val =0;
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;

while (totalBytesRead < formDataLength) {
byteRead =, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
System.out.println("saveFile=" + saveFile);
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
System.out.println("saveFile" + saveFile);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;

pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));

Connection con=null;
PreparedStatement pstatement = null;
String line = null;
String value=null;
String url = "jdbc:mysql://";;
String dbName = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName = "amar";
String password = "amar123";
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
while (( line = input.readLine()) != null){
value = contents.toString();
con = DriverManager.getConnection(url+dbName,userName,password);
java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String strDateNew = sdf.format(now) ;
String queryString = "INSERT INTO FileUpload set filename='"+value+"',filedate='"+strDateNew+"'";

val = pstatement.executeUpdate();

<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database at <%=strDateNew%>.</b>

catch(Exception e){
System.out.println("Error :" + e);




Related Tutorials/Questions & Answers:
creating new file with File class - JSP-Interview Questions
creating new file with File class   I have a file uploading concept in my project. For that, while creating a new file is it necessary to give... of FileOutputStream class. If we give the path as from the file uploading location will it work
Create a new zip file in ZipArchive.
Creating a new Zip file in ZipArchive For creating zip file, use zipArchive () class. Create a new zip file in ZipArchive. Use the conditional statement... will generate a new zip file, in case of failure, it will print failed in creating
creating a file in a directory
creating a file in a directory  hi i have tried all the file methods... are new File ( ".", "C:/fileInRoot.txt" ) new File ( "myDir", "/otherDir" ) File( "myDir/OtherDir" ) File( "/OtherDir" ); new File ( "species/clan/individual.txt
class file
class file  How to create class file in jsf easily
file class
file class  Hi Friend, I have a file class it lets me extract all... void ReadCSV(){ File csvFile = new File("Hotels.csv... File"); } Scanner in = new Scanner(; try
Creating an executable jar file
Creating an executable jar file  When I try to create jar.exe through CMD, the manifest file created doesn't contain Main-Class attribute, even though it is included in text file used during creation of jar file
Creating XMl file - XML
Creating XMl file   I went on this page: and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file
Creating a setup file
Creating a setup file  How to create a setup file in tomcat as done in visual studio... could plz tell me as early as possible
Creating an exe file
Creating an exe file  Hi All, I have developed java application using swing by Net Beans IDE. Now I need to convert my application into exe file... as an exe file. Please anyone suggest me.Thanks in Advance. Thanks, T.Thanga meena
class file
class file  how to convert java classfile into java sourcefile
Creating JAR File - Java Beginners
or class files. And what is this manifest file, and what changes needs to be done... main(String[]args){ CreateJar jar=new CreateJar(); File folder = new File... file=new File("C://Answers//DataBase//DataBaseExamples.jar
a jsp code for creating a text file
for creating a new text file for each user, when they login in to the website for creating a new data file. So i need a jsp code for following options. when user login then the following has to be displayed for user 1.Creating the new file 2.delete
Creating a log in a text file - Applet
"); Label label1 = new Label("Enter the file name:"); TextField text = new...(); } else{ File f = new File(str); if(f.exists...Creating a log in a text file  Hey there, I have created an applet
Java file create new file
Java file create new file This section demonstrates you how to create a new... of these tools, you can can easily create a new empty file. Now for this task, we have used the method createNewFile() of boolean type which creates a new empty file
Create new file in java
Create new file in java We are going to discuss how to create new file in java... in java File class representing the files and folder pathnames by location. With File class we create files and directories. Java File represents actual
Java file class
Java file class  What is the purpose of the File class
new operator for creating the object of inner class
new operator for creating the object of inner class  Hi , The folliowing is my code : class Outer { class Inner{ void show... "); } } } class Test { public statis void main(String args
creating an .exe file - Java Beginners
creating an .exe file  how to create an .exe file in java? thanks in advance
about the base class of file
about the base class of file  what is the basic class of file
Creating JAR File - Java Beginners
a line to my manifest file (i.e. Main-Class: mypackage.MyClass...Creating JAR File  Respected Sir, Thankyou very much for your..., which says, Failed to load Main-Class manifest attribute from H:\Stuff\NIIT\Java
creating a jar file - JSP-Servlet
creating a jar file  Can you give me detail answer 1. i am having a servlet.class file in classes folder 2. web.xml file my questions are 1. where to place the html or jsp files 2. how to create a jar file and how can
Creating dynamic jsp file in java file to include another jsp file
Creating dynamic jsp file in java file to include another jsp file  I am able to create jsp file in java that includes ; table tr td and img tags..... Need to include jsp file in the same content. The below code doesn't help me
Random Access File class
Random Access File class  Write a short note on Random Access File class.   Please visit the following link: Java Random Access File
Java class file
Java class file  How do i open a java class file using eclipse
Creating a File in the directory.
Creating a file using HTML form action In this example, you will learn to make a query form asking to to create a file in PHP. You will have to write... of PHP code in the form. for creating a file in PHP, first declare a file name
NOT ABLE TO MAKE A NEW FILE IN SERVLET  What is the error in my code? It is not making a new file which contains the string values coming from a HTML...{ File file=new File("C://abc.xml"); FileWriter fr=new FileWriter
How To Create a New File
How To Create a New File In this section we will discuss about how to create a new file in Java. A Computer File is a storage of data. In this file we can.... Such as, File.createNewFile() method creates the new file. creates the new
Convert Class File to .java File - Java Beginners
Convert Class File to .java File  Sir, How To Convert .class file to .java File. Please Help me  Use cavaj software ,which is freeware, to convert .class to .java  Use jd-gui to convert ur .class to .java
Java file new line
Java file new line In this section, you will learn how to write the text in new line of text file. Description of code: The package* has provide... that uses the FileWriter class to parse the file and create it. The method
How to write file text In New Line
How to write file text In New Line  Hi friends, How to write file... you went new text in new line. For this we created a new text file named "newLineFileWriter.txt" using the File class. This file name we created that we use
Write Text To File In New Line.
Write Text To File In New Line. In this tutorial you will learn how to write text into file in a new line. When you are writing in a file you may be required... text file named "newLineFileWriter.txt" using the File class. This (File


Advertisement null