Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of File Class in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "the use of File classes in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the use of File classes in Java".

The role of the File class

● creates and deletes files

● renames the file

● determines the read and write permissions of a file and whether it exists or not

● settings and query the last modification time of the file, etc.

The ● construction file stream can take an object of the File class as a parameter.

Create a file Hello.txt, delete the old file if it exists, and create a new import java.io.* directly if it does not exist

Public class FileTester {

Public static void main (String [] args) {

File f=new File ("Hello.txt")

If (f.exists ())

F.delete ()

Else

Try {

F.createNewFile ()

}

Catch (Exception e) {

System.out.println (e.getMessage ())

}

}

}

Running result

Because Hello.txt has been created in the previous example, the first run will delete this file

The second run creates an empty file with that name.

Analysis.

Before attempting to open a file, you can use the isFile method of the File class to determine whether the File object represents a file rather than a directory

We can also judge whether the file or path with the same name exists by exists method, and then take the correct method to avoid misoperation.

File copying program import java.io.*

Class NewCopyBytes {

Public static void main (String [] args) {

DataInputStream instr

DataOutputStream outstr

If (args.roomthreading = 2) {

System.out.println ("Please Enter file names!")

Return

}

File inFile = new File (args [0])

File outFile = new File (args [1])

If (outFile.exists ()) {

System.out.println (args [1] + "already exists")

Return

}

If (! inFile.exists ()) {

System.out.println (args [0] + "does not exist")

Return

}

Try {

Instr = new DataInputStream (new BufferedInputStream (new FileInputStream (inFile)

Outstr = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (outFile)

Try {

Int data

While (true) {

Data = instr.readUnsignedByte ()

Outstr.writeByte (data)

}

}

Catch (EOFException eof) {

Outstr.close ()

Instr.close ()

Return

}

}

Catch (FileNotFoundException nfx) {

System.out.println ("Problem opening files")

}

Catch (IOException iox)

{

System.out.println ("IO Problems")

}

}

} at this point, I believe you have a deeper understanding of "the use of File classes in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report