The use of file in Java io is more frequent and is used in uploading and deleting files. For example, when we write the management system, it is possible to use the image upload, and delete. Then we'll use Java file to handle it.
Basic use of file in Java to create and delete files:
public class Filedemo {public
static void Main (string[] args) {
file F=new file ("D:" +file.separator+ "Io.txt"); C3/>//file.separator get "\"
//file.pathseparator get is ";"
try {
f.createnewfile ();
} catch (IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
}
Wait a while, you can view the generation of the file
try {
thread.sleep (3000);
} catch (Interruptedexception e) {
//TODO Auto-generated Catch block
e.printstacktrace ();
}
if (f.exists ()) {
f.delete ();
} else{
System.out.println ("file does not exist");}}
Java File example uses: The image upload function code used in EE development:
public void FileUpload (@RequestParam multipartfile[] myfiles, httpservletrequest request, HttpServletResponse Respon
SE) throws IOException {String Imgpath = "/uploads" + "/";
File directory = new file (Request.getsession (). Getservletcontext (). Getrealpath ("/") + Imgpath);
String desfilename = null;
String filenewname = null; Response.setcontenttype ("text/html;
Charset=utf-8 ");
PrintWriter out = Response.getwriter ();
String originalfilename = null;
for (Multipartfile myfile:myfiles) {if (Myfile.isempty ()) {out.write ("Please upload after file");
Out.flush ();
else {originalfilename = Myfile.getoriginalfilename (); if (null!= originalfilename && originalfilename.length () > 0) {filenewname = Uuid.randomuuid () + Origina
Lfilename;
Desfilename = directory.tostring () + "/" + filenewname;
try {fileutils.copyinputstreamtofile (Myfile.getinputstream (), New File (Desfilename)); catch (Ioexception e) {e.printstacktrace (); Out.write ("File upload failed, please try again!!")
");
Out.flush ();
}} out.print (Filenewname);
Out.flush ();
}
And where the folder generates the following code:
File F1=new file ("D:" +file.separator+ "test");
F1.mkdir ();
Gets the folder name Method
F1.getname ();
This is the basic use of Java IO, but also the more frequently used part.
The above is the entire content of this article, I hope to help you learn.