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

How java moves files and modifies how they are named

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

Share

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

This article mainly shows you "java how to move files and change the name of the way", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "java how to move files and change the name of this article" it.

Java moves the file and changes the name

Cut 1.txt from the source folder, move to the target folder, and rename it to 2.txt

/ / cut 1.txt from the source folder, move to the target folder, and rename it to 2.txt File startFile=new File ("D:\\ source\\ 1.txt"); File endFile=new File ("D:\\ target\\ 2.txt"); if (startFile.renameTo (endFile)) {System.out.println ("File moved successfully! Destination path: {"+ endFile.getAbsolutePath () +"} ");} else {System.out.println (" File move failed! Starting path: {"+ startFile.getAbsolutePath () +"});} copy the file specified in the single-level folder and modify the name * requirement: copy the specified file in the specified directory and modify the suffix. * the specified file is: .java file. * the suffix specified is: .jad * the specified directory is: jad * * data source: e:\\ java\\ A.java * destination: e:\\ jad\\ A.jad * * Analysis: * A: encapsulation directory * B: get the File array of java files in this directory * C: traverse the File array Get each File object * D: copy the File * E: rename package cn.itcast_04 in the destination directory Import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FilenameFilter;import java.io.IOException; / * requirements: copy the specified file in the specified directory and modify the suffix. * the specified file is: .java file. * the suffix specified is: .jad * the specified directory is: jad * * data source: e:\\ java\\ A.java * destination: e:\\ jad\\ A.jad * * Analysis: * A: encapsulation directory * B: get the File array of java files in this directory * C: traverse the File array Get each File object * D: copy the File * E: rename it under the destination directory * / public class CopyFolderDemo {public static void main (String [] args) throws IOException {/ / Encapsulation directory File srcFolder = new File ("e:\\ java") / / Encapsulation destination File destFolder = new File ("e:\\ jad"); / / if the destination directory does not exist, create if (! destFolder.exists ()) {destFolder.mkdir () } / / get the File array of java files in this directory File [] fileArray = srcFolder.listFiles (new FilenameFilter () {@ Override public boolean accept (File dir, String name) {return new File (dir) Name) .isFile () & & name.endsWith (".java") }}); / / iterate through the File array to get each File object for (File file: fileArray) {/ / System.out.println (file) / / data source: e:\ java\ DataTypeDemo.java / / destination: e:\\ jad\ DataTypeDemo.java String name = file.getName (); File newFile = newFile (destFolder, name); copyFile (file, newFile) } / / rename File [] destFileArray = destFolder.listFiles (); for (File destFile: destFileArray) {/ / System.out.println (destFile) under the destination directory / / e:\ jad\ DataTypeDemo.java / / e:\\ jad\\ DataTypeDemo.jad String name = destFile.getName (); / / DataTypeDemo.java String newName = name.replace (".java", ".jad") / / DataTypeDemo.jad File newFile = newFile (destFolder,newName); destFile.renameTo (newFile);}} private static void copyFile (File file, File newFile) throws IOException {BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file)); BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (newFile)) Byte [] bys = new byte [1024]; int len = 0; while ((len = bis.read (bys))! =-1) {bos.write (bys, 0, len);} bos.close (); bis.close () }} above is all the contents of the article "how to move files and change their names in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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