Home Answers Viewqa JSP-Interview-Questions creating new file with File class

 
 


Jagadeesh
creating new file with File class
1 Answer(s)      4 years and 5 months ago
Posted in : JSP-Interview Questions

View Answers

January 7, 2009 at 5:28 AM


Hi Jagadeesh,

Please implement following code:


<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<html>
<%
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 = in.read(dataBytes, 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://192.168.10.211:3306/";;
String dbName = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName = "amar";
String password = "amar123";
try{
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
while (( line = input.readLine()) != null){
contents.append(line);
}
value = contents.toString();
Class.forName(driver);
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+"'";

pstatement=con.prepareStatement(queryString);
val = pstatement.executeUpdate();

if(val>0){
%>
<br><br>
<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database at <%=strDateNew%>.</b>
<%
}

}
catch(Exception e){
System.out.println("Error :" + e);
}
}
%>
</html>

------------------------------------

Thanks.









Related Pages:
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 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
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
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
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
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(System.in); try
FILE
args[]) { try { File file=new File("Name.txt"); FileReader f=new FileReader(file... the content of the file. import java.io.*; public class Appliance { public static void main(String args[]) { try { File file=new File
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
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 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
NOT ABLE TO MAKE A NEW FILE IN SERVLET
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. java.io.File creates the new
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 java.io.* has provide... that uses the FileWriter class to parse the file and create it. The method
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
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
class file
class file  How to create class file in jsf easily
Creating XMl file - XML
Creating XMl file   I went on this page: http://www.roseindia.net/xml/dom/createblankdomdocument.shtml 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
Creating a JAR file in Java
Creating a JAR file in Java       This section provides you to the creating a jar file.... This program has been used the jar command "jar cf jar-file-name directory
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
Create zip file in Java
program. Our program accepts the file name and the output file name for creating... of the ZipOutputStream class by passing the FileOutpuStream instance. The zip file name...("Please enter file name to zip : "); BufferedReader input = new
Java file new directory
Java file new directory In this section, you will learn how to create a new... an object of File class and specify a directory which you want to create. Then we have called the method mkdir() through the object of file class. On executing
class file
class file  how to convert java classfile into java sourcefile
file
file  hi i want read special line of file. what am i doing ? I write... read data from file. please help me! void maketree(BtNode root,int i) throws FileNotFoundException, IOException{ FileInputStream fis=new
Java IO File
of this class a Java programmer can create a new file. File.createNewFile() method...Java IO File In this section we will discuss about the File class in Java... can also create a File to store the data. To create file in Java a class named
creating an .exe file - Java Beginners
creating an .exe file  how to create an .exe file in java? thanks in advance
Java append new node to XML file
Java append new node to XML file In this tutorial, you will learn how to append new node to xml file. Sometimes there is a need to append a new XML node to the xml file. Here is an xml file where we are going to append a new node
Storing properties in XML file
in xml file:- File f=new File("2.xml"):-Creating File in which properties are to be Stored. Properties p=new Properties():-Creates an instance of class... Storing properties in XML file     
Creating a ZIP file in Java
Creating a ZIP file in Java       Zip File: Zip file format is the popular method of data compression. Zip file contains many files in compressed format. You can
creating Xml - Java Beginners
creating Xml  how we can create a xml file using java Creating XML... object. Creating XML File is possible  Creating XML File is possible... = buff.readLine(); File file = new File(str); if(file.exists
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 a temporary file
(String prefix, String suffix): This is a static method of file class... Java Temporary File - Temporary File Creation in Java... a temporary file, which will be deleted automatically by the garbage collector when
Creating objects for classes and calling functions of a dll file using JNI
Creating objects for classes and calling functions of a dll file using JNI  Hi all, I am new to JNI. I am having a dll file created by somebody else. In this file, some c++ classes and functions are available. How can we create
creating document in XML - XML
creating document in XML  Create an XML document for catalogue...*; import javax.xml.transform.stream.*; import org.w3c.dom.*; public class... { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in
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
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
File I/O
File I/O  i have a problem i am trying to print on a new line every time i reach a certain condition "if(line.startsWith("Creating"))" i want... File file = new File(inpath);//files from the inpath File[] files
File upload - JSP-Servlet
= ((file.substring(0, boundaryLocation)).getBytes()).length; // creating a new file...); totalBytesRead += byteRead; } String file = new String(dataBytes...")); out.println(saveFile); File up = new File(saveFile); out.println
creating table in hibernate
creating table in hibernate  how to create table by the help... ========================================== /* Class Course */ package com.mypkg.course; import java.io.Serializable; public class Course implements Serializable { /** * */ private
read a file
static void main(String[] args) { File file = new File("D://Try.txt"); try { FileInputStream fin = new FileInputStream(file); byte...); String strFileContent = new String(fileContent); System.out.println("File
creating web services - WebSevices
creating web services  Actually i am new for web services so Plz tell... stopped publish Window II. Steps for Creating Web Service 1.Create a new Dynamic Web Project in (File-> New -> Other -> web -> Dynamic web project
Property File
"); properties.setProperty("National-Bird", "Peacock"); File file = new File("file.properties"); FileOutputStream fos = new FileOutputStream(file...Property File  how to store key & values pair in property file
Java file class
Java file class  What is the purpose of the File class
Convert Text File to PDF file
Text file to PDF File, public class TextFileToPDF { private static void... BufferedReader(new FileReader( "**INPUT FILE**")); String inLine...(document, new FileOutputStream( "**OUTPUT FILE
How to copy a file in java
IO " , in that there is one class called FileUtils, which provide file...]; // creating a byte type buffer to store the content of file. int len...How to copy a file in java In this section you will learn about how to copy
about the base class of file
about the base class of file  what is the basic class of file
File handling
in a file import java.io.*; import java.util.*; class FileOperations{ public static...=new File("C:/hello.txt"); BufferedReader br=new BufferedReader(new...File handling  how do i search for an occurance of a word in a file
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
and writing to new file.  Hello, I would like to know how to write... stumped when it comes to reading the file and writing to a new file. My code... the input one."); }else{ loop=1;} Scanner IF = new Scanner(new File

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.