📄 filecopyjframe.java
字号:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FileCopyJFrame extends JFrame implements ActionListener
{
JPanel panel=new JPanel();
JLabel sourceFile=new JLabel("源文件路径:");
JTextField sourcePath=new JTextField(20);
JLabel targetFile=new JLabel("目标文件路径:");
JTextField targetPath=new JTextField(20);
JLabel fileName=new JLabel("新文件名:");
JTextField file=new JTextField(10);
JLabel tip=new JLabel("提示:");
JTextField tipcontent=new JTextField(10);
JButton copy=new JButton("复制");
public FileCopyJFrame()
{super("文件COPY小工具");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(680,100);
FlowLayout layout=new FlowLayout(FlowLayout.LEFT);
Container pane=getContentPane();
panel.setLayout(layout);
panel.add(sourceFile);
sourcePath.setBackground(new Color(0,255,255));
panel.add(sourcePath);
panel.setBackground(new Color(255,255,0));
panel.add(targetFile);
targetPath.setBackground(new Color(255,23,23));
panel.add(targetPath);
panel.add(fileName);
file.setBackground(Color.lightGray);
panel.add(file);
panel.add(tip);
tipcontent.setBackground(Color.orange);
panel.add(tipcontent);
panel.add(copy);
copy.addActionListener(this);
pane.add(panel);
setVisible(true);
}
public String getSourceText()
{
String text=sourcePath.getText();
return text;
}
public String getTargetText()
{
String text1=targetPath.getText();
return text1;
}
public String getFileName()
{
String filename=file.getText();
return filename;
}
public void actionPerformed( ActionEvent event)
{
try{String inputFile =getSourceText();
File sourcePath=new File(inputFile);
String outputFile = getTargetText();
File file=new File(outputFile);
String path=file.getAbsolutePath();
File Path=new File(path);
if(!Path.exists())
Path.mkdirs();
String fileName1=getFileName();
File files=new File(fileName1);
if(!files.exists())
files.createNewFile();
File integerPath=new File(Path,fileName1);
RandomAccessFile inf = new RandomAccessFile( sourcePath, "r" );
FileOutputStream outf = new FileOutputStream(integerPath);
long inputLength = new File( inputFile ).length();
FileChannel inc = inf.getChannel();
FileChannel outc = outf.getChannel();
MappedByteBuffer inputData =//将FileChannel inc影射到MappedByteBuffer中
inc.map( FileChannel.MapMode.READ_ONLY, 0, inputLength );
Charset latin1 = Charset.forName( "ISO-8859-1" );//得到某类型的字符集
CharsetDecoder decoder = latin1.newDecoder();//得到解码器
CharsetEncoder encoder = latin1.newEncoder();//得到编码器
CharBuffer cb = decoder.decode( inputData );//将影射得到的进行解码到CharBuff
//er中得到字符类型的缓存
ByteBuffer outputData = encoder.encode( cb );//将解码后的CharBuffer编码成
//ByteBuffer的通用类型
outc.write( outputData );//将ByteBuffer类型的缓存中的内容写到输出管道中
inf.close();
outf.close();
if(files.exists())
tipcontent.setText("文件COPY成功!");
else
tipcontent.setText("文件COPY失败!");
}catch(FileNotFoundException e)
{System.out.println(e);}catch(IOException e)
{System.out.println(e);}
}
public static void main(String [] args)
{FileCopyJFrame frame=new FileCopyJFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -