Java Learning Files Basic operations

Source: Internet
Author: User

First, the file class

Creation of files

Package File;import Java.io.file;import Java.io.ioexception;public class CreateFile {public static void main (string[] args) {//TODO auto-generated method stub      file File=new file ("C:\\users\\public\\desktop\\demo.txt");       try{    file.createnewfile ();        } catch (IOException e) {      e.printstacktrace ();}}      }
deletion of files
Package File;import Java.io.file;public class DeleteFile {public static void main (string[] args) {//TODO auto-generated method stub file fi Le = new File ("C:\\users\\public\\desktop\\demo.txt"); File.delete ();}}
Search for Files

Package File;import Java.io.file;public class Existfile {public static void main (string[] args) {//TODO auto-generated method stub file fil E = new File ("C:\\users\\public\\desktop\\demo.txt"), if (File.exists ()) {System.out.println ("file exists! ");} ELSE{SYSTEM.OUT.PRINTLN ("file does not exist!) ");}}}
the judgment of the path
Package File;import Java.io.file;public class Judgefile {public static void main (string[] args) {//TODO auto-generated method stub file fil E1=new File ("C:\\users\\public\\desktop\\demo.txt"); File File2=new file ("C:\\users\\public\\desktop"); System.out.println (File1.isfile ()); System.out.println (File2.isdirectory ());}}

Catalog Output

Direct file output

Package File;import Java.io.file;public class FilePath {public static void main (string[] args) {//TODO auto-generated method stub file file =new File ("C:\\users\\public\\desktop"); String path[]=file.list (); int len=path.length;for (int i=0;i<len;i++) {System.out.println (path[i]);}}}

Path output

Package File;import Java.io.file;public class FilePath {public static void main (string[] args) {//TODO auto-generated method stub file file =new File ("C:\\users\\public\\desktop"); File[] Path=file.listfiles ();//--------------------This has changed to a file object array element int len=path.length;for (int i=0;i<len;i + +) {System.out.println (path[i]);}}}
Create a folder

Package File;import Java.io.File;
public class Createdirect {public static void main (string[] args) {//TODO auto-generated method stub file File=new file ("C:\\users\\public\ \desktop\\demo "); File.mkdir ();//Create Demo folder}}}


Create a file within a folder while creating a file

Package File;import Java.io.file;import Java.io.ioexception;public class Createdirect {public static void main (string[] args) {//TODO auto-generated method stub file File=new file ("C:\\users\\public\\desktop\\javatest\\demo.txt"); File.getparentfile (). mkdir ();//Create D\\javatest folder try {file.createnewfile ();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();}}


Note: Because the file hierarchy separators for Linux and Windows are both/and \, so in order to implement the Code cross-platform, the \ \ In this article can be changed to File.separator.

Issue: List all files in a directory (including subdocuments in subdirectories)

Import Java.io.file;public class Listdirectorydemo {public static void main (string[] args) {File file = new file ("D:" + Fi le.separator); list (file);} public static void list (file file) {if (File.isdirectory ()) {file lists[] = File.listfiles (), if (lists! = null) {for (int i = 0; i < lists.length; i++) {list (lists[i]);//List Contents}}}system.out.println (file);}}

Second, Randomaccessfile

Construction method: Public randomaccessfile (File file,string mode) throws FileNotFoundException

Write operation:

Import Java.io.file;import Java.io.randomaccessfile;public class Randomwrite {public static void main (string[] args) Throws Exception {//All exceptions throw File File = new file ("C:\\users\\public\\desktop\\demo.txt");//Specify files to manipulate Randomaccessfile RAF = new Randomaccessfile (file, "RW");//write the first data in the form of read/write String name = "Zhangsan";//denotes name int age = 20; Denotes age raf.writebytes (name); Writes a string to Raf.writeint (age) in bytes; Writes the integer data//writes the second data name = "Lisi    ";//indicates the name of age = 19;///means raf.writebytes (name);//Writes a string to Raf.writeint in bytes;//write Enter the integer data//write the third data name = "Wangwu  ";//indicates the name of age = 21;///Indicates the ages raf.writebytes (name);//writes a string in bytes to the raf.writeint;//Writes the whole Type data raf.close ();//file operation must be closed at the end}}
Read operation:
Import Java.io.file;import Java.io.randomaccessfile;public class Randomread {public static void main (string[] args) Throws Exception {//All exceptions throw File File = new file ("C:\\users\\public\\desktop\\demo.txt");//Specify files to manipulate Randomaccessfile RAF = new Randomaccessfile (file, "R");//operation in read form byte b[] = null;//defines the byte array string name = Null;int Age = 0;b = new Byte[8];raf. Skipbytes (12); The information that crosses the first person System.out.println ("The second person's information:"); for (int i = 0; i < 8; i++) {B[i] = Raf.readbyte ();//Read bytes}age = Raf.readin t ();//Read digital System.out.println ("\t|-Name:" + new String (b)); System.out.println ("\t|-Ages:" + age); Raf.seek (0);//return to start position System.out.println ("information of the first person:"); for (int i = 0; i < 8; i++) {b [I] = Raf.readbyte (); Read byte}age = Raf.readint ();//Read digital System.out.println ("\t|-Name:" + new String (b)); System.out.println ("\t|-Ages:" + age); Raf.skipbytes (12); Information to cross the second person System.out.println ("information of the third Person:"); for (int i = 0; i < 8; i++) {B[i] = Raf.readbyte ();//Read bytes}age = Raf.readin t ();//Read digital System.out.println ("\t|-Name:" +New String (b)); System.out.println ("\t|-Ages:" + age); Raf.close ();//file operation must be closed at the end}}


Java Learning Files Basic operations

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.