功能代码
@requires_authorization
public static void copyFile ( File srcFile, File destFile) {
InputStream in = null ;
OutputStream out = null ;
try {
in = new FileInputStream ( srcFile) ;
out = new FileOutputStream ( destFile) ;
byte [ ] b = new byte [ 1024 ] ;
int length = in. read ( b) ;
while ( length != - 1 ) {
out. write ( b, 0 , length) ;
length = in. read ( b) ;
} } catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
try {
in. close ( ) ;
out. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
public class FileTool {
public static void copyFile ( String srcPath, String destPath, String srcWord, String destWord) {
copyFile ( new File ( srcPath) , new File ( destPath) , srcWord, destWord) ;
}
public static void copyFile ( File srcFile, File destFile, String srcWord, String destWord) {
InputStream in = null ;
OutputStream out = null ;
try {
in = new FileInputStream ( srcFile) ;
out = new FileOutputStream ( destFile) ;
byte [ ] b = new byte [ 1024 ] ;
int length = in. read ( b) ;
if ( srcFile == null && "" . equals ( srcFile) ) {
while ( length != - 1 ) {
out. write ( b, 0 , length) ;
length = in. read ( b) ;
}
} else {
if ( destWord == null ) {
destWord = "" ;
}
while ( length != - 1 ) {
String string = new String ( b, 0 , length) ;
string = string. replace ( srcWord, destWord) ;
out. write ( b, 0 , length) ;
length = in. read ( b) ;
}
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
try {
in. close ( ) ;
out. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
public static void copyFolder ( String srcPath, String destPath, String srcWord, String destWord) {
copyFolder ( new File ( srcPath) , new File ( destPath) , srcWord, destWord) ;
}
public static void copyFolder ( File srcFile, File destFile, String srcWord, String destWord) {
if ( srcFile != null && srcFile. exists ( ) ) {
if ( srcFile. isDirectory ( ) ) {
if ( ! destFile. exists ( ) ) {
destFile. mkdirs ( ) ;
}
File [ ] files = srcFile. listFiles ( ) ;
for ( File f : files) {
String string = f. getName ( ) ;
File file = new File ( destFile, string) ;
copyFolder ( f, file, srcWord, destWord) ;
}
} else {
copyFile ( srcFile, destFile, srcWord, destWord) ;
}
}
}
public static void deleteFolder ( String srcPath) {
deleteFolder ( new File ( srcPath) ) ;
}
public static void deleteFolder ( File srcFile) {
if ( srcFile. exists ( ) ) {
if ( srcFile. isDirectory ( ) ) {
File [ ] files = srcFile. listFiles ( ) ;
for ( File f : files) {
deleteFolder ( f) ;
}
}
srcFile. delete ( ) ;
} else {
srcFile. delete ( ) ;
}
}
public static void saveFile ( String path, String content) {
saveFile ( new File ( path) , content) ;
}
public static void saveFile ( File file, String content) {
if ( ! file. getParentFile ( ) . exists ( ) ) {
file. mkdirs ( ) ;
}
OutputStream out = null ;
try {
out = new FileOutputStream ( file) ;
out. write ( content. getBytes ( ) ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( out != null )
out. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
out = null ;
}
}
} }
带功能界面
package javaTEST ;
import java. awt. event. ActionEvent ;
import java. awt. event. ActionListener ;
import javax. swing. JButton ;
import javax. swing. JFileChooser ;
import javax. swing. JFrame ;
import javax. swing. JLabel ;
import javax. swing. JTextField ;
import javax. swing. filechooser. FileNameExtensionFilter ;
public class FunctionUI extends JFrame {
static String Path1 ;
static String Path2 ;
static String Path3 ;
static String Path4 ;
public void showUI ( ) {
setSize ( 515 , 450 ) ;
setLocationRelativeTo ( null ) ;
setDefaultCloseOperation ( 3 ) ;
setLayout ( null ) ;
JLabel fileCopy = new JLabel ( "文件拷贝" ) ;
JLabel src = new JLabel ( "源路径" ) ;
JLabel dest = new JLabel ( "目标路径" ) ;
fileCopy. setBounds ( 10 , 12 , 70 , 20 ) ;
src. setBounds ( 10 , 40 , 70 , 20 ) ;
dest. setBounds ( 10 , 65 , 70 , 20 ) ;
JTextField srcPath = new JTextField ( ) ;
srcPath. setEditable ( true ) ;
srcPath. setBounds ( 80 , 40 , 365 , 20 ) ;
JTextField destPath = new JTextField ( ) ;
destPath. setEditable ( true ) ;
destPath. setBounds ( 80 , 65 , 365 , 20 ) ;
JButton button1 = new JButton ( "..." ) ;
button1. setBounds ( 450 , 40 , 30 , 20 ) ;
JButton button2 = new JButton ( "..." ) ;
button2. setBounds ( 450 , 65 , 30 , 20 ) ;
JTextField aaa = new JTextField ( ) ;
JTextField bbb = new JTextField ( ) ;
aaa. setBounds ( 10 , 100 , 200 , 20 ) ;
bbb. setBounds ( 280 , 100 , 200 , 20 ) ;
JLabel convertTo = new JLabel ( "转成" ) ;
convertTo. setBounds ( 230 , 100 , 60 , 20 ) ;
JButton copy = new JButton ( "确认拷贝" ) ;
copy. setBounds ( 195 , 135 , 100 , 20 ) ;
add ( fileCopy) ;
add ( src) ;
add ( dest) ;
add ( srcPath) ;
add ( destPath) ;
add ( button1) ;
add ( button2) ;
add ( aaa) ;
add ( bbb) ;
add ( convertTo) ;
add ( copy) ;
JLabel fileDelete = new JLabel ( "文件删除" ) ;
fileDelete. setBounds ( 10 , 172 , 70 , 20 ) ;
JLabel path = new JLabel ( "路径" ) ;
path. setBounds ( 10 , 200 , 70 , 20 ) ;
JTextField route = new JTextField ( ) ;
route. setEditable ( true ) ;
route. setBounds ( 80 , 200 , 365 , 20 ) ;
JButton button3 = new JButton ( "..." ) ;
button3. setBounds ( 450 , 200 , 30 , 20 ) ;
JButton delete = new JButton ( "删除" ) ;
delete. setBounds ( 195 , 235 , 100 , 20 ) ;
add ( fileDelete) ;
add ( path) ;
add ( route) ;
add ( button3) ;
add ( delete) ;
JLabel fileSave = new JLabel ( "文件保存" ) ;
fileSave. setBounds ( 10 , 277 , 70 , 20 ) ;
JLabel destSave = new JLabel ( "目标路径" ) ;
destSave. setBounds ( 10 , 305 , 70 , 20 ) ;
JTextField savePath = new JTextField ( ) ;
savePath. setBounds ( 80 , 305 , 365 , 20 ) ;
savePath. setEditable ( true ) ;
JTextField enter = new JTextField ( ) ;
enter. setBounds ( 10 , 330 , 415 , 20 ) ;
enter. setEditable ( true ) ;
JButton button4 = new JButton ( "..." ) ;
button4. setBounds ( 450 , 305 , 30 , 20 ) ;
JButton save = new JButton ( "保存" ) ;
save. setBounds ( 430 , 330 , 60 , 20 ) ;
add ( fileSave) ;
add ( destSave) ;
add ( savePath) ;
add ( enter) ;
add ( button4) ;
add ( save) ;
setVisible ( true ) ;
button1. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
JFileChooser j = new JFileChooser ( ) ;
j. setFileSelectionMode ( JFileChooser . FILES_AND_DIRECTORIES ) ;
j. showOpenDialog ( null ) ;
FileNameExtensionFilter filter = new FileNameExtensionFilter ( ".txt" , "txt" ) ;
j. setFileFilter ( filter) ;
Path1 = j. getSelectedFile ( ) . getAbsolutePath ( ) ;
srcPath. setText ( Path1 ) ;
}
} ) ;
button2. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
JFileChooser j = new JFileChooser ( ) ;
j. setFileSelectionMode ( JFileChooser . FILES_AND_DIRECTORIES ) ;
j. showOpenDialog ( null ) ;
Path2 = j. getSelectedFile ( ) . getAbsolutePath ( ) ;
destPath. setText ( Path2 ) ;
}
} ) ;
button3. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
JFileChooser j = new JFileChooser ( ) ;
j. setFileSelectionMode ( JFileChooser . FILES_AND_DIRECTORIES ) ;
j. showOpenDialog ( null ) ;
Path3 = j. getSelectedFile ( ) . getAbsolutePath ( ) ;
route. setText ( Path3 ) ;
}
} ) ;
button4. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
JFileChooser j = new JFileChooser ( ) ;
j. setFileSelectionMode ( JFileChooser . FILES_AND_DIRECTORIES ) ;
j. showOpenDialog ( null ) ;
Path4 = j. getSelectedFile ( ) . getAbsolutePath ( ) ;
savePath. setText ( Path4 ) ;
}
} ) ;
copy. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
String srcWord = aaa. getText ( ) ;
String destWord = bbb. getText ( ) ;
FileTool . copyFile ( Path1 , Path2 , srcWord, destWord) ;
}
} ) ;
delete. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
FileTool ft = new FileTool ( ) ;
Path3 = route. getText ( ) ;
ft. deleteFolder ( Path3 ) ;
}
} ) ;
save. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
FileTool ft = new FileTool ( ) ;
Path4 = savePath. getText ( ) ;
String content = enter. getText ( ) ;
ft. saveFile ( Path4 , content) ;
}
} ) ;
}
public static void main ( String args[ ] ) {
FunctionUI fc = new FunctionUI ( ) ;
fc. showUI ( ) ;
}
}