Summary of basic usage methods of Java file class _java

Source: Internet
Author: User
Tags flush

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.